Arduino Uno Programming Question

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);
}

If there's no definite way to detect a loss of connection you'll need to move to using a constantly
refreshed command - if no command is received within say 1 second, you shutdown the car...

MarkT:
If there's no definite way to detect a loss of connection you'll need to move to using a constantly
refreshed command - if no command is received within say 1 second, you shutdown the car...

Can you help me, please?

Does the Bluetooth module you use have a State or DTR pin? If so, that pin should be high or low when connected, and the opposite state when disconnected. You can use that signal as input to an Arduino pin.

I have a system to control a model train. The code in loop() is like this

void loop() {
	currentMillis = millis();
	checkRadio();
	if (currentMillis - prevMsgMillis >= msgIntervalMillis) {
		zeroMotorPwr();
		ledOffInterval = ledLongInterval;
	}
        // etc

and in the function checkRadio() the value of prevMsgMillis is updated if a message is received. If no message is received within the msgintervalMillis the train is stopped.

You could substutute your function for receiving messages from Bluetooth for my checkRadio() function.

Note that my transmitter sends a message about 10 times per second even if the data has not changed.

...R

jack1992:
Can you help me, please?

You now have 6 commands: Forward, Right, Left, Backward, Neutral, Stop.
You can add one more command: Heartbeat.

When you receive a heartbeat, you mark the time.
In your loop() you can check how long ago you got your last heartbeat.
If this is longer than some timeout, then the connection is considered dead.

Of course, for this to work the controller must keep sending heartbeat commands periodically.

Jobi-Wan:
You can add one more command: Heartbeat.

I don't use an extra heartbeat command. I just send the current state of the controls every 100 millisecs even if they don't change.

...R

Thank you all for your help