Im working on converting a old RC in to a BT car. I'm using a nano, l298n h bridge, hc-05 BT module and a relay for lights. I have it kinda working. Only thing I can't seem to figure out is this, I have it setup to continuously transmit when button on my phone is pressed and this works on the throttle but the steering doesn't come back to center (90°) when I let go of the button. I'm using the Bluetooth RC car app by andi co
This is my code. Any help would be highly appreciated.
#include <Servo.h>
#include <L298N.h>
#define ENA 3
#define IN1 5
#define IN2 6
#define SERVO_PIN 9
#define RELAY_PIN 8
#define LED_PIN 13
// Create a new instance of the L298N class
L298N motor(ENA, IN1, IN2);
// Create a Servo object
Servo steeringServo;
void setup() {
Serial.begin(38400); // Start the serial communication with the HC-05 module
pinMode(RELAY_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
// Attach the servo to the servo pin
steeringServo.attach(SERVO_PIN);
steeringServo.write(90); // Sets the servo to the initial position (centered at 90 degrees)
// Turn on the LED
digitalWrite(LED_PIN, HIGH);
}
void loop() {
// Check if data is available to read
if (Serial.available() > 0) {
// Read the incoming byte
char incomingByte = Serial.read();
// Check the incoming byte and perform the corresponding action
switch (incomingByte) {
case 'F': // Forward
motor.setSpeed(70); // Set the speed to 70
motor.forward(); // Move forward
break;
case 'B': // Backward
motor.setSpeed(70); // Set the speed to 70
motor.backward(); // Move backward
break;
case 'S': // Stop
motor.stop(); // Stop the motor
break;
case 'L': // Left
steeringServo.write(45);
break;
case 'R': // Right
steeringServo.write(135);
break;
case 'C': // Center
steeringServo.write(90);
break;
case 'O': // Relay on
digitalWrite(RELAY_PIN, HIGH);
break;
case 'P': // Relay off
digitalWrite(RELAY_PIN, LOW);
break;
}
}
}