Electronic longboard bluetooth control

Hello,

I am trying to make a remote control for my electronic longboard using a bluetooth module HC06, and app inventor. I success in making a code, however it is not secured, if I loose the bluetooth connexion the skate will continue to run. That is why I tried other things. I change the app, and ask it to send every second the value of the speed, and made this code :

#include <SoftwareSerial.h>
#include <Servo.h>

#define bluetoothTx 10
#define bluetoothRx 11
SoftwareSerial HC06(bluetoothTx,bluetoothRx);
Servo mot;
int t1 = 0;
int t2;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  HC06.begin(9600);
  mot.attach(6);
  mot.write(0);
  

}

void loop() {
  // put your main code here, to run repeatedly:
  if (HC06.available()>0)
  {
    t1 = millis();
    Serial.println( 5 * HC06.read());
    mot.write( 5 * HC06.read());
    delay(1005); //motor will continue to run for a second until a new data
    t2 = millis();
    //Serial.println ( t2 - t1);
    
  }
  else 
  {
    mot.write(0);
  }

The speed of the motor doesn’t even math what I have ordered him.
Does anyone have an idea why it doesn’t work ? Or how I can fix it ?

It is a very poor idea to use delay() in a program where a quick response is essential because it blocks the Arduino until it completes. Use millis() to manage timing as illustrated in Several Things at a Time.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

For a project such as yours it would be a good idea to send a message at regular intervals (say 5 or 10 times per second) even if the data does not change. Then if a few messages fail to arrive the receiver will know there is a communication failure and can turn off the motor.

...R