Hi, I'm a newbie and I'm having a project using ps2 and arduino, but somehow I can only connect my ps2 to my arduino for 1 time. The problem is only the red LED on the ps2 controller that lights and 1 time, both LEDs are light up but they're not even blinking. I've tried different arduino board and different L298N, different wires and batteries but still not working any help?
Here's the code I use:
#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
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(9600);
error = ps2x.config_gamepad(12,11,10,13, 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_LY); // read left stick
int nJoyR = ps2x.Analog(PSS_RY); // read right stick
nJoyL = map(nJoyL, 0, 255, 1023, -1023);
nJoyR = map(nJoyR, 0, 255, -1023, 1023);
// Perform movements based on both analog sticks
if(nJoyR>50) {
digitalWrite(MOTORA_1,HIGH);
digitalWrite(MOTORA_2,LOW);
analogWrite(ENA, 1023);
}
if(nJoyR<-50) {
digitalWrite(MOTORA_1,LOW);
digitalWrite(MOTORA_2,HIGH);
analogWrite(ENA, 1023);
}
if (abs(nJoyR)<50) {
analogWrite(ENA, 0);
}
if(nJoyL>50) {
digitalWrite(MOTORB_1,HIGH);
digitalWrite(MOTORB_2,LOW);
analogWrite(ENB, 1023);
}
if(nJoyL<-50) {
digitalWrite(MOTORB_1,LOW);
digitalWrite(MOTORB_2,HIGH);
analogWrite(ENB, 1023);
}
if (abs(nJoyL)<50) {
analogWrite(ENB, 0);
}
delay(50);
}
}