felis / USB_Host_Shield_2.0

Revision 2.0 of USB Host Library for Arduino.
https://chome.nerpa.tech
1.8k stars 779 forks source link

getButtonClick not working PS4BT #347

Closed dragzor55 closed 6 years ago

dragzor55 commented 6 years ago

I am entering a robotics competition and need help with the "getButtonClick". We are using an Arduino to produce pwm signals to run the motor controllers. I have managed to get the back triggers to move the robot forward these use the "getAnalogButton". Yet when I try to use the L3 and R3 buttons on the controller w/ the "getButtonClick", nothing happened. To troubleshoot I've put "PS4.setLed(Red);" to the if statement. The LED did in fact turn red, but no pwm signal was made. Also, if someone could help me understand how to implement joysticks into the drive control of the robot, then that would be incredibly helpful.

#include <PS4BT.h>
#include <usbhub.h>

// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>

USB Usb;
//USBHub Hub1(&Usb); // Some dongles have a hub inside
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so

/* You can create the instance of the PS4BT class in two ways */
// This will start an inquiry and then pair with the PS4 controller - you only have to do this once
// You will need to hold down the PS and Share button at the same time, the PS4 controller will then start to blink rapidly indicating that it is in pairing mode
PS4BT PS4(&Btd, PAIR);

#define MotorpwmR1 6
#define MotorDirR1 7

#define MotorpwmL1 5
#define MotorDirL1 8

#define MotorpwmR2 3
#define MotorDirR2 2

#define MotorpwmL2 13
#define MotorDirL2 4

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
  }
  pinMode(MotorpwmR1, OUTPUT);
  pinMode(MotorDirR1, OUTPUT);

  pinMode(MotorpwmR2, OUTPUT);
  pinMode(MotorDirR2, OUTPUT);

  pinMode(MotorpwmL1, OUTPUT);
  pinMode(MotorDirL1, OUTPUT);

  pinMode(MotorpwmL2, OUTPUT);
  pinMode(MotorDirL2, OUTPUT);

}
void loop() {
  Usb.Task();

  analogWrite(MotorpwmR1, 0);
  analogWrite(MotorpwmR2, 0);
  analogWrite(MotorpwmL1, 0);
  analogWrite(MotorpwmL2, 0);

  if (PS4.getAnalogButton(L2)) {
    analogWrite(MotorpwmL1, 127);
    analogWrite(MotorDirL1, HIGH);
    analogWrite(MotorpwmL2, 127);
    analogWrite(MotorDirL2, HIGH);
  }

  if (PS4.getButtonClick(L1))   {
    analogWrite(MotorpwmL1, -127);
    analogWrite(MotorDirL1, LOW);
    analogWrite(MotorpwmL2, 127);
    analogWrite(MotorDirL2, LOW);
    PS4.setLed(Red);

  }
  if (PS4.getAnalogButton(R2)) {
    analogWrite(MotorpwmR1, 127);
    analogWrite(MotorDirR1, HIGH);
    analogWrite(MotorpwmR2, 127);
    analogWrite(MotorDirR2, HIGH);
  }
}
Lauszus commented 6 years ago

@dragzor55 first of all you should check if the PS4 controller is actually connected. The reason why your code does not work is because you are setting the PWM pins low in every loop. Also you probably want to use the getButtonPress function instead, as it will return true as long as the button is pressed.

Here is a minimal example:

void loop() {
  Usb.Task();

  if (PS4.connected()) {
    if (PS4.getButtonPress(L1)) {
      analogWrite(pwmPin, 127);
    } else {
      analogWrite(pwmPin, 0);
    }
  } else {
    analogWrite(pwmPin, 0);
  }
}