milford ohio
Offline
Newbie
Karma: 0
Posts: 3
|
 |
« Reply #1 on: December 10, 2012, 06:54:13 pm » |
Ok last night i found this code, and uploaded it to the arduino. Pins 11 and 9 are motor outputs, it works ok, still having problems with the joysticks. When i press R1,L1 on the controller this allows me too use the joysticks to control the speed of the motors, the problem is that i have to hold the joy sticks all the way down or the motors will spin about half speed. I have tried to fix this in the code but have not had any luck. here is the code!
#include <PS2X_lib.h>
PS2X ps2x;
//int Lp = 7; //int Ln = 4; //int Rp = 8; //int Rn = 12; int El = 11; int Er = 9; int right_speed = 0; int left_speed = 0;
void setup () { ps2x.config_gamepad(2,6,4,7, true, true); //pinMode (Lp, OUTPUT); //pinMode (Ln, OUTPUT); //pinMode (Rp, OUTPUT); //pinMode (Rn, OUTPUT); pinMode (El, OUTPUT); pinMode (Er, OUTPUT); digitalWrite (El,LOW); digitalWrite (Er,LOW); Serial.begin(9600); }
void loop () { ps2x.read_gamepad(); left_speed = ps2x.Analog(PSS_LY); right_speed = ps2x.Analog(PSS_RY); left_speed = 255 - left_speed; right_speed = 255 - right_speed; left_speed = map(left_speed, 127, 255, -255, 255); right_speed = map(right_speed, 127, 255, -255, 255); if((left_speed>=-25) && (left_speed<=45)) { left_speed = 0; } //WHAT SHOULD I PUT IN THE ELSE STATEMENT?? Serial.print("Left Joystick Value: "); Serial.print(left_speed); Serial.print("\t"); Serial.print("Right Joystick Value: "); Serial.println(right_speed); if(ps2x.Button(PSB_L1) && ps2x.Button(PSB_R1)) { analogWrite(Er, right_speed); analogWrite(El, left_speed); } else { analogWrite(Er, 0); analogWrite(El, 0); } }
|