I have a problem with controlling an Arduino car via bluetooth application in mit app inventor. If I press the button, I have to wait a second or two for the car to move, and when I want to stop it, it also takes some time. Can you advise me where the problem is?
Update :
I tried it first with if but I changed it to switch and the response decreased. I also reconnected the bluethooth module to the arduino and it also reduced, but there is still a 2 second response.
Code :
```
#include <AFMotor.h>
#include <SoftwareSerial.h>
SoftwareSerial BTSerial (0,1); // Rx a Tx
AF_DCMotor Motor_1(1);
AF_DCMotor Motor_2(2);
AF_DCMotor Motor_3(3);
AF_DCMotor Motor_4(4);
int fast = 250; //M4
const int ledPin = 7;
void setup() {
BTSerial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
if (BTSerial.available()) {
char sign = BTSerial.read ();
//F,B,L,R,S,U,D,A,C,E,G,H,I
switch(sign) {
case 'F':
forward_();
break;
case 'B':
backward_();
break;
case 'L':
left_();
break;
case 'R':
right_();
break;
case 'A':
high_left();
break;
case 'C':
high_right();
break;
case 'E':
down_left();
break;
case 'G' :
down_right();
break;
case 'H':
turn_right();
break;
case 'I':
turn_left();
break;
case 'S':
stopping();
break;
case 'U':
digitalWrite(ledPin, HIGH);
break;
case 'D' :
digitalWrite(ledPin, LOW);
break;
default:
stopping();
break;
}
}
}
void forward_ () {
Motor_1.setSpeed(fast);
Motor_1.run(FORWARD);
Motor_2.setSpeed(fast);
Motor_2.run(FORWARD);
Motor_3.setSpeed(fast);
Motor_3.run(FORWARD);
Motor_4.setSpeed(fast);
Motor_4.run(FORWARD);
}
void backward_ () {
Motor_1.setSpeed(fast);
Motor_1.run(BACKWARD);
Motor_2.setSpeed(fast);
Motor_2.run(BACKWARD);
Motor_3.setSpeed(fast);
Motor_3.run(BACKWARD);
Motor_4.setSpeed(fast);
Motor_4.run(BACKWARD);
}
void left_ () {
Motor_1.setSpeed(fast);
Motor_1.run(FORWARD);
Motor_2.setSpeed(fast);
Motor_2.run(BACKWARD);
Motor_3.setSpeed(fast);
Motor_3.run(FORWARD);
Motor_4.setSpeed(fast);
Motor_4.run(BACKWARD);
}
void right_ () {
Motor_1.setSpeed(fast);
Motor_1.run(BACKWARD);
Motor_2.setSpeed(fast);
Motor_2.run(FORWARD);
Motor_3.setSpeed(fast);
Motor_3.run(BACKWARD);
Motor_4.setSpeed(fast);
Motor_4.run(FORWARD);
}
void high_left () {
Motor_1.setSpeed(fast);
Motor_1.run(FORWARD);
Motor_3.setSpeed(fast);
Motor_3.run(FORWARD);
}
void high_right () {
Motor_2.setSpeed(fast);
Motor_2.run(FORWARD);
Motor_4.setSpeed(fast);
Motor_4.run(FORWARD);
}
void down_left () {
Motor_2.setSpeed(fast);
Motor_2.run(BACKWARD);
Motor_4.setSpeed(fast);
Motor_4.run(BACKWARD);
}
void down_right () {
Motor_1.setSpeed(fast);
Motor_1.run(BACKWARD);
Motor_3.setSpeed(fast);
Motor_3.run(BACKWARD);
}
void stopping () {
Motor_1.setSpeed(0);
Motor_1.run(RELEASE);
Motor_2.setSpeed(0);
Motor_2.run(RELEASE);
Motor_3.setSpeed(0);
Motor_3.run(RELEASE);
Motor_4.setSpeed(0);
Motor_4.run(RELEASE);
}
void turn_right() {
Motor_1.setSpeed(fast);
Motor_1.run(FORWARD);
Motor_2.setSpeed(fast);
Motor_2.run(FORWARD);
Motor_3.setSpeed(fast);
Motor_3.run(BACKWARD);
Motor_4.setSpeed(fast);
Motor_4.run(BACKWARD);
}
void turn_left() {
Motor_1.setSpeed(fast);
Motor_1.run(BACKWARD);
Motor_2.setSpeed(fast);
Motor_2.run(BACKWARD);
Motor_3.setSpeed(fast);
Motor_3.run(FORWARD);
Motor_4.setSpeed(fast);
Motor_4.run(FORWARD);
}
```