Hello i have make an rc car controlled via bluetooth and i want your help with my code.I want to add in the code to Stop the car when connection lost or bluetooth disconnected.How to do that?Thanks for your help.
int const pwm_drive = 6;
int const dir_drive = 7;
int const pwm_steer = 5;
int const dir_steer = 4;
void setup() {
pinMode(pwm_drive, OUTPUT);
pinMode(pwm_steer, OUTPUT);
pinMode(dir_drive, OUTPUT);
pinMode(dir_steer, OUTPUT);
turnNone();
moveNone();
Serial.begin(9600);
}
void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
int incomingByte = Serial.read();
// action depending on the instruction
// as well as sending a confirmation back to the app
switch (incomingByte) {
case 'F':
moveForward(255);
Serial.println("Going forward");
break;
case 'R':
turnRight();
Serial.println("Turning right");
break;
case 'L':
turnLeft();
Serial.println("Turning left");
break;
case 'B':
moveBackward(255);
Serial.println("Going backwards");
break;
case 'N':
turnNone();
Serial.println("Turning centered");
break;
case 'S':
moveNone();
Serial.println("Stopping");
break;
default:
// if nothing matches, do nothing
break;
}
}
}
void moveForward(int speedBot) {
// turn the driving motor on to go forwards at set speed
digitalWrite(dir_drive, HIGH);
analogWrite(pwm_drive, speedBot);
}
void moveBackward(int speedBot) {
// turn the driving motor on to go backwards at set speed
digitalWrite(dir_drive, LOW);
analogWrite(pwm_drive, speedBot);
}
void moveNone() {
// turn the driving motor off
digitalWrite(dir_drive, LOW);
analogWrite(pwm_drive, 0);
}
void turnRight() {
// slam it to the right
digitalWrite(dir_steer, HIGH);
analogWrite(pwm_steer, 255);
}
void turnLeft() {
// slam it to the left
digitalWrite(dir_steer, LOW);
analogWrite(pwm_steer, 255);
}
void turnNone() {
// turn steering motor off - spring centering takes effect
digitalWrite(dir_steer, LOW);
analogWrite(pwm_steer, 0);
}