Exit the if statement in the moment that the user press the push button.

I have 4 buttons for 4 LED which will work when the user press any of the buttons If the user press button A then LED A will work and so on for other LEDs and buttons, but when the user doesn’t press any button another LED(blue LED) will work for 5 seconds then off for 1-second until the user press a button then I want the LED that the user press it to work immediately.
My problem is that when the blue light is high and the user press the button its not change to the LED that the user press it button immediately, but its wait until the 5 seconds finish then change to the wanted LED.
So how can I do this?
My code is attached.

rec_2.ino (2 KB)

Welcome,

Code reindented:

#include <VirtualWire.h>

void setup()
{
  vw_set_ptt_inverted(true); // Required for DR3100
  vw_set_rx_pin(12);
  vw_setup(4000); // Bits per sec
  Serial.begin(9600);
  //A
  pinMode(0, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  //B
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  //C
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  //D
  pinMode(11, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(A0, OUTPUT);
  
  pinMode(A1, OUTPUT);//Blue LED
  
  vw_rx_start(); // Start the receiver PLL running
}

void loop()
{
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  Serial.println();
  
  
  if (vw_get_message(buf, &buflen)) // Non-blocking
  
  {
    //the code that will work when the user press any button
    if(buf[0]=='A')
    {
      
      digitalWrite(4,HIGH);
      digitalWrite(7,HIGH);
      digitalWrite(11,HIGH);
      
      digitalWrite(0,HIGH);
      delay(1000);
      digitalWrite(0,LOW);
      digitalWrite(2,HIGH);
      delay(1000);
      digitalWrite(2,LOW);
      digitalWrite(3,HIGH);
      delay(5000);
      digitalWrite(3,LOW);
      
    }
    else if(buf[0]=='B')
    {
      
      digitalWrite(0,HIGH);
      digitalWrite(7,HIGH);
      digitalWrite(11,HIGH);
      
      digitalWrite(4,HIGH);
      delay(1000);
      digitalWrite(4,LOW);
      digitalWrite(5,HIGH);
      delay(1000);
      digitalWrite(5,LOW);
      digitalWrite(6,HIGH);
      delay(5000);
      digitalWrite(6,LOW);
    }
    else if(buf[0]=='C')
    {
      
      digitalWrite(0,HIGH);
      digitalWrite(4,HIGH);
      digitalWrite(11,HIGH);
      
      digitalWrite(7,HIGH);
      delay(1000);
      digitalWrite(7,LOW);
      digitalWrite(8,HIGH);
      delay(1000);
      digitalWrite(8,LOW);
      digitalWrite(9,HIGH);
      delay(5000);
      digitalWrite(9,LOW);
    }
    else if(buf[0]=='D')
    {
      
      digitalWrite(0,HIGH);
      digitalWrite(4,HIGH);
      digitalWrite(7,HIGH);
      
      digitalWrite(11,HIGH);
      delay(1000);
      digitalWrite(11,LOW);
      digitalWrite(13,HIGH);
      delay(1000);
      digitalWrite(13,LOW);
      digitalWrite(A0,HIGH);
      delay(5000);
      digitalWrite(A0,LOW);
    }
  }
  else
  {
    //the blue LED wich will work normally until the user press any button
    digitalWrite(A1,HIGH);
    delay(5000);
    digitalWrite(A1,LOW);
    delay(1000);
  }
}

Your problem is that you use delay, which is a blocking function (it will block your code until time is elapsed).

You have to use the method demonstrated in the example “Blink Without Delay”.

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