Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Response problem wi...
 
Share:
Notifications
Clear all

[Solved] Response problem with bluetooth

2 Posts
2 Users
0 Likes
660 Views
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

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

}
```


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2678
 

Hi there!

My best guess would be that the Bluetooth code is holding it up. (and yes: using switch instead of if can have an impact indeed as well)

I'd try doing the code without motor control first. Meaning: just reading Bluetooth values, to see how "fast" this works.

Test1: So you could change the code to send the char to the Serial monitor first.
Test2: Then test again with the switch statement where you you print char to the Serial monitor.

If all still works fast enough then we get to the next challenge and that is that motor movement takes up too much time.
Since the Arduino does not support multitasking, this may be your real problem.

Tip: The ESP32 and ESP8266 modules are cheaper, faster! Also ... those have 2 CPU cores, so you could make use of that (but this would be much more advanced).

 

Test 1:

void setup() {
  BTSerial.begin(9600);
  pinMode(ledPin, OUTPUT);
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {

  if (BTSerial.available()) {
    char sign = BTSerial.read ();
    //F,B,L,R,S,U,D,A,C,E,G,H,I

  Serial.println(sign);
}

 

Test 2:

void setup() {
  BTSerial.begin(9600);
  pinMode(ledPin, OUTPUT);
  // put your setup code here, to run once:
  Serial.begin(9600);
}

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':
      Serial.println(sign);
      break;
    case 'B':
      Serial.println(sign);
      break;
    case 'L':
      Serial.println(sign);
      break;
    case 'R':
  
    // etc etc 
  
    default:
      Serial.println(sign);
      break;
  }
  
}

Hope this helps ... 😊 


   
ReplyQuote
Share: