Hi I'm trying to get a code to work for uni to control some 12 V motors. We have a usb host shield 2.0, a board and the Arduino. When we run my code the motor turns on however it does not respond to any of the commands i've set in the code to the buttons on the PS4. Does anyone have any ideas why this code may not be working? I would appreciate some help so much. The 4 motors are meant to run in motor pairs, being able to go clockwise, anticlockwise and off.
#include <PS4USB.h>
// Define motor pins
const int motor1A = 10;
const int motor1B = 11;
const int motor2A = 4;
const int motor2B = 5;
const int motor3A = 6;
const int motor3B = 7;
const int motor4A = 8;
const int motor4B = 9;
USB Usb;
PS4USB PS4(&Usb);
#include <PS4USB.h>
// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>
bool printAngle, printTouch;
uint8_t oldL2Value, oldR2Value;
void setup() {
Serial.begin(115200);
#if !defined(MIPSEL)
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
while (1); // Halt
}
// Initialize motor pins
pinMode(motor1A, OUTPUT);
pinMode(motor1B, OUTPUT);
pinMode(motor2A, OUTPUT);
pinMode(motor2B, OUTPUT);
pinMode(motor3A, OUTPUT);
pinMode(motor3B, OUTPUT);
pinMode(motor4A, OUTPUT);
pinMode(motor4B, OUTPUT);
Serial.print(F("\r\nPS4 USB Library Started"));
}
void loop() {
Usb.Task();
if (PS4.connected()) {
// Motor control logic
if (PS4.getButtonClick(RIGHT)) { // clockwise
Serial.println("right");
digitalWrite(motor1A, HIGH);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, HIGH);
digitalWrite(motor2B, LOW);
} else if (PS4.getButtonClick(LEFT)) { // anticlockwise
Serial.println("left");
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, HIGH);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, HIGH);
} else if (PS4.getButtonClick(DOWN)) { // stop
Serial.println("down");
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, LOW);
delay(1000);
}
if (PS4.getButtonClick(CIRCLE)) { // clockwise
Serial.println("circle");
digitalWrite(motor3A, HIGH);
digitalWrite(motor3B, LOW);
digitalWrite(motor4A, HIGH);
digitalWrite(motor4B, LOW);
} else if (PS4.getButtonClick(SQUARE)) { // anticlockwise
Serial.println("square");
digitalWrite(motor3A, LOW);
digitalWrite(motor3B, HIGH);
digitalWrite(motor4A, LOW);
digitalWrite(motor4B, HIGH);
} else if (PS4.getButtonClick(CROSS)) { // stop
Serial.println("cross");
digitalWrite(motor3A, LOW);
digitalWrite(motor3B, LOW);
digitalWrite(motor4A, LOW);
digitalWrite(motor4B, LOW);
}
}
}