学习灌水

注意周一:上午是毛概(超星课程),以及工程材料(QQ直播)

 

 

  1 // PS2 Tank by Igor Fonseca @2019
  2 // Controls a robotic tank using a PS2 joystick, using D-pad buttons
  3 // based on an example using the PS2X library by Bill Porter 2011
  4 // All text above must be included in any redistribution.
  5 
  6 // include libraries
  7 #include <PS2X_lib.h>
  8 
  9 // These are used to set the direction of the bridge driver.
 10 #define ENA 3      //ENA
 11 #define MOTORA_1 4 //IN3
 12 #define MOTORA_2 5 //IN4
 13 #define MOTORB_1 8 //IN1
 14 #define MOTORB_2 7 //IN2
 15 #define ENB 6      //ENB
 16 
 17 PS2X ps2x; // create PS2 Controller Class
 18 
 19 //right now, the library does NOT support hot pluggable controllers, meaning 
 20 //you must always either restart your Arduino after you conect the controller, 
 21 //or call config_gamepad(pins) again after connecting the controller.
 22 int error = 0; 
 23 byte type = 0;
 24 byte vibrate = 0;
 25 
 26 void setup(){
 27 
 28  // Configure output pins
 29  pinMode(ENA, OUTPUT);
 30  pinMode(MOTORA_1, OUTPUT);
 31  pinMode(MOTORA_2, OUTPUT);
 32  pinMode(ENB, OUTPUT);
 33  pinMode(MOTORB_1, OUTPUT);
 34  pinMode(MOTORB_2, OUTPUT);
 35 
 36  // Disable both motors
 37  digitalWrite(ENA,0);
 38  digitalWrite(ENB,0);
 39 
 40  // Start serial communication
 41  Serial.begin(57600);
 42   
 43  error = ps2x.config_gamepad(13,11,10,12, true, true);   //setup pins and settings:  GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
 44 
 45  // Check for error
 46  if(error == 0){
 47   Serial.println("Found Controller, configured successful");
 48  }
 49    
 50  else if(error == 1)
 51   Serial.println("No controller found, check wiring or reset the Arduino");
 52    
 53  else if(error == 2)
 54   Serial.println("Controller found but not accepting commands");
 55   
 56  else if(error == 3)
 57   Serial.println("Controller refusing to enter Pressures mode, may not support it.");
 58    
 59  // Check for the type of controller
 60  type = ps2x.readType();
 61  switch(type) {
 62   case 0:
 63     Serial.println("Unknown Controller type");
 64     break;
 65   case 1:
 66     Serial.println("DualShock Controller Found");
 67     break;
 68   case 2:
 69     Serial.println("GuitarHero Controller Found");
 70     break;
 71   }
 72 }
 73 
 74 // Main loop
 75 void loop(){
 76    
 77  if(error == 1) //skip loop if no controller found
 78   return; 
 79   
 80  else { //DualShock Controller
 81   
 82     ps2x.read_gamepad(false, vibrate); // disable vibration of the controller
 83 
 84      // Perform movements based on D-pad buttons
 85      
 86      // MOVE FORWARD
 87      if(ps2x.Button(PSB_PAD_UP)) {
 88         digitalWrite(MOTORA_1,LOW);
 89         digitalWrite(MOTORA_2,HIGH);
 90         digitalWrite(MOTORB_1,HIGH);
 91         digitalWrite(MOTORB_2,LOW);
 92         analogWrite(ENB, 1023);
 93         analogWrite(ENA, 1023);
 94         Serial.println("Move forward");
 95       }
 96       // TURN RIGHT
 97       if(ps2x.Button(PSB_PAD_RIGHT)){
 98         digitalWrite(MOTORA_1,HIGH);
 99         digitalWrite(MOTORA_2,LOW);
100         digitalWrite(MOTORB_1,HIGH);
101         digitalWrite(MOTORB_2,LOW);
102         analogWrite(ENB, 1023);
103         analogWrite(ENA, 1023);
104         Serial.println("Turn right");
105       }
106       // TURN LEFT
107       if(ps2x.Button(PSB_PAD_LEFT)){
108         digitalWrite(MOTORA_1,LOW);
109         digitalWrite(MOTORA_2,HIGH);
110         digitalWrite(MOTORB_1,LOW);
111         digitalWrite(MOTORB_2,HIGH);
112         analogWrite(ENB, 1023);
113         analogWrite(ENA, 1023);
114         Serial.println("Turn left");
115       }
116       // MOVE BACK
117       if(ps2x.Button(PSB_PAD_DOWN)){
118         digitalWrite(MOTORA_1,HIGH);
119         digitalWrite(MOTORA_2,LOW);
120         digitalWrite(MOTORB_1,LOW);
121         digitalWrite(MOTORB_2,HIGH);
122         analogWrite(ENB, 1023);
123         analogWrite(ENA, 1023);         
124         Serial.println("Move back");
125       }  
126       if (!ps2x.Button(PSB_PAD_DOWN) && !ps2x.Button(PSB_PAD_UP) && !ps2x.Button(PSB_PAD_RIGHT) && !ps2x.Button(PSB_PAD_LEFT)) {
127         analogWrite(ENB, 0);
128         analogWrite(ENA, 0);
129       }
130           
131  delay(50);
132  }    
133 }
原文地址:https://www.cnblogs.com/shewhen/p/12342680.html