I am making an RC car on Arduino Uno. I have connected an L298N motor driver to an Arduino Uno to control 2 DC motors. I also connected an HC-06 to the Uno to receive commands from an Android smartphone. Everything is working fine. But if a connection is lost (because of low battery or disconnected on Android application), when the RC car is moving it is not stopping. It stops only when there is connection and "s"(stop) command is received via Bluetooth.
int in1 = 4;
int in2 = 5;
int in3 = 6;
int in4 = 7;
void setup() {
Serial.begin(9600);
pinMode (in1, OUTPUT);
pinMode (in2, OUTPUT);
pinMode (in3, OUTPUT);
pinMode (in4, OUTPUT);
}
void loop() {
if (Serial.available()) {
int command = Serial.read();
move(command);
}
}
void move(int command) {
if (command == 'f') forward();
else if (command == 'b') backward();
else if (command == 'l') left();
else if (command == 'L') leftback();
else if (command == 'r') right();
else if (command == 'R') rightback();
else if (command == 's') stop();
}
Now what I want is to identify whether there is connection or it is lost. For example:
if(Serial.connectionLost()) stop();
Is there such a function to identify the connection state? If not, is it possible to implement this function in another way?