Hi hopefully some one can help me, I am new to Arduino and electronics but have been doing ok. My project is a robot head controlled by a Playstation 2 controller moving servos.
The problem I have is when pressing any controls on the ps2 control the servos do nothing.
Now I am using the drivers and sketch info from PlayStation 2 Controller Arduino Library v1.0 « The Mind of Bill Porter and if I run the example sketch I get all the correct serial information feedback eg controller connected, you just pressed red etc. Then my servos are controlled by 12 Channel Servo*PRO Controller IC and the same with that if I run the example code from hobby tronics pdf it sweeps my servos from left to right.
So the fact that my PS2 Controller functions correctly and my servos on the 12C controller work this leads my to think I have made a programming error. For the purpose of testing I only have 1 servo connected which runs off its own power.
Here is my code:
#include <PS2X_lib.h> //for v1.6
#include <Wire.h>
const int servoslave_address=40; // I2C Address of Servo Chip
PS2X ps2x; // create PS2 Controller Class
int error = 0;
byte type = 0;
byte vibrate = 0;
int ud_eye_val;
int lr_eye_val;
int eyelids_val;
int jaw_val;
int ears_val;
// Write a config value to address register on device
void servoConfig(int device, byte address, byte val) {
Wire.beginTransmission(device); // start transmission to device
Wire.write(address); // send register address
Wire.write(val); // send value to write
Wire.endTransmission(); // end transmission
}
// Write Startup values for each Servo
void servoStartup(int device) {
unsigned char i;
Wire.beginTransmission(device); // start transmission to device
Wire.write(0); // send register address
for(i=0;i<12;i++) Wire.write(0); // send 0 as startup value for each servo
// We could easily send different values for
// each servo
Wire.endTransmission(); // end transmission
}
void moveservo(int numservo, int degree) {
Wire.beginTransmission(servoslave_address); // transmit to device
Wire.write(numservo); // servo register to start from
Wire.write(degree); // send 12 bytes of data
Wire.endTransmission(); // stop transmitting
delay(100); // wait 2 seconds to allow servos to reach end
}
// Setup our Servo Configuration
void setup(){
Wire.begin(); // join i2c bus (address optional for master)
// Set Servo Config and Startup values
servoConfig(servoslave_address, 61, 1); // Extended Mode
servoConfig(servoslave_address, 62, 0); // Servo Update Rate (0-6)
servoStartup(servoslave_address); // Send Startup values for servos
servoConfig(servoslave_address, 60, 1); // Servo Output ON
Serial.begin(57600);
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("Found Controller, configured successful");
}
}
void loop(){
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");
if(ps2x.Button(PSB_SELECT))
Serial.println("Select is being held");
if(ps2x.Button(PSB_PAD_UP)) { //will be TRUE as long as button is pressed
Serial.print("Up held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
}
if(ps2x.Button(PSB_PAD_RIGHT)){
Serial.print("Right held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
}
if(ps2x.Button(PSB_PAD_LEFT)){
Serial.print("LEFT held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
}
if(ps2x.Button(PSB_PAD_DOWN)){
Serial.print("DOWN held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
}
if (ps2x.NewButtonState()) //will be TRUE if any button changes state (on to off, or off to on)
{
if(ps2x.Button(PSB_L2))
if(ps2x.Button(PSB_L3))
if(ps2x.Button(PSB_R2))
if(ps2x.Button(PSB_R3))
if(ps2x.Button(PSB_GREEN))
if(ps2x.ButtonPressed(PSB_RED))
if(ps2x.ButtonReleased(PSB_PINK))
if(ps2x.NewButtonState(PSB_BLUE))
{
moveservo(3,255); // ear wiggle
delay (200);
moveservo(3,0); // ear wiggle
}
if(ps2x.Button(PSB_R1)) // print stick values if either is TRUE
if(ps2x.Button(PSB_L1)) // print stick values if either is TRUE
ud_eye_val = ps2x.Analog(PSS_LY), DEC; // reads the value of the potentiometer (value between 0 and 1023)
moveservo(0,ud_eye_val); // move eyes up or down
delay(5);
lr_eye_val = ps2x.Analog(PSS_LX), DEC; // reads the value of the potentiometer (value between 0 and 1023)
moveservo(1,lr_eye_val); // move eyes left or right
delay(5);
jaw_val = ps2x.Analog(PSS_RY), DEC; // reads the value of the potentiometer (value between 0 and 1023)
moveservo(2,jaw_val); // open or close mouth
delay(5);
}
}