Check out this sketch I made for my robot.
#include <PS2X_lib.h> //for v1.6
PS2X ps2x; // create PS2 Controller Class
int error = 0;
byte type = 0;
byte vibrate = 0;
byte speed1 =128, speed2 = 128;
byte M1L = 5;// PWM
byte M1R = 3;// PWM
byte M2L = 6;// PWM
byte M2R = 9;// PWM
void setup()
{
Serial.begin(115200);
//**************PAY ATTENTION*************
pinMode(M1L, OUTPUT); // Establishes LEDPin as an output so the LED can be seen
pinMode(M1R, OUTPUT);
pinMode(M2L, OUTPUT); // Establishes LEDPin as an output so the LED can be seen
pinMode(M2R, OUTPUT);
error = ps2x.config_gamepad(13,11,10,12, true, true); //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
if(error == 0)
Serial.println("Controller found! You may now send commands");
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");
break;
case 1:
Serial.println("DualShock Controller Found");
break;
}
}
void loop()
{
if(error == 1) //skip loop if no controller found
return;
else //DualShock Controller
{
ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
if(ps2x.Button(PSB_L1))//open claw manual
{
// rotate turret left
}
else if(ps2x.Button(PSB_R1))//close claw manual
{
// rotate turret right
}
else
{
// do nothing
}
// My sketch is designed to use both joysticks, Left joystick controls the left motor and right joystick does the right motor.
// I can condense these two into just one IF / ELSE IF / ELSE statement, but here I separated them to show how one joystick controls its motor.
if(ps2x.Analog(PSS_LY) >= 136 && ps2x.Analog(PSS_LY) <= 255)//real center value is 128, but 140 is needed because controller is HIGHLY sensitive
{
speed1 = map(ps2x.Analog(PSS_LY),136 , 255, 0 , 255);
analogWrite(M1L, speed1);
digitalWrite(M1R, LOW);
}
else if(ps2x.Analog(PSS_LY) >= 0 && ps2x.Analog(PSS_LY) <= 120) //Same as above
{
speed1 = map(ps2x.Analog(PSS_LY),0 ,120 , 255 , 0);// create a set range for values and set output values respectively
digitalWrite(M1L, LOW);
analogWrite(M1R, speed1);
}
else {
digitalWrite(M1L, LOW);// all off
digitalWrite(M1R, LOW);
}
//--------------------Right side motor-----------------------
if(ps2x.Analog(PSS_RY) >= 136 && ps2x.Analog(PSS_RY) <= 255)
{
speed2 = map(ps2x.Analog(PSS_RY),136 , 255, 0 , 255);
analogWrite(M2L, speed2);
digitalWrite(M2R, LOW);
}
else if(ps2x.Analog(PSS_RY) >= 0 && ps2x.Analog(PSS_RY) <= 120)
{
speed2 = map(ps2x.Analog(PSS_RY),0 ,120 , 255 , 0);
digitalWrite(M2L, LOW);
analogWrite(M2R, speed2);
}
else
{
digitalWrite(M2L, LOW);
digitalWrite(M2R, LOW);
}
}
delay(50); // not needed, but give the robot some time to finish inputted commands
}