Hey guys,
I'm brand new to Arduino and currently trying to get my 4WD model working. I've uploaded a video to show what is happening in the hope that someone can help.
Immediately after uploading the code to my Arduino board, all 4 wheels will spin (polarity is wrong so 2 spin the wrong way). After that only 2 spin and it is intermittent, the red light on my controller also flashes and I assume this is why the spin is intermittent. I'm grateful for any help. I have also uploaded my code below.
Thank you
/******************************************************************
set pins connected to PS2 controller:
- 1e column: original
- 2e colmun: Stef?
replace pin numbers by the ones you use
******************************************************************/
#define PS2_DAT 11
#define PS2_CMD 10
#define PS2_SEL 9
#define PS2_CLK 8
#define IN1 33
#define IN2 35
#define IN3 37
#define IN4 39
#define ENA 31
#define ENB 41
//analog values read from PS2 joysticks
int LX = 0;
int LY = 0;
int RX = 0;
int RY = 0;
/******************************************************************
select modes of PS2 controller:
- pressures = analog reading of push-butttons
- rumble = motor rumbling
uncomment 1 of the lines for each mode selection
******************************************************************/
#define pressures true
//#define pressures false
#define rumble true
//#define rumble false
PS2X ps2x; // create PS2 Controller Class
int error = 0;
byte type = 0;
byte vibrate = 0;
void setup() {
Serial.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
digitalWrite(ENA, LOW);
digitalWrite(ENB, LOW);
delay(300); //added delay to give wireless ps2 module some time to startup, before configuring it
//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 ");
}
else if (error == 1)
Serial.print("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
else if (error == 2)
Serial.print("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
else if (error == 3)
Serial.print("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(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
delay(50);
if (ps2x.Button(PSB_L1))
{
LY = ps2x.Analog(PSS_LY); //receive values from p22 joystick
LX = ps2x.Analog(PSS_LX);
}
if (ps2x.Button(PSB_R1))
{
RY = ps2x.Analog(PSS_RY);
RX = ps2x.Analog(PSS_RX);
}
if (LY > 200 || RY > 200) //check if the joystick pushed up side
{
REV();
}
if (LY < 100 || RY < 100)
{
forward();
}
if (LX < 100 || RX < 100)
{
left();
}
if (LX > 200 || RX > 200)
{
right();
}
if(LX == 128 && LY == 128 && RX == 128 && RY == 128)
{
waithere();
}
LY = LX = 128; //return to default vlaues
RY = RX = 128; //return to default values
}
void forward()
{
//Serial.print("forward");
digitalWrite(ENA, HIGH);
digitalWrite(ENB, HIGH);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void REV()
{
//Serial.print("rev");
digitalWrite(ENA, HIGH);
digitalWrite(ENB, HIGH);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void left()
{
//Serial.print("left");
digitalWrite(ENA, LOW);
digitalWrite(ENB, HIGH);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void right()
{
//Serial.print("right");
digitalWrite(ENA, HIGH);
digitalWrite(ENB, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
}
void waithere()
{
//Serial.print("stop");
digitalWrite(ENA, LOW);
digitalWrite(ENB, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
//**************END**************************