so I want to make a car controlled using arduino and ps2 controlller.
This is my wiring, and I have double checked it, it is correct as shown
and I have used this ps2x library
and this is the sketch
// include libraries
#include <PS2X_lib.h>
// These are used to set the direction of the bridge driver.
#define ENA 3 //ENA
#define MOTORA_1 4 //IN3
#define MOTORA_2 5 //IN4
#define MOTORB_1 8 //IN1
#define MOTORB_2 7 //IN2
#define ENB 6 //ENB
int motor_right_speed = 0;
int motor_left_speed = 0;
PS2X ps2x; // create PS2 Controller Class
//right now, the library does NOT support hot pluggable controllers, meaning
//you must always either restart your Arduino after you conect the controller,
//or call config_gamepad(pins) again after connecting the controller.
int error = 0;
byte type = 0;
byte vibrate = 0;
void setup(){
// Configure output pins
pinMode(ENA, OUTPUT);
pinMode(MOTORA_1, OUTPUT);
pinMode(MOTORA_2, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(MOTORB_1, OUTPUT);
pinMode(MOTORB_2, OUTPUT);
// Disable both motors
digitalWrite(ENA,0);
digitalWrite(ENB,0);
// Start serial communication
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
// Check for error
if(error == 0){
Serial.println("Found Controller, configured successful");
}
else if(error == 1)
Serial.println("No controller found, check wiring or reset the Arduino");
else if(error == 2)
Serial.println("Controller found but not accepting commands");
else if(error == 3)
Serial.println("Controller refusing to enter Pressures mode, may not support it.");
// Check for the type of controller
type = ps2x.readType();
switch(type) {
case 0:
Serial.println("Unknown Controller type");
break;
case 1:
Serial.println("DualShock Controller Found");
break;
case 2:
Serial.println("GuitarHero Controller Found");
break;
}
}
// Main loop
void loop(){
if(error == 1) //skip loop if no controller found
return;
else { //DualShock Controller
ps2x.read_gamepad(false, vibrate); // disable vibration of the controller
int nJoyL = ps2x.Analog(PSS_LX); // read left stick
nJoyL = map(nJoyL, 0, 255, 1023, -1023);
int motor_right_speed = 1023;
int motor_left_speed = 1023;
if (nJoyL < 0) {
motor_right_speed = motor_right_speed + nJoyL;
}
if (nJoyL > 0) {
motor_left_speed = motor_left_speed - nJoyL;
}
if(ps2x.Button(PSB_R2)) {
digitalWrite(MOTORA_1,LOW);
digitalWrite(MOTORA_2,HIGH);
digitalWrite(MOTORB_1,HIGH);
digitalWrite(MOTORB_2,LOW);
analogWrite(ENA, motor_right_speed);
analogWrite(ENB, motor_left_speed);
}
if(ps2x.Button(PSB_L2)) {
digitalWrite(MOTORA_1,HIGH);
digitalWrite(MOTORA_2,LOW);
digitalWrite(MOTORB_1,LOW);
digitalWrite(MOTORB_2,HIGH);
analogWrite(ENA, motor_right_speed);
analogWrite(ENB, motor_left_speed);
}
if(!ps2x.Button(PSB_L2) && !ps2x.Button(PSB_R2)) {
analogWrite(ENA, 0);
analogWrite(ENB, 0);
}
delay(50);
}
}
when I turn on the controller and the battery , I can see both the leds on the receiver and the controller stop blinking, indicating that they are connected.
when I try to use the joystick or any button, nothing, literally nothing happens. I don't know what is wrong and would appreciate any help
thanks
