hi everyone.
i’d want to make a project, that could control the motor dc using joystick from ps2.
first i use a motor dc, with a transistor, resistor and dioda to connect to arduino. Meanwhile, the controller ps2 is connected to arduino.
The sticks value from joystick ps2 are being read by the arduino, but my motor wouldnt stop spinning.
and, here its my code.
#include <PS2X_lib.h>
#define PS2_DAT 13 //14
#define PS2_CMD 11 //15
#define PS2_SEL 10 //16
#define PS2_CLK 12 //17
//#define pressures true
#define pressures false
//#define rumble true
#define rumble false
PS2X ps2x;
byte motor1=5;
int speed =128;
int error =0;
byte type = 0;
byte vibrate = 0;
int RX=0,RY=0,LX=0,LY=0;
void setup(){
Serial.begin(57600);
delay(300);
pinMode (motor1,OUTPUT);
//added delay to give wireless ps2 module some time to startup, before configuring it
//CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************
//setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
if(error == 0){
Serial.print("Found Controller, configured successful ");
Serial.print("pressures = ");
if (pressures)
Serial.println("true ");
else
Serial.println("false");
Serial.print("rumble = ");
if (rumble)
Serial.println("true)");
else
Serial.println("false");
Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
Serial.println("holding L1 or R1 will print out the analog stick values.");
Serial.println("Note: Go to www.billporter.info for updates and to report bugs.");
}
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. ");
// Serial.print(ps2x.Analog(1), HEX);
type = ps2x.readType();
switch(type) {
case 0:
Serial.print("Unknown Controller type found ");
break;
case 1:
Serial.print("DualShock Controller found ");
break;
case 2:
Serial.print("GuitarHero Controller found ");
break;
case 3:
Serial.print("Wireless Sony DualShock Controller found ");
break;
}
}
void loop ()
{
ps2x.read_gamepad();
if((ps2x.Analog(PSS_RY) < 128))
{
speed =map(ps2x.Analog(PSS_RY), 0 , 128, 0 , 255);
analogWrite(motor1, speed);
digitalWrite(motor1,LOW);
delay (50);
}
else {
digitalWrite(motor1, LOW);
digitalWrite(motor1,LOW);
delay (50);
}
Serial.print("Stick Values:");
Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX
Serial.print(",");
Serial.print(ps2x.Analog(PSS_LX), DEC);
Serial.print(",");
Serial.print(ps2x.Analog(PSS_RY), DEC);
Serial.print(",");
Serial.println(ps2x.Analog(PSS_RX), DEC);
delay(50);
}
i hope everyone here could help me, because i already opened may source, but couldnt get the answer