I am trying to build an RC car controlled by a PS3 controller. so far I have this for the code.But it only spins in one direction. I am using a L293DNE H-bridge to control the motor spin direction.
help me please!
#include <PS3USB.h>
USB Usb;
PS3USB PS3(&Usb);
int EnablePin = 5;
int MotorPin1 = 4;
int MotorPin2 = 6;
int Forward;
int Backward;
//boolean printAngle;
//uint8_t state = 0;
void setup() {
pinMode(EnablePin, OUTPUT);
pinMode(MotorPin1, OUTPUT);
pinMode(MotorPin2, OUTPUT);
Serial.begin(115200);
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
while(1); //halt
}
Serial.print(F("\r\nPS3 USB Library Started"));
}
void loop() {
Usb.Task();
if(PS3.PS3Connected || PS3.PS3NavigationConnected) {
if(PS3.getAnalogHat(LeftHatY) > 135) {
Forward = map(PS3.getAnalogHat(LeftHatY), 135, 255, 0, 255);
analogWrite(EnablePin, Forward);
//digitalWrite(EnablePin, HIGH);
digitalWrite(MotorPin1, HIGH);
digitalWrite(MotorPin2, LOW);
}
if(PS3.getAnalogHat(LeftHatY) < 120) {
Backward = map(PS3.getAnalogHat(LeftHatY), 120, 0, 0, 255);
analogWrite(EnablePin, Backward);
digitalWrite(MotorPin1, LOW);
digitalWrite(MotorPin2, HIGH);
}
else{
analogWrite(EnablePin, 0);
digitalWrite(MotorPin1, LOW);
digitalWrite(MotorPin2, LOW);
}
}
}