Hi everybody, this is my first post on this forum. I am working on my first Arduino project where i want to control a ''RC'' car with a PS4 controller(very original I know). I am using an L298N H-bridge to control the speed of my motor with a PWM signal. However the problem I run into is that the speed seems to be either 100% or 0%. I have a feeling that it is because my Arduino is sending a new PWM signal to the H-bridge every time a signal comes in from the PS4controller, which makes the PWM signal start over at a very high frequency resulting in a 100% duty cycle. I have no clue how to fix this and am frankly not even sure if this is the problem. Any help/ advice would be greatly appreciated. I have pasted my code and a picture of the robot below:
#include <PS4BT.h>
#include <usbhub.h>
#include <Servo.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);
// After that you can simply create the instance like so and then press the PS button on the device
PS4BT PS4(&Btd);
bool printAngle, printTouch;
uint8_t oldL2Value, oldR2Value;
Servo servo;
int angle = 90;
uint8_t oldJoyValue;
void setup() {
pinMode(6, OUTPUT); //motor PWM
pinMode(5, OUTPUT); //motor direction
pinMode(4, OUTPUT); //motor direction
servo.attach(11);
servo.write(angle);
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
}
Serial.print(F("\r\nPS4 Bluetooth Library Started"));
}
void loop() {
// put your main code here, to run repeatedly:
Usb.Task();
if (PS4.connected()) {
if (abs(PS4.getAnalogButton(R2)-oldR2Value)> 5 && PS4.getAnalogButton(R2)> 5) {
oldR2Value = PS4.getAnalogButton(R2);
Serial.print(oldR2Value);
digitalWrite(6, oldR2Value);
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
}
else if (abs(PS4.getAnalogButton(L2)-oldL2Value)> 5 && PS4.getAnalogButton(L2)> 5) {
oldL2Value = PS4.getAnalogButton(L2);
Serial.print(oldL2Value);
digitalWrite(6, oldL2Value);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
}
else if (PS4.getAnalogButton(L2) < 5 && PS4.getAnalogButton(R2) < 5){
digitalWrite(6, 0);
}
}
if (PS4.getButtonClick(PS)) {
Serial.print(F("\r\nPS"));
PS4.disconnect();
}
}