Delay in Bluetooth

I am trying to make a Bluetooth controlled dustbin but when I combine the code of Bluetooth robot and automatic dustbin, there is delay in response when I press the button on my phone.

/* program by LOHITH KUMAR(Richu)
 * subscribe DREAM IDEAS for more updates
 */
#include <AFMotor.h>
#include <Servo.h>
Servo servo;
int triggerPin = A0; //triggering on pin 7
int echoPin = A1;    //echo on pin 8
int SERVOPIN = 6;


AF_DCMotor motor1(1, MOTOR12_1KHZ); 
AF_DCMotor motor2(2, MOTOR12_1KHZ); 
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);

char info; 

void setup() { //we will be combinig both setups from the codes
  
  Serial.begin(9600);  //we'll start serial comunication, so we can see the distance on the serial monitor
  
  pinMode(triggerPin, OUTPUT); //defining pins
  pinMode(echoPin, INPUT);
  servo.attach(SERVOPIN);
  
}

void loop(){
  bluetooth();
  sensor();
}


void bluetooth() {
  if(Serial.available() > 0){
    info = Serial.read();   
    Stop();
  }
  if(info == 'F'){
    forward();
    }
  else if(info == 'B'){
    back();
  }
  else if(info == 'R'){
    right();
  }
  else if(info == 'L'){
    left();
  }
}

void sensor() { 
  
  int duration, distance;
  
  digitalWrite(triggerPin, HIGH); 
  delay(2);
  digitalWrite(triggerPin, LOW);
  
  duration = pulseIn(echoPin, HIGH); 
  distance = (duration/2) / 29.1; 
  if(distance == 10){  
    servo.write(0);
    delay(2000); //so the stopping is visabl
    servo.write(180);
  }
}
void forward()
{
  motor1.setSpeed(255); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(255); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor clockwise
  motor3.setSpeed(255);//Define maximum velocity
  motor3.run(FORWARD); //rotate the motor clockwise
  motor4.setSpeed(255);//Define maximum velocity
  motor4.run(FORWARD); //rotate the motor clockwise
}

void back()
{
  motor1.setSpeed(255); //Define maximum velocity
  motor1.run(BACKWARD); //rotate the motor anti-clockwise
  motor2.setSpeed(255); //Define maximum velocity
  motor2.run(BACKWARD); //rotate the motor anti-clockwise
  motor3.setSpeed(255); //Define maximum velocity
  motor3.run(BACKWARD); //rotate the motor anti-clockwise
  motor4.setSpeed(255); //Define maximum velocity
  motor4.run(BACKWARD); //rotate the motor anti-clockwise
}

void left()
{
  motor1.setSpeed(255); //Define maximum velocity
  motor1.run(BACKWARD); //rotate the motor anti-clockwise
  motor2.setSpeed(255); //Define maximum velocity
  motor2.run(BACKWARD); //rotate the motor anti-clockwise
  motor3.setSpeed(255); //Define maximum velocity
  motor3.run(FORWARD);  //rotate the motor clockwise
  motor4.setSpeed(255); //Define maximum velocity
  motor4.run(FORWARD);  //rotate the motor clockwise
}

void right()
{
  motor1.setSpeed(255); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(255); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor clockwise
  motor3.setSpeed(255); //Define maximum velocity
  motor3.run(BACKWARD); //rotate the motor anti-clockwise
  motor4.setSpeed(255); //Define maximum velocity
  motor4.run(BACKWARD); //rotate the motor anti-clockwise
} 

void Stop()
{
  motor1.setSpeed(0); //Define minimum velocity
  motor1.run(RELEASE); //stop the motor when release the button
  motor2.setSpeed(0); //Define minimum velocity
  motor2.run(RELEASE); //rotate the motor clockwise
  motor3.setSpeed(0); //Define minimum velocity
  motor3.run(RELEASE); //stop the motor when release the button
  motor4.setSpeed(0); //Define minimum velocity
  motor4.run(RELEASE); //stop the motor when release the button
}

Your program sits here for 2 seconds and does nothing. You can't have that in your code if you want to be responsive to more bluetooth commands.

Look at the Blink Without Delay example in the IDE. You will also probably have to code this up as a state machine so you know what state you are in (moving to 0 and waiting or not) each time through loop().

Okay I will try removing the delay

I am trying to make bluetooth controlled automatic Dustbin for which I have to combine two codes but when i combine them the motors dont work instantly when the button is pressed instead there is a 5-10 seconds delay. I am using Bluetooth rc controller app. I've attached the code.

//Arduino Bluetooth Controlled Car
//Before uploading the code you have to install the necessary library
//Note - Disconnect the Bluetooth Module before hiting the upload button otherwise you'll get compilation error message.
//AFMotor Library https://learn.adafruit.com/adafruit-motor-shield/library-install 
//After downloading the library open Arduino IDE >> go to sketch >> Include Libray >> ADD. ZIP Libray >> Select the downloaded 
//ZIP File >> Open it >> Done
//Now You Can Upload the Code without any problem but make sure the bt module isn't connected with Arduino while uploading code

#include <AFMotor.h>
#include <Servo.h>
#include <NewPing.h>
Servo servo;
int echopin = 7;
int trigpin = 8;
int servopin = 10;

//initial motors pin
AF_DCMotor motor1(1, MOTOR12_1KHZ); 
AF_DCMotor motor2(2, MOTOR12_1KHZ); 
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);

char command; 

void setup() 
{       
  Serial.begin(9600);  //Set the baud rate to your Bluetooth module.
  servo.attach(servopin);
  pinMode(trigpin, OUTPUT);
  pinMode(echopin, INPUT);
  servo.write(180);
  servo.detach();
}

void loop(){
      long duration, distance;
  digitalWrite(trigpin, HIGH);
  delay(500);
  digitalWrite(trigpin, LOW);
  duration = pulseIn(echopin, HIGH);
  delay(10);

  if((duration<= 500))
  {
    servo.attach(servopin);
    servo.write(0);
    delay(3500);
    servo.write(180);

  }
  if(Serial.available() > 0){ 
    command = Serial.read(); 
    Stop(); //initialize with motors stoped
    //Change pin mode only if new command is different from previous.   
    //Serial.println(command);
    switch(command){
    case 'F':  
      forward();
      break;
    case 'B':  
       back();
      break;
    case 'L':  
      left();
      break;
    case 'R':
      right();
      break;
    }
  }
}

void forward()
{
  motor1.setSpeed(255); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(255); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor clockwise
  motor3.setSpeed(255);//Define maximum velocity
  motor3.run(FORWARD); //rotate the motor clockwise
  motor4.setSpeed(255);//Define maximum velocity
  motor4.run(FORWARD); //rotate the motor clockwise
}

void back()
{
  motor1.setSpeed(255); //Define maximum velocity
  motor1.run(BACKWARD); //rotate the motor anti-clockwise
  motor2.setSpeed(255); //Define maximum velocity
  motor2.run(BACKWARD); //rotate the motor anti-clockwise
  motor3.setSpeed(255); //Define maximum velocity
  motor3.run(BACKWARD); //rotate the motor anti-clockwise
  motor4.setSpeed(255); //Define maximum velocity
  motor4.run(BACKWARD); //rotate the motor anti-clockwise
}

void left()
{
  motor1.setSpeed(255); //Define maximum velocity
  motor1.run(BACKWARD); //rotate the motor anti-clockwise
  motor2.setSpeed(255); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor anti-clockwise
  motor3.setSpeed(255); //Define maximum velocity
  motor3.run(BACKWARD);  //rotate the motor clockwise
  motor4.setSpeed(255); //Define maximum velocity
  motor4.run(FORWARD);  //rotate the motor clockwise
}

void right()
{
  motor1.setSpeed(255); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(255); //Define maximum velocity
  motor2.run(BACKWARD); //rotate the motor clockwise
  motor3.setSpeed(255); //Define maximum velocity
  motor3.run(FORWARD); //rotate the motor anti-clockwise
  motor4.setSpeed(255); //Define maximum velocity
  motor4.run(BACKWARD); //rotate the motor anti-clockwise
} 

void Stop()
{
  motor1.setSpeed(0); //Define minimum velocity
  motor1.run(RELEASE); //stop the motor when release the button
  motor2.setSpeed(0); //Define minimum velocity
  motor2.run(RELEASE); //rotate the motor clockwise
  motor3.setSpeed(0); //Define minimum velocity
  motor3.run(RELEASE); //stop the motor when release the button
  motor4.setSpeed(0); //Define minimum velocity
  motor4.run(RELEASE); //stop the motor when release the button
}

Avoid the usage of the delay() function.

  new 1 (3 hits)
	Line  38:   delay(500);
	Line  41:   delay(10);
	Line  47:     delay(3500);

the 3rd delay is necessary for the dustbin lid to stay open for a while. I will remove other delay and try

I removed the delays and tried but its still not working

It is up to you to debug.
You may insert some serial.println() functions at POI to see what happens.

May I know what s POI?

point of interrest

Ok thanks

hello

does ultrasonic sensor not work with bluetooth module hc-06?

now it is kind of working but after the servo motor returns to its position, for some time the commands I send through the app is delayed.

/* program by LOHITH KUMAR(Richu)
 * subscribe DREAM IDEAS for more updates
 */
#include <AFMotor.h>
#include <Servo.h>
Servo servo;
int triggerPin = A0; //triggering on pin 7
int echoPin = A1;    //echo on pin 8
int SERVOPIN = 6;


AF_DCMotor motor1(1, MOTOR12_1KHZ); 
AF_DCMotor motor2(2, MOTOR12_1KHZ); 
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);

char info; 

void setup() { //we will be combinig both setups from the codes
  
  Serial.begin(9600);  //we'll start serial comunication, so we can see the distance on the serial monitor
  
  pinMode(triggerPin, OUTPUT); //defining pins
  pinMode(echoPin, INPUT);
  servo.attach(SERVOPIN);
  
}

void loop(){
  bluetooth();
  sensor();
}


void bluetooth() {
  if(Serial.available() > 0){
    info = Serial.read();   
    Stop();
  }
  if(info == 'F'){
    forward();
    }
  else if(info == 'B'){
    back();
  }
  else if(info == 'R'){
    right();
  }
  else if(info == 'L'){
    left();
  }
}

void sensor() { 
  
  int duration, distance;
  
  digitalWrite(triggerPin, HIGH); 
  delay(2);
  digitalWrite(triggerPin, LOW);
  
  duration = pulseIn(echoPin, HIGH); 
  distance = (duration/2) / 29.1; 
  if(distance == 10){  
    servo.write(0);
    delay(2000); //so the stopping is visabl
    servo.write(180);
  }
}
void forward()
{
  motor1.setSpeed(255); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(255); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor clockwise
  motor3.setSpeed(255);//Define maximum velocity
  motor3.run(FORWARD); //rotate the motor clockwise
  motor4.setSpeed(255);//Define maximum velocity
  motor4.run(FORWARD); //rotate the motor clockwise
}

void back()
{
  motor1.setSpeed(255); //Define maximum velocity
  motor1.run(BACKWARD); //rotate the motor anti-clockwise
  motor2.setSpeed(255); //Define maximum velocity
  motor2.run(BACKWARD); //rotate the motor anti-clockwise
  motor3.setSpeed(255); //Define maximum velocity
  motor3.run(BACKWARD); //rotate the motor anti-clockwise
  motor4.setSpeed(255); //Define maximum velocity
  motor4.run(BACKWARD); //rotate the motor anti-clockwise
}

void left()
{
  motor1.setSpeed(255); //Define maximum velocity
  motor1.run(BACKWARD); //rotate the motor anti-clockwise
  motor2.setSpeed(255); //Define maximum velocity
  motor2.run(BACKWARD); //rotate the motor anti-clockwise
  motor3.setSpeed(255); //Define maximum velocity
  motor3.run(FORWARD);  //rotate the motor clockwise
  motor4.setSpeed(255); //Define maximum velocity
  motor4.run(FORWARD);  //rotate the motor clockwise
}

void right()
{
  motor1.setSpeed(255); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(255); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor clockwise
  motor3.setSpeed(255); //Define maximum velocity
  motor3.run(BACKWARD); //rotate the motor anti-clockwise
  motor4.setSpeed(255); //Define maximum velocity
  motor4.run(BACKWARD); //rotate the motor anti-clockwise
} 

void Stop()
{
  motor1.setSpeed(0); //Define minimum velocity
  motor1.run(RELEASE); //stop the motor when release the button
  motor2.setSpeed(0); //Define minimum velocity
  motor2.run(RELEASE); //rotate the motor clockwise
  motor3.setSpeed(0); //Define minimum velocity
  motor3.run(RELEASE); //stop the motor when release the button
  motor4.setSpeed(0); //Define minimum velocity
  motor4.run(RELEASE); //stop the motor when release the button
}

This is the code. I don't think it is because of the delay's.

Your two topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

Okay I am sorry. I'll read it

It's fixed. I'll attach the code for anyone who needs it in future.

#include <AFMotor.h>
#include <Servo.h>
Servo servo;
int triggerPin = A0;
int echoPin = A1;
int SERVOPIN = 7;

AF_DCMotor motor1(1, MOTOR12_1KHZ); 
AF_DCMotor motor2(2, MOTOR12_1KHZ); 
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);

char info; 

void setup() {
  Serial.begin(9600);
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
  servo.attach(SERVOPIN);
  servo.write(180);
}

void loop(){
  bluetooth();
  sensor();
}

void bluetooth() {
  if(Serial.available() > 0){
    info = Serial.read();   
    Stop();
  }
  if(info == 'F'){
    forward();
    }
  else if(info == 'B'){
    back();
  }
  else if(info == 'R'){
    right();
  }
  else if(info == 'L'){
    left();
  }
}

void sensor() { 
  int duration, distance;
  digitalWrite(triggerPin, HIGH); 
  delay(2);
  digitalWrite(triggerPin, LOW);
  duration = pulseIn(echoPin, HIGH); 
  distance = (duration/2) / 29.1;
  Serial.println(distance);
  if(distance == 10){  
    servo.write(0);
    delay(3500);
    servo.write(180);
  }
}
void forward()
{
  motor1.setSpeed(255);
  motor1.run(FORWARD);
  motor2.setSpeed(255);
  motor2.run(FORWARD);
  motor3.setSpeed(255);
  motor3.run(FORWARD);
  motor4.setSpeed(255);
  motor4.run(FORWARD);
}

void back()
{
  motor1.setSpeed(255);
  motor1.run(BACKWARD);
  motor2.setSpeed(255);
  motor2.run(BACKWARD);
  motor3.setSpeed(255);
  motor3.run(BACKWARD);
  motor4.setSpeed(255);
  motor4.run(BACKWARD);
}

void left()
{
  motor1.setSpeed(255);
  motor1.run(BACKWARD);
  motor2.setSpeed(255);
  motor2.run(BACKWARD);
  motor3.setSpeed(255);
  motor3.run(FORWARD);
  motor4.setSpeed(255);
  motor4.run(FORWARD);
}

void right()
{
  motor1.setSpeed(255);
  motor1.run(FORWARD);
  motor2.setSpeed(255);
  motor2.run(FORWARD);
  motor3.setSpeed(255);
  motor3.run(BACKWARD);
  motor4.setSpeed(255);
  motor4.run(BACKWARD);
} 

void Stop()
{
  motor1.setSpeed(0);
  motor1.run(RELEASE);
  motor2.setSpeed(0);
  motor2.run(RELEASE);
  motor3.setSpeed(0);
  motor3.run(RELEASE);
  motor4.setSpeed(0);
  motor4.run(RELEASE);
}

3.5 seconds? Are you sure?

that's for the dustbin lid to be open. After 3.5 seconds it will close.