Problem of using Bluetooth module and Ultrasonic sensors

I have a problem with my project that is 4 wheels car interfaced by android application through a bluetooth module and contains an ultrasonic sensor for obstacles avoiding .. the problem is there is lagging between sending orders from the application and the car movement for a few seconds

this is the code that i've used :

int IN1=3; int IN2=4; int IN3=7; int IN4=8; int ENA=5; int ENB=6; int ABS=135; //HC-SR04 Sensor connection const int trigger = 11; const int echo = 12; float distance; unsigned long period;

void Forward (){ digitalWrite (ENA,HIGH); digitalWrite (ENB,HIGH); digitalWrite (IN1,LOW); digitalWrite (IN2,HIGH); digitalWrite (IN3,LOW); digitalWrite (IN4,HIGH); }

void Backward () { digitalWrite (ENA,HIGH); digitalWrite (ENB,HIGH); digitalWrite (IN1,HIGH); digitalWrite (IN2,LOW); digitalWrite (IN3,HIGH); digitalWrite (IN4,LOW); } void Stop (){ digitalWrite (ENA,LOW); digitalWrite (ENB,LOW);

digitalWrite (IN1,LOW); digitalWrite (IN2,LOW); digitalWrite (IN3,LOW); digitalWrite (IN4,LOW); } void Right () { analogWrite(ENA,ABS); analogWrite(ENB,ABS); digitalWrite(IN1,HIGH); digitalWrite(IN2,LOW); digitalWrite(IN3,LOW); digitalWrite(IN4,HIGH); } void Left (){ analogWrite(ENA,ABS); analogWrite(ENB,ABS); digitalWrite(IN1,LOW); digitalWrite(IN2,HIGH); digitalWrite(IN3,HIGH); digitalWrite(IN4,LOW); }

void bluetooth_motion() { byte Dir = Serial.read();

if (Dir=='1'){ Forward(); Serial.println("FORWARD"); } else if (Dir=='2'){ Backward(); Serial.println("BACKWARD"); } else if (Dir=='0'){ Stop(); Serial.println("STOP"); } else if (Dir=='4'){ Right(); Serial.println("RIGHT"); } else if (Dir=='3'){ Left(); Serial.println("LEFT"); } }

void setup() { // put your setup code here, to run once: pinMode (ENA,OUTPUT); pinMode (ENB,OUTPUT); pinMode (IN1,OUTPUT); pinMode (IN2,OUTPUT); pinMode (IN3,OUTPUT); pinMode (IN4,OUTPUT); pinMode(trigger, OUTPUT); pinMode(echo, INPUT); Serial.begin(9600); }

void loop() { digitalWrite(trigger,LOW); delayMicroseconds(3); digitalWrite(trigger,HIGH); delayMicroseconds(3); digitalWrite(trigger,LOW); period=pulseIn(echo,HIGH); //distance=(period/(2*10^6))*(340*100) distance = float(period)/58.8; Serial.println(distance); delay(3);

if (Serial.available()>0) { bluetooth_motion(); } if (distance <= 20) { Stop(); } }

I have only a brief knowledge about the IDE function "pulseln" so I am guessing telling that there might be a chance of time being spent. How often is that signal being measured coming, what frequency? The code looks like free from whiles and looping around delays. Unless You some kind of chain logic that sums up time I don't see anything pointing out.

pulseIn() is a "blocking function" and there is a timeout period if no echo is sensed. If you comment out the pulseIn(), is there still the lag in response?

If you disconnect the bluetooth and run the program from the serial monitor do you still see the lag in response?

nterfaced by android application

Can you tell us more about the app?

Specify the timeout for pulsIn; 10,000 microseconds should give a max distance of approx. 170 cm (just from memory). You can fill it in the formula that you use.

when i disconnect the bluetooth module and use serial monitor there's no lag it happens only when i connect the bluetooth module

when i disconnect the bluetooth module and use serial monitor there's no lag it happens only when i connect the bluetooth module

This says the problem is not with the code you have written. It's now time to tell us some more about the bluetooth module and the app.

OK … the bluetooth module is HC-05 type and the mobile application is Arduino BT joystick and it works with me without any problems when i don’t add ultrasonic sensor to it

it works with me without any problems when i don’t add ultrasonic sensor to it

This is what I understand you are indicating for the problem statement.

When using the Serial monitor for command input, (without bluetooth connected) but with the ultrasonic and pulseIn(), the program works for distance measurement and motor control without obvious latency.

When using the Arduino bluetooth joystick app for command input the program works for motor control without obvious latency when the ultrasonic and pulseIn() are not active.

When using the Arduino bluetooth joystick app for command input the program works for motor control with obvious latency when the ultrasonic and pulseIn() are active.

Here’s one idea to help narrow down the problem. It will help sort out any possible issue with the joystick app independent of the bluetooth interface.
If you do not use the bluetooth joystick app, but a simple bluetooth serial terminal app to send the commands, do you have the latency?

Can you provide a link to the Android app “Arduino Bluetooth Joystick” you are using.

i didn't use pulseln() i don't how to use it honstly .. but your conclusion is correct and here is the app : https://play.google.com/store/apps/details?id=com.heightdev.arduinobtjoysticklite&hl=eng

You do use pulseIn ! Read your loop() code line by line.

here is the app : https://play.google.com/store/apps/details?id=com.heightdev.arduinobtjoysticklite&hl=eng

I don't see where the free app has buttons to send text commands, and I'm not going to pay to debug this for you.

Try a different bluetooth serial terminal app.

If you indeed need the joystick, take a look at Joystick Bluetooth Commander which appears to be more widely used, and has a 62 page thread on the forum http://forum.arduino.cc/index.php?topic=173246.915

i didn't use pulseln() i don't how to use it honstly

This may be true, but your code uses the function

period=pulseIn(echo,HIGH);