Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <PS2X_lib.h> //for v1.6
- 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 conect the controller,
- //or call config_gamepad(pins) again after connecting the controller.
- int error = 0;
- byte type = 0;
- byte vibrate = 0;
- int a = 0;
- //Arduino PWM Speed Control for DFRobot Motor Shield (default pins)
- int E1 = 3;
- int M1 = 12;
- int E2 = 11;
- int M2 = 13;
- int lmotor = 0;
- int rmotor = 0;
- int led = 10;
- int light = 2;
- void setup()
- {
- Serial.begin(57600);
- // set pin modes for DFRobot Motor Shield. These are the modes for both motors. For example: M1 is for motor 1 and M2 is for motor 2.
- pinMode(M1, OUTPUT);
- pinMode(M2, OUTPUT);
- pinMode(led, OUTPUT);
- pinMode(light, OUTPUT);
- error = ps2x.config_gamepad(7,6,5,4, true, true); //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
- if(error == 0)
- {
- Serial.println("Found Controller, configured successful");
- 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("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();
- if (type != 1)
- {
- Serial.println("warning: DualShock Controller Not Found!");
- }
- }
- void loop()
- {
- if(error == 1) //skip loop if no controller found
- return;
- if (type == 1)
- {
- ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
- if (ps2x.Button(PSB_PINK)){ // On this line I just added another button that I can use on my controller to turn on muy led lights. The color of the button is pink so that is what it's called.
- if( a == 0)
- a = 1;
- else
- a = 0;}
- if (a == 0){
- digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
- digitalWrite(light, LOW); // turn the LED off by making the voltage LOW
- }
- else
- {
- digitalWrite(led, HIGH); // turn the LED off by making the voltage LOW
- digitalWrite(light, HIGH); // turn the LED off by making the voltage LOW
- }
- lmotor = 0;
- if (ps2x.Button(PSB_L1)) // this is the left motor speed. 255 is the top speed that the tank goes forward for the left motor. The -255 is the top speed that the tank goes backwards.
- // it is the same way with the right motor. It works the same way that the right motor works.
- lmotor = 255;
- if (ps2x.Button(PSB_L2))
- lmotor = -255;
- rmotor = 0;
- if (ps2x.Button(PSB_R1))
- rmotor = 255;
- if (ps2x.Button(PSB_R2))
- rmotor = -255;
- }
- else
- {
- lmotor = 0;
- rmotor = 0;
- }
- // update motors
- if (lmotor < 0)
- {
- digitalWrite(M1, LOW);
- analogWrite(E1, -lmotor); //PWM Speed Control
- }
- else
- {
- digitalWrite(M1, HIGH);
- analogWrite(E1, lmotor); //PWM Speed Control
- }
- if (rmotor < 0)
- {
- digitalWrite(M2, LOW);
- analogWrite(E2, -rmotor); //PWM Speed Control
- }
- else
- {
- digitalWrite(M2, HIGH);
- analogWrite(E2, rmotor); //PWM Speed Control
- }
- // the HIGH means that the motors are at full speed and it is what makes it move. The low means that the motors are stopped.
- delay(30); // I added the led code in this so that I can get everything on my tank working together.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement