Hello, im kind of new to botboarduino and i have been working on a project involving the Adafruit motorshield V2, Botboarduino, 2 DC motors and the PS2 robot controller (v2). For some reason when i run the PS2 example code from the library, everything works fine, but when i run the program i’ve created to work with the motor shield, after about 30 seconds i lose connection to the controller (one of the red buuttons on the reciever start to flash and the ps2 controller starts to flash and i lose connection to the motor). (The code is not quite finished yet i stopped because of this issue). I’m powering both the motorshield and the botboarduino with 2 separate 9 v batteries.I basically just took snippets from the example codes so i dont know why i keep losing connection. Here is the current code ive been working on. Any help would be appreciated, thanks!
[code]#include <Wire.h> #include <Adafruit_MotorShield.h> #include “utility/Adafruit_PWMServoDriver.h” #include <PS2X_lib.h> //for v1.6
byte type = 0;
int error = 0;
int leftStick=128;
int rightStick=128;
PS2X ps2x; // create PS2 Controller Class
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
// Select which ‘port’ M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *rightMotor = AFMS.getMotor(1);
// You can also make another motor on port M2
Adafruit_DCMotor *leftMotor = AFMS.getMotor(4);
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bp
AFMS.begin(1000); // create with the default frequency 1.6KHz //AFMS.begin(1000); // OR with a different frequency, say 1KHz
error = ps2x.config_gamepad(13,11,10,12, true, true);
Serial.println(“WORK!”);
// Set the speed to start, from 0 (off) to 255 (max speed)
rightMotor->setSpeed(255);
rightMotor->run(FORWARD);
// turn on motor
rightMotor->run(RELEASE);
leftMotor->setSpeed(255);
leftMotor->run(FORWARD);
// turn on motor
leftMotor->run(RELEASE);
}
void loop() {
ps2x.read_gamepad();
int leftStick= ps2x.Analog(PSS_LY);
if (leftStick != 128){
Serial.println (leftStick);
}
if(ps2x.ButtonPressed(PSB_R1)) {
Serial.println(“R1 just pressed”);
rightMotor->run(FORWARD);
rightMotor->setSpeed(255);
}
if (ps2x.ButtonReleased(PSB_R1)) {
Serial.println(“R1 just released”);
rightMotor->setSpeed(0);
}
if(ps2x.ButtonPressed(PSB_L1)) {
Serial.println(“L1 just pressed”);
leftMotor->run(FORWARD);
leftMotor->setSpeed(255);
}
if (ps2x.ButtonReleased(PSB_L1)) {
Serial.println(“L1 just released”);
leftMotor->setSpeed(0);
}
if(ps2x.ButtonPressed(PSB_L2))
{ leftMotor->run (BACKWARD);
leftMotor->setSpeed (255);
Serial.println(“L2 just Pressed”);
}
if(ps2x.ButtonPressed(PSB_R2))
{ rightMotor->run (BACKWARD);
rightMotor->setSpeed (255);
Serial.println(“R2 just Pressed”);
}
if (ps2x.ButtonReleased(PSB_L2)) {
Serial.println(“L2 just released”);
leftMotor->setSpeed(0);
}
if (ps2x.ButtonReleased(PSB_R2)) {
Serial.println(“R2 just released”);
rightMotor->setSpeed(0);
}
Sometimes I find the PS2 becomes sensitive to timing. If you query too often, for example it gives it no time to ask the actual PS2 for it’s new data. So you might try a short pause at the end of your loop.
Also In the past I found that the PS2 sometimes drops out of the right mode and so many times in my code I have some hacks in place to detect and try to recover.
Example with the Phoenix code base with PS2 input, I have code that looks like:
...
ps2x.read_gamepad(); //read controller and set large motor to spin at 'vibrate' speed
// Wish the library had a valid way to verify that the read_gamepad succeeded... Will hack for now
if ((ps2x.Analog(1) & 0xf0) == 0x70) {
// do all of the stuff I want to do with valid data.
...
g_sPS2ErrorCnt = 0; // reset error count
} else {
// We may have lost the PS2... See what we can do to recover...
if (g_sPS2ErrorCnt < MAXPS2ERRORCNT)
g_sPS2ErrorCnt++; // Increment the error count and if to many errors, turn off the robot.
else if (g_InControlState.fRobotOn)
PS2TurnRobotOff();
ps2x.reconfig_gamepad();
}
So it tries to detect we are in PS2 analog mode, if not, it tries to get back into analog mode. If we fail too many times, then turn off the robot (stop the motors) as we no longer have control.