Communication delay between android and HC-05 BT module

I'm working on my project in that im to controlling traffic light with android which are connect by HC-05 using Arduino Uno. HC-05 is connecting with android.
At first I have make single led on and off with android through Hc-05 it was working without delay.

I have build app with MIT App inventor.
Now i have made connection of 12 led with arduino and programmed it. The HC-05 was receiving data but there was of 25 sec ( when i pressed on from mobile the HC-05 was receiving it after 25 sec).
I thought HC-05 is getting low power due to 12 leds connected on arduino.

Then i commented 6 leds working and forgot to remove connection of that 6 leds then the delay reduced to 10 sec.
I figured out that as the code length increases the communication delay also increases.

I have allo tried to change the baud rate, but i was getting garbge values at 38400 baud rate so im now using 9600.

Please help me to reduce this delay in project.

HC -05 PINS >>> ARDUINO PINS

EN 3.3v
Vcc 5v
gnd gnd
RX TX
tx rx

######## Code ######

char Incoming_value = 0;

int red1 = 2;
int yel1 = 3;
int gre1 = 4;

int red2 = 5;
int yel2 = 6;
int gre2 = 7;

int red3 = 8;
int yel3 = 9;
int gre3 = 10;

int red4 = 11;
int yel4 = 12;
int gre4 = 13;


void setup() 
{
  pinMode(red1,OUTPUT);
  pinMode(yel1,OUTPUT);
  pinMode(gre1,OUTPUT);
  
  pinMode(red2,OUTPUT);
  pinMode(yel2,OUTPUT);
  pinMode(gre2,OUTPUT);
  
  pinMode(red3,OUTPUT);
  pinMode(yel3,OUTPUT);
  pinMode(gre3,OUTPUT);
  
  pinMode(red4,OUTPUT);
  pinMode(yel4,OUTPUT);
  pinMode(gre4,OUTPUT);
  
  Serial.begin(9600);         
       
}

///////////////////////////////////////////// ## TRAFFIC 1 ## ////////////////////////////////////////////////////

void TRAFFIC_1(){

  digitalWrite(red1, LOW);
  digitalWrite(yel1, LOW);
  digitalWrite(gre1, HIGH);
  
  digitalWrite(red2, HIGH);
  digitalWrite(yel2, LOW);
  digitalWrite(gre2, LOW);

  digitalWrite(red3, HIGH);
  digitalWrite(yel3, LOW);
  digitalWrite(gre3, LOW);
  
  digitalWrite(red4, HIGH);
  digitalWrite(yel4, LOW);
  digitalWrite(gre4, LOW);

  delay(5000);
  
  ////////////////////////

  
  digitalWrite(red1, LOW);
  digitalWrite(yel1, HIGH);
  digitalWrite(gre1, LOW);
  
  digitalWrite(red2, LOW);
  digitalWrite(yel2, HIGH);
  digitalWrite(gre2, LOW);

  digitalWrite(red3, HIGH);
  digitalWrite(yel3, LOW);
  digitalWrite(gre3, LOW);
  
  digitalWrite(red4, HIGH);
  digitalWrite(yel4, LOW);
  digitalWrite(gre4, LOW);
  
  delay(2000);
}

///////////////////////////////////////////// ## TRAFFIC 2 button ## ////////////////////////////////////////////////////

void TRAFFIC_2(){
    
  digitalWrite(red1, HIGH);
  digitalWrite(yel1, LOW);
  digitalWrite(gre1, LOW);
  
  digitalWrite(red2, LOW);
  digitalWrite(yel2, LOW);
  digitalWrite(gre2, HIGH);

  digitalWrite(red3, HIGH);
  digitalWrite(yel3, LOW);
  digitalWrite(gre3, LOW);
  
  digitalWrite(red4, HIGH);
  digitalWrite(yel4, LOW);
  digitalWrite(gre4, LOW);

  delay(5000);
  
  ////////////////////////

  
  digitalWrite(red1, HIGH);
  digitalWrite(yel1, LOW);
  digitalWrite(gre1, LOW);
  
  digitalWrite(red2, LOW);
  digitalWrite(yel2, HIGH);
  digitalWrite(gre2, LOW);

  digitalWrite(red3, LOW);
  digitalWrite(yel3, HIGH);
  digitalWrite(gre3, LOW);
  
  digitalWrite(red4, HIGH);
  digitalWrite(yel4, LOW);
  digitalWrite(gre4, LOW);
  
  delay(2000);
}

///////////////////////////////////////////// ## TRAFFIC 3 button ## ////////////////////////////////////////////////////

void TRAFFIC_3(){
    
  digitalWrite(red1, HIGH);
  digitalWrite(yel1, LOW);
  digitalWrite(gre1, LOW);
  
  digitalWrite(red2, HIGH);
  digitalWrite(yel2, LOW);
  digitalWrite(gre2, LOW);

  digitalWrite(red3, LOW);
  digitalWrite(yel3, LOW);
  digitalWrite(gre3, HIGH);
  
  digitalWrite(red4, HIGH);
  digitalWrite(yel4, LOW);
  digitalWrite(gre4, LOW);

  delay(5000);
  
  ////////////////////////

  
  digitalWrite(red1, HIGH);
  digitalWrite(yel1, LOW);
  digitalWrite(gre1, LOW);
  
  digitalWrite(red2, HIGH);
  digitalWrite(yel2, LOW);
  digitalWrite(gre2, LOW);

  digitalWrite(red3, LOW);
  digitalWrite(yel3, HIGH);
  digitalWrite(gre3, LOW);
  
  digitalWrite(red4, LOW);
  digitalWrite(yel4, HIGH);
  digitalWrite(gre4, LOW);
  
  delay(2000);
}

///////////////////////////////////////////// ## TRAFFIC 4 button ## ////////////////////////////////////////////////////

void TRAFFIC_4(){
    
  digitalWrite(red1, HIGH);
  digitalWrite(yel1, LOW);
  digitalWrite(gre1, LOW);
  
  digitalWrite(red2, HIGH);
  digitalWrite(yel2, LOW);
  digitalWrite(gre2, LOW);

  digitalWrite(red3, HIGH);
  digitalWrite(yel3, LOW);
  digitalWrite(gre3, LOW);
  
  digitalWrite(red4, LOW);
  digitalWrite(yel4, LOW);
  digitalWrite(gre4, HIGH);

  delay(10000);
  
  ////////////////////////

  
  digitalWrite(red1, LOW);
  digitalWrite(yel1, HIGH);
  digitalWrite(gre1, LOW);
  
  digitalWrite(red2, HIGH);
  digitalWrite(yel2, LOW);
  digitalWrite(gre2, LOW);

  digitalWrite(red3, HIGH);
  digitalWrite(yel3, LOW);
  digitalWrite(gre3, LOW);
  
  digitalWrite(red4, LOW);
  digitalWrite(yel4, HIGH);
  digitalWrite(gre4, LOW);
  
  delay(2000);

}

/////////////////////////////////////////////////// # START BUTTON # ///////////////////////////////////////////////////

void startbutton(){

  digitalWrite(red1, HIGH);
  digitalWrite(red2, HIGH);
  digitalWrite(red3, HIGH);
  digitalWrite(red4, HIGH);
  delay(300);

  digitalWrite(red1, LOW);
  digitalWrite(red2, LOW);
  digitalWrite(red3, LOW);
  digitalWrite(red4, LOW);
  delay(300);

   digitalWrite(red1, HIGH);
  digitalWrite(red2, HIGH);
  digitalWrite(red3, HIGH);
  digitalWrite(red4, HIGH);
  delay(20000);
}

/////////////////////////////////////////////////// Normal traffic working ///////////////////////////////////////////////////

void traffic_light(){

   TRAFFIC_1();
   TRAFFIC_2();
   TRAFFIC_3();
   TRAFFIC_4();
}

/////////////////////////////////////////////////// BT traffic working ///////////////////////////////////////////////////

void loop(){
  
 traffic_light(); 

if(Serial.available() > 0)  
  {
    Incoming_value = Serial.read();      
    Serial.print(Incoming_value);        
    Serial.print("\n");

//////////////////////////////// ## START button ## //////////////////////////////////////////    

  if(Incoming_value == '1'){             
       startbutton();
   }
 
//////////////////////////////// ## TRAFFIC 2 button ## //////////////////////////////////////////

  if(Incoming_value == '2'){             
       TRAFFIC_1();
   }

//////////////////////////////// ## TRAFFIC 3 button ## //////////////////////////////////////////

  if(Incoming_value == '3'){             
       TRAFFIC_2();
   }

//////////////////////////////// ## TRAFFIC 4 button ## //////////////////////////////////////////

  if(Incoming_value == '4'){             
       TRAFFIC_3();
   }

//////////////////////////////// ## TRAFFIC 5 button ## //////////////////////////////////////////

  if(Incoming_value == '5'){             
       TRAFFIC_4();
   }    
 
 }         
} 

CONNECTION IMAGE

wrong connections
there is no resistor on LEDs. pins0/1 are occupied by USB converter, it may cause problems.


int red1 = 2;
int yel1 = 3;
int gre1 = 4;

int red2 = 5;
int yel2 = 6;
int gre2 = 7;

int red3 = 8;
int yel3 = 9;
int gre3 = 10;

int red4 = 11;
int yel4 = 12;
int gre4 = 13;


void setup()
{
  pinMode(red1, OUTPUT);
  pinMode(yel1, OUTPUT);
  pinMode(gre1, OUTPUT);

  pinMode(red2, OUTPUT);
  pinMode(yel2, OUTPUT);
  pinMode(gre2, OUTPUT);

  pinMode(red3, OUTPUT);
  pinMode(yel3, OUTPUT);
  pinMode(gre3, OUTPUT);

  pinMode(red4, OUTPUT);
  pinMode(yel4, OUTPUT);
  pinMode(gre4, OUTPUT);

  Serial.begin(9600);

}

///////////////////////////////////////////// ## TRAFFIC 1 ## ////////////////////////////////////////////////////

void TRAFFIC_1() {

  digitalWrite(red1, LOW);
  digitalWrite(yel1, LOW);
  digitalWrite(gre1, HIGH);

  digitalWrite(red2, HIGH);
  digitalWrite(yel2, LOW);
  digitalWrite(gre2, LOW);

  digitalWrite(red3, HIGH);
  digitalWrite(yel3, LOW);
  digitalWrite(gre3, LOW);

  digitalWrite(red4, HIGH);
  digitalWrite(yel4, LOW);
  digitalWrite(gre4, LOW);

  delay(5000);

  ////////////////////////


  digitalWrite(red1, LOW);
  digitalWrite(yel1, HIGH);
  digitalWrite(gre1, LOW);

  digitalWrite(red2, LOW);
  digitalWrite(yel2, HIGH);
  digitalWrite(gre2, LOW);

  digitalWrite(red3, HIGH);
  digitalWrite(yel3, LOW);
  digitalWrite(gre3, LOW);

  digitalWrite(red4, HIGH);
  digitalWrite(yel4, LOW);
  digitalWrite(gre4, LOW);

  delay(2000);
}

///////////////////////////////////////////// ## TRAFFIC 2 button ## ////////////////////////////////////////////////////

void TRAFFIC_2() {

  digitalWrite(red1, HIGH);
  digitalWrite(yel1, LOW);
  digitalWrite(gre1, LOW);

  digitalWrite(red2, LOW);
  digitalWrite(yel2, LOW);
  digitalWrite(gre2, HIGH);

  digitalWrite(red3, HIGH);
  digitalWrite(yel3, LOW);
  digitalWrite(gre3, LOW);

  digitalWrite(red4, HIGH);
  digitalWrite(yel4, LOW);
  digitalWrite(gre4, LOW);

  delay(5000);

  ////////////////////////


  digitalWrite(red1, HIGH);
  digitalWrite(yel1, LOW);
  digitalWrite(gre1, LOW);

  digitalWrite(red2, LOW);
  digitalWrite(yel2, HIGH);
  digitalWrite(gre2, LOW);

  digitalWrite(red3, LOW);
  digitalWrite(yel3, HIGH);
  digitalWrite(gre3, LOW);

  digitalWrite(red4, HIGH);
  digitalWrite(yel4, LOW);
  digitalWrite(gre4, LOW);

  delay(2000);
}

///////////////////////////////////////////// ## TRAFFIC 3 button ## ////////////////////////////////////////////////////

void TRAFFIC_3() {

  digitalWrite(red1, HIGH);
  digitalWrite(yel1, LOW);
  digitalWrite(gre1, LOW);

  digitalWrite(red2, HIGH);
  digitalWrite(yel2, LOW);
  digitalWrite(gre2, LOW);

  digitalWrite(red3, LOW);
  digitalWrite(yel3, LOW);
  digitalWrite(gre3, HIGH);

  digitalWrite(red4, HIGH);
  digitalWrite(yel4, LOW);
  digitalWrite(gre4, LOW);

  delay(5000);

  ////////////////////////


  digitalWrite(red1, HIGH);
  digitalWrite(yel1, LOW);
  digitalWrite(gre1, LOW);

  digitalWrite(red2, HIGH);
  digitalWrite(yel2, LOW);
  digitalWrite(gre2, LOW);

  digitalWrite(red3, LOW);
  digitalWrite(yel3, HIGH);
  digitalWrite(gre3, LOW);

  digitalWrite(red4, LOW);
  digitalWrite(yel4, HIGH);
  digitalWrite(gre4, LOW);

  delay(2000);
}

///////////////////////////////////////////// ## TRAFFIC 4 button ## ////////////////////////////////////////////////////

void TRAFFIC_4() {

  digitalWrite(red1, HIGH);
  digitalWrite(yel1, LOW);
  digitalWrite(gre1, LOW);

  digitalWrite(red2, HIGH);
  digitalWrite(yel2, LOW);
  digitalWrite(gre2, LOW);

  digitalWrite(red3, HIGH);
  digitalWrite(yel3, LOW);
  digitalWrite(gre3, LOW);

  digitalWrite(red4, LOW);
  digitalWrite(yel4, LOW);
  digitalWrite(gre4, HIGH);

  delay(10000);

  ////////////////////////


  digitalWrite(red1, LOW);
  digitalWrite(yel1, HIGH);
  digitalWrite(gre1, LOW);

  digitalWrite(red2, HIGH);
  digitalWrite(yel2, LOW);
  digitalWrite(gre2, LOW);

  digitalWrite(red3, HIGH);
  digitalWrite(yel3, LOW);
  digitalWrite(gre3, LOW);

  digitalWrite(red4, LOW);
  digitalWrite(yel4, HIGH);
  digitalWrite(gre4, LOW);

  delay(2000);

}

/////////////////////////////////////////////////// # START BUTTON # ///////////////////////////////////////////////////

void startbutton() {

  digitalWrite(red1, HIGH);
  digitalWrite(red2, HIGH);
  digitalWrite(red3, HIGH);
  digitalWrite(red4, HIGH);
  delay(300);

  digitalWrite(red1, LOW);
  digitalWrite(red2, LOW);
  digitalWrite(red3, LOW);
  digitalWrite(red4, LOW);
  delay(300);

  digitalWrite(red1, HIGH);
  digitalWrite(red2, HIGH);
  digitalWrite(red3, HIGH);
  digitalWrite(red4, HIGH);
  delay(20000);
}

/////////////////////////////////////////////////// Normal traffic working ///////////////////////////////////////////////////

void traffic_light() {

  TRAFFIC_1();
  TRAFFIC_2();
  TRAFFIC_3();
  TRAFFIC_4();
}

/////////////////////////////////////////////////// BT traffic working ///////////////////////////////////////////////////

void loop() {
  traffic_light();
  if (Serial.available() > 0)  {
    char Incoming_value = Serial.read();
    if (Incoming_value.isAscii()) {
      Serial.write(Incoming_value);
      Serial.print("\n");
      switch (Incoming_value) {
        case '1':
          startbutton();
          break;
        case '2':
          TRAFFIC_1();
          break;
        case '3':
          TRAFFIC_2();
          break;
        case '4':
          TRAFFIC_3();
          break;
        case '5':
          TRAFFIC_4();
      }
    }
  }
}

my mistake i have attached wrong img

You use long delays in your code so you are not looking for input until those delays are complete.

additional i would rework pin assignment, 2D array.

const byte Pins[3][4]={2,3,4,5,6,7,8,9,10,11,12,13};

then Pins[0][0], Pins[0][1], Pins[0][2], Pins[0][3] are all red

Pins[0][0], Pins[1][0], Pins[2][0] are three LEDs of first block

register manipulation is recommendable too

so sir should I decrease delay

I don't know if connecting EN is the cause of your problem. I'm surprised that it worked at all.

ok I'll give it a try

If you want your sketch to be responsive to input you should eliminate delay() completely.

sir if there is no delay then all led will remain on or off

You need to learn to use a state machine design and millis() for scheduling. Here is a basic tutorial I wrote a while back:

The easiest way to insert a delay without delay() is to break the process into steps or 'states'. A state is any part of your sketch that is waiting for an event to happen (button press, operation completed, delay finished) and when the event happens, you go on to the next state. Since there are a limited number of states each process can be in, the process is known as a Finite State Machine.

For example: Let's say you wanted two LEDs to blink at the same time but at different speeds. You can't even do that easily with a simple delay():

void loop()
{
  digitalWrite(LED1, HIGH);
  delay(3000);
  digitalWrite(LED1, LOW);
  delay(2000);

  digitalWrite(LED2, HIGH);
  delay(1000);
  digitalWrite(LED2, LOW);
  delay(2500);
}

This will obviously not do what you want because the 'delay()' calls allow only one LED to be active at a time.

The way to make it work is to make each independent process a separate Finite State Machine. I like to use an 'enum' to give names to each state. In this case, the states are the same for both so we can get by with one 'enum' for both:

  enum
  {
    Idle, TurnOn, OnDelay, TurnOff, OffDelay
  } StateLED1 = TurnOn, StateLED2 = TurnOn;

Now you can see that each 'delay()' is spread across two states. The first state starts the delay timer and changes to the second state. The second state checks the timer and, if the timer has expired, goes on to the next state. The process repeats when the "OffDelay" state ends and goes to the "TurnOn" state.

The "Idle" state is there in case you want to turn off either or both blinkers.

const byte LED1 = 4;
const byte LED2 = 5;

enum
  {
    Idle, TurnOn, OnDelay, TurnOff, OffDelay
  } StateLED1 = TurnOn, StateLED2 = TurnOn;

void setup()
{
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
}

void loop()
{
  unsigned long currentMillis = millis();
  static unsigned long Delay1Start;
  static unsigned long Delay2Start;

  switch (StateLED1)
  {
    case Idle:
      digitalWrite(LED1, LOW);
      break;

    case TurnOn:
      digitalWrite(LED1, HIGH);
      Delay1Start = currentMillis;
      StateLED1 = OnDelay;
      break;

    case OnDelay:
      if (currentMillis - Delay1Start >= 3000)
        StateLED1 = TurnOff;
      break;

    case TurnOff:
      digitalWrite(LED1, LOW);
      Delay1Start = currentMillis;
      StateLED1 = OffDelay;
      break;

    case OffDelay:
      if (currentMillis - Delay1Start >= 2000)
        StateLED1 = TurnOn;
      break;
  }
  
  
  switch (StateLED2)
  {
    case Idle:
      digitalWrite(LED2, LOW);
      break;

    case TurnOn:
      digitalWrite(LED2, HIGH);
      Delay2Start = currentMillis;
      StateLED2 = OnDelay;
      break;

    case OnDelay:
      if (currentMillis - Delay2Start >= 1000)
        StateLED2 = TurnOff;
      break;

    case TurnOff:
      digitalWrite(LED2, LOW);
      Delay2Start = currentMillis;
      StateLED2 = OffDelay;
      break;

    case OffDelay:
      if (currentMillis - Delay2Start >= 2500)
        StateLED2 = TurnOn;
      break;
  }
}

And there we go. No delay(). No wait loops.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.