Hey I am interfacing Ps2 with an arduino to control servo motor wirelessly.My code is able to detect ps2 but it's not reading input from ps2.please tell me Is there any problem in my code.Here is my code::
#include <Servo.h>
#include <PS2X_lib.h>
Servo myservo;
PS2X ps2x;
int ps2x_error = 0;
byte ps2x_type = 0;
byte ps2x_vibrate = 0;
int pos = 0;// variable to store the servo position
const byte servoPin = 9;
byte incomingByte;
void setup()
{
{
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(servoPin, OUTPUT);
}
ps2x_error = ps2x.config_gamepad(13,10,11,12, false, false);
if(ps2x_error == 0)
Serial.println("Found Controller, configured successful");
else if(ps2x_error == 1)
Serial.println("No controller found, check wiring.");
else if(ps2x_error == 2)
Serial.println("Controller found but not accepting commands.");
else if(ps2x_error == 3)
Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
// get which type
ps2x_type = ps2x.readType();
switch(ps2x_type)
{
case 0: Serial.println("Unknown Controller type"); break;
case 1: Serial.println("DualShock Controller Found"); break;
}
}
void loop () {
if((ps2x_error == 1) || (ps2x_error == 2) || (ps2x_error == 3))
{
//skip loop if no controller found
return;
}
else if(ps2x_error == 0)
{
if(ps2x_type == 1)
{
ps2x.read_gamepad(); //read controller
if(ps2x.Button(PSB_PAD_UP))
{
Serial.println("Up ");
for(pos = 0; pos < 90; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
else
{
Serial.println("DOWN ");
for(pos = 90; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
}
}
delay(50);
}