久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1760|回復: 0
打印 上一主題 下一主題
收起左側

PS2 arduino l298n

[復制鏈接]
跳轉到指定樓層
樓主
ID:328989 發表于 2018-5-13 17:21 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
  #include <PS2X_lib.h>  //for v1.6
/******************************************************************
* set pins connected to PS2 controller:
*   - 1e column: original
*   - 2e colmun: Stef?
* replace pin numbers by the ones you use
******************************************************************/
//PS2
#define PS2_DAT        13  //14   
#define PS2_CMD        11  //15
#define PS2_SEL        10  //16
#define PS2_CLK        12  //17
// 電機控制引腳
#define IN1 4
#define IN2 5
#define IN3 6
#define IN4 7
//PWM控制引腳;
  int speedPinA = 8;
  int speedPinB = 9;

int speed;
/******************************************************************
* select modes of PS2 controller:
*   - pressures = analog reading of push-butttons
*   - rumble    = motor rumbling
* uncomment 1 of the lines for each mode selection
******************************************************************/
#define pressures   true
//#define pressures   false
#define rumble      true
//#define rumble      false

PS2X ps2x; // create PS2 Controller Class

//right now, the library does NOT support hot pluggable controllers, meaning
//you must always either restart your Arduino after you connect the controller,
//or call config_gamepad(pins) again after connecting the controller.

int error = 0;
byte type = 0;
byte vibrate = 0;

// Reset func
void (* resetFunc) (void) = 0;

void setup(){
               pinMode(IN1, OUTPUT);
               pinMode(IN2, OUTPUT);
               pinMode(IN3, OUTPUT);
               pinMode(IN4, OUTPUT);

                 speed =200;
                Serial.begin(115200);

                delay(300);  //added delay to give wireless ps2 module some time to startup, before configuring it

  //CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************

  //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
  error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);

  if(error == 0){
    Serial.print("Found Controller, configured successful ");
    Serial.print("pressures = ");
  if (pressures)
    Serial.println("true ");
  else
    Serial.println("false");
  Serial.print("rumble = ");
  if (rumble)
    Serial.println("true)");
  else
    Serial.println("false");
    Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
    Serial.println("holding L1 or R1 will print out the analog stick values.");
    Serial.println("Note: Go to www.billporter.info for updates and to report bugs.");
  }  
  else if(error == 1)
    Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");

  else if(error == 2)
    Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");

  else if(error == 3)
    Serial.println("Controller refusing to enter Pressures mode, may not support it. ");

  type = ps2x.readType();
  switch(type) {
    case 0:
      Serial.println("Unknown Controller type found ");
      break;
    case 1:
      Serial.println("DualShock Controller found ");
      break;
    case 2:
      Serial.println("GuitarHero Controller found ");
      break;
  case 3:
      Serial.println("Wireless Sony DualShock Controller found ");
      break;
   }
}
void turnLeft()
{
   digitalWrite(IN1,LOW);
   digitalWrite(IN2, HIGH);
   digitalWrite(IN3, HIGH);
   digitalWrite(IN4,LOW);
}
void turnRight()
{
  digitalWrite(IN1,HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
}

void forward()
{
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
}

void back()
{
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
}
void stop()
{
  digitalWrite(IN1, LOW);
  digitalWrite(IN2,LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}
void loop() {
  /* You must Read Gamepad to get new values and set vibration values
     ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
     if you don't enable the rumble, use ps2x.read_gamepad(); with no values
     You should call this at least once a second
   */  
  if(error == 1){ //skip loop if no controller found
    return;
  }

  if(type == 2){ //Guitar Hero Controller
    return;
  }
  else { //DualShock Controller
    ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
    //開始運行
    if(ps2x.Button(PSB_START)) {        //will be TRUE as long as button is pressed
      Serial.println("Start is being held");
      speed = 120;
      analogWrite(speedPinA, speed);
       analogWrite(speedPinB, speed);
       forward();
    }
    if(ps2x.Button(PSB_SELECT)){
      Serial.println("stop");
   speed = 0;
   analogWrite(speedPinA,speed);
   analogWrite(speedPinB,speed);
   stop();
  }     

    if(ps2x.Button(PSB_PAD_UP)) {      //will be TRUE as long as button is pressed
     Serial.println("Up held this hard: ");
     //speed= 200;
      analogWrite(speedPinA, speed);
      analogWrite(speedPinB, speed);
      forward();
    }
    if(ps2x.Button(PSB_PAD_RIGHT)){
      Serial.println("turn right");
    analogWrite(speedPinA, 0);
    analogWrite(speedPinB, speed);
    turnRight();

    }
  if(ps2x.Button(PSB_PAD_LEFT)){
     Serial.println("turn left ");
       analogWrite(speedPinA, speed);
       analogWrite(speedPinB, 0);
      turnLeft();
    }
    if(ps2x.Button(PSB_PAD_DOWN)){
      Serial.print("Down held this hard: ");
  //    speed= 200;
      analogWrite(speedPinA, speed);
      analogWrite(speedPinB, speed);
      back();
    }  
  }
}

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 亚洲一区二区av在线 | 亚洲一区 | 日本一区二区三区免费观看 | 日韩在线免费视频 | 欧美日韩一区二区在线播放 | 国产一区二区三区高清 | 亚洲精品一区二三区不卡 | 精品网 | 韩日一区二区 | 欧美黄色一级毛片 | 日韩在线欧美 | 国产91一区 | 在线第一页 | 午夜影院网站 | 国产精品久久久久久久久久久久午夜片 | 一本岛道一二三不卡区 | 国产精品亚洲综合 | 91精品一区 | 欧美高清hd | 国产精品久久久久久久午夜片 | 久久精品国产一区二区电影 | 久久成人精品视频 | 亚洲成人国产精品 | 久久国产精品久久久久久 | 超碰免费在线观看 | 亚洲精品久久久一区二区三区 | 91av亚洲 | 亚洲综合区 | 激情毛片 | 欧美精品一区二区三区在线 | 久久综合久色欧美综合狠狠 | 日韩在线一区二区 | 国产高清一区二区三区 | 亚洲视频中文字幕 | 精品熟人一区二区三区四区 | 日本一区二区三区视频在线 | 亚洲一区二区三区在线视频 | 一区二区三区精品 | 久久一级免费视频 | 综合久久综合久久 | 亚洲永久在线 |