How to get rid of this blocking delay

I have written simple code. I want the 4 relay to turn on/off independently when switch is pressed first time

Relay is active low and switch is connected with pull up resistor, initially all relays are active, relay should goes off / on after pressing button

How to get rid of this blocking delay ?


const int Switch_Pin1 = 4;     
const int Switch_Pin2 = 5;     
const int Switch_Pin3 = 6;    
const int Switch_Pin4 = 7;     

const int Relay_Pin1 =  8;     
const int Relay_Pin2 =  9;     
const int Relay_Pin3 =  10;    
const int Relay_Pin4 =  12;     

void setup()
{ 
  // begin serial port with baud rate 9600bps 
  Serial.begin(9600);               
 

  pinMode(Switch_Pin1, INPUT);
  pinMode(Switch_Pin2, INPUT);
  pinMode(Switch_Pin3, INPUT);
  pinMode(Switch_Pin4, INPUT);

  pinMode(Relay_Pin1, OUTPUT);
  pinMode(Relay_Pin2, OUTPUT);
  pinMode(Relay_Pin3, INPUT);
  pinMode(Relay_Pin4, INPUT);

  digitalWrite(Relay_Pin1, LOW); 
  digitalWrite(Relay_Pin2, LOW); 
  digitalWrite(Relay_Pin3, LOW); 
  digitalWrite(Relay_Pin4, LOW); 
 
}

void loop()
{  

int  Switch_State1 = digitalRead(Switch_Pin1);
int  Switch_State2 = digitalRead(Switch_Pin2);  
int  Switch_State3 = digitalRead(Switch_Pin3);
int  Switch_State4 = digitalRead(Switch_Pin4);  

  Serial.println(Switch_State1);
  Serial1.println(Switch_State2);
  Serial2.println(Switch_State3);
  Serial3.println(Switch_State4);

  if(Switch_State1 == LOW ){ 
     digitalWrite(Relay_Pin1, HIGH);   
     delay(4000); 
     digitalWrite(Relay_Pin1, LOW);
  } 

  if(Switch_State2 == LOW ){
     digitalWrite(Relay_Pin2, HIGH);   
     delay(4000); 
     digitalWrite(Relay_Pin2, LOW);
  } 

  if(Switch_State3 == LOW ){
     digitalWrite(Relay_Pin3, HIGH);   
     delay(4000); 
     digitalWrite(Relay_Pin3, LOW);
  } 

 if(Switch_State4 == LOW ){
     digitalWrite(Relay_Pin4, HIGH);   
     delay(4000); 
     digitalWrite(Relay_Pin4, LOW);
  } 
}

Example-code for timing based on millis()

BlinkWithoutDelay

I don't understand what to do next ?


const int Switch_Pin1 = 4;     
const int Switch_Pin2 = 5;     
const int Switch_Pin3 = 6;    
const int Switch_Pin4 = 7;     

const int Relay_Pin1 =  8;     
const int Relay_Pin2 =  9;     
const int Relay_Pin3 =  10;    
const int Relay_Pin4 =  12;   

// constants won't change:
const long interval = 4000;             

void setup()
{ 
  // begin serial port with baud rate 9600bps 
  Serial.begin(9600);               
 
 // configure inputs pins for switche's
  pinMode(Switch_Pin1, INPUT);
  pinMode(Switch_Pin2, INPUT);
  pinMode(Switch_Pin3, INPUT);
  pinMode(Switch_Pin4, INPUT);

  pinMode(Relay_Pin1, OUTPUT);
  pinMode(Relay_Pin2, OUTPUT);
  pinMode(Relay_Pin3, INPUT);
  pinMode(Relay_Pin4, INPUT);

  digitalWrite(Relay_Pin1, LOW); 
  digitalWrite(Relay_Pin2, LOW); 
  digitalWrite(Relay_Pin3, LOW); 
  digitalWrite(Relay_Pin4, LOW); 
 
}

void loop()
{  
  unsigned long currentMillis = millis();
  
int  Switch_State1 = digitalRead(Switch_Pin1);
int  Switch_State2 = digitalRead(Switch_Pin2);  
int  Switch_State3 = digitalRead(Switch_Pin3);
int  Switch_State4 = digitalRead(Switch_Pin4);  

  Serial.println(Switch_State1);
  Serial1.println(Switch_State2);
  Serial2.println(Switch_State3);
  Serial3.println(Switch_State4);

  if(Switch_State1 == LOW ){ 
     if (currentMillis - previousMillis >= interval) {
         previousMillis = currentMillis;

  } 
  
}

Let's clear up a couple of things first:

  1. Why do you have Relay_Pin3 and Relay_Pin4 defined as inputs?
  2. How are your switches wired? Do you have external pullup resistors?

You will need 4 (four) different BWD type timers.

Set a Flag when the switch goes from not pushed to pushed, turn OFF the output and reset that TIMER

When a Flag is set, check to see if that TIMER has expired; if it has, reset that Flag and turn ON the output.

Confirm your switches are wired as S2 in the schematic below:


Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

1 Like

An error occurred while pasting the code

Yes

here is an example of 3 independent groups of input pin / output pin and separate timers:

struct Groups {
  const byte buttonPin;
  const byte outputPin;
  uint32_t previousMillis;
};

Groups group[] {
  {A0, 2, 0},  // index 0: button Pin, outputPin, previousMillis
  {A1, 3, 0},
  {A2, 4, 0}
};

void tickButtonLed()
{
  for (auto & i : group)
  {
    if (digitalRead(i.buttonPin) == LOW) {
      digitalWrite(i.outputPin, HIGH);
      i.previousMillis = millis();   
    }
    if (millis() - i.previousMillis > 3 * 1000UL && digitalRead(i.outputPin) == HIGH) 
    {
      digitalWrite(i.outputPin, LOW);
    }
  }
}

void setup() {
  for (auto & i : group)
  {
    pinMode(i.buttonPin, INPUT_PULLUP);
    pinMode(i.outputPin, OUTPUT);
  }
}

void loop() {
  tickButtonLed();
}

the interval is set to 3 * 1000UL = 3 seconds.
You should be able to modify to 4 seconds on your own.

the code is described here:
https://werner.rothschopf.net/202003_arduino_retriggerbares_nachlaufrelais.htm

you can translate it with any online service to your language.

I have 40000 ms the time interval. The flag is set when the button is pressed.

How to configure the timer in code?

When a Flag is set, how to check if that TIMER has expired; ?

The code @noiasca posted will work with very little modification.

Before writing the code I want to understand few things.

I've been told I'll need four timers. I don't understand in the code which timer is using in code, how its configured and how can we compare the time

This is the code that compares the time:

if (millis() - i.previousMillis > 3 * 1000UL && digitalRead(i.outputPin) == HIGH) 
{
  digitalWrite(i.outputPin, LOW);
}

This should be very close, you will do the last two :wink:

//
// https://forum.arduino.cc/t/how-to-get-rid-of-this-blocking-delay/1025651
//
//********************************************^************************************************
//  XXXXXXXXXXXXXXXXX.ino
//
//
//  Version   YY/MM/DD     Comments
//  =======   ========     ========================================================
//  1.01      22/08/25     Running code
//
//********************************************^************************************************


#define ResetTimer         millis()

#define ENABLED            true
#define DISABLED           false

#define RelayOFF           HIGH
#define RelayON            LOW

#define PUSHED             LOW
#define RELEASED           HIGH


//********************************************^************************************************

const byte Switch_Pin1   = 4;
const byte Switch_Pin2   = 5;
const byte Switch_Pin3   = 6;
const byte Switch_Pin4   = 7;

const byte Relay_Pin1    = 8;
const byte Relay_Pin2    = 9;
const byte Relay_Pin3    = 10;
const byte Relay_Pin4    = 12;

const byte heartbeatLED  = 13;

boolean FLAG1            =  DISABLED;
boolean FLAG2            =  DISABLED;
boolean FLAG3            =  DISABLED;
boolean FLAG4            =  DISABLED;

byte lastSwitch_Pin1     =  RELEASED;
byte lastSwitch_Pin2     =  RELEASED;
byte lastSwitch_Pin3     =  RELEASED;
byte lastSwitch_Pin4     =  RELEASED;

//timing stuff
unsigned long heartbeatMillis;
unsigned long checkSwitchesTime;
unsigned long currentTime;

unsigned long relayTime1;
unsigned long relayTime2;
unsigned long relayTime3;
unsigned long relayTime4;

const long interval = 4000ul;


//********************************************^************************************************
void setup()
{
  // begin serial port with baud rate 9600bps
  Serial.begin(9600);

  // configure inputs pins for switche's
  pinMode(Switch_Pin1, INPUT_PULLUP);
  pinMode(Switch_Pin2, INPUT_PULLUP);
  pinMode(Switch_Pin3, INPUT_PULLUP);
  pinMode(Switch_Pin4, INPUT_PULLUP);

  pinMode(heartbeatLED, OUTPUT);

  pinMode(Relay_Pin1, OUTPUT);
  pinMode(Relay_Pin2, OUTPUT);
  pinMode(Relay_Pin3, OUTPUT);
  pinMode(Relay_Pin4, OUTPUT);

  digitalWrite(Relay_Pin1, RelayON);
  digitalWrite(Relay_Pin2, RelayON);
  digitalWrite(Relay_Pin3, RelayON);
  digitalWrite(Relay_Pin4, RelayON);

} //END of   setup()


//********************************************^************************************************
void loop()
{
  //capture the current time in milliseconds
  currentTime = millis();

  //*********************************                         heartbeat TIMER
  //is it time to toggle the heartbeatLED (every 500ms)?
  if (millis() - heartbeatMillis >= 500ul)
  {
    //restart this TIMER
    heartbeatMillis = millis();

    //toggle the heartbeatLED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //*********************************                         checkSwitches TIMER
  //time to check our switches ?
  if (currentTime - checkSwitchesTime >= 50)
  {
    //restart this TIMER
    checkSwitchesTime = ResetTimer;

    checkSwitches();
  }

  //*********************************                         Relay_Pin1 TIMER
  //if enabled, is this TIMER expired ?
  if (FLAG1 == ENABLED && currentTime - relayTime1 >= interval)
  {
    //we are now finished with this TIMER
    FLAG1 = DISABLED;

    digitalWrite(Relay_Pin1, RelayON);
  }

  //*********************************                         Relay_Pin2 TIMER
  //if enabled, is this TIMER expired ?
  if (FLAG2 == ENABLED && currentTime - relayTime2 >= interval)
  {
    //we are now finished with this TIMER
    FLAG2 = DISABLED;

    digitalWrite(Relay_Pin2, RelayON);
  }

  //*********************************
  //other non blocking code goes here
  //*********************************


} //END of    loop()


//********************************************^************************************************
void checkSwitches()
{
  //*********************************                   Switch_Pin1
  byte currentState = digitalRead(Switch_Pin1);

  //did this switch change state ?
  if (lastSwitch_Pin1 != currentState)
  {
    //update to the new state
    lastSwitch_Pin1 = currentState;

    //if we are not timing, is the switch now pushed ?
    if (FLAG1 == DISABLED && currentState == PUSHED)
    {
      //enable this TIMER
      FLAG1 = ENABLED;

      digitalWrite(Relay_Pin1, RelayOFF);

      //reset this TIMER
      relayTime1 = ResetTimer;
    }

    else
    {
      //do nothing here
    }

  } //END of this switch

  //*********************************                   Switch_Pin2
  currentState = digitalRead(Switch_Pin2);

  //did this switch change state ?
  if (lastSwitch_Pin2 != currentState)
  {
    //update to the new state
    lastSwitch_Pin2 = currentState;

    //if we are not timing, is the switch now pushed ?
    if (FLAG2 == DISABLED && currentState == PUSHED)
    {
      //enable this TIMER
      FLAG2 = ENABLED;

      digitalWrite(Relay_Pin2, RelayOFF);

      //reset this TIMER
      relayTime2 = ResetTimer;
    }

    else
    {
      //do nothing here
    }

  } //END of this switch

  //*********************************

} //END of checkSwitches()

EDIT
Added some comments

2 Likes

I am out of the home so can't test code but this is program I would test


#define ResetTimer         millis()

#define ENABLED            true
#define DISABLED           false

#define RelayOFF           HIGH
#define RelayON            LOW

#define PUSHED             LOW
#define RELEASED           HIGH

const byte Switch_Pin1   = 4;
const byte Switch_Pin2   = 5;
const byte Switch_Pin3   = 6;
const byte Switch_Pin4   = 7;


const byte Relay_Pin1    = 8;
const byte Relay_Pin2    = 9;
const byte Relay_Pin3    = 10;
const byte Relay_Pin4    = 12;


boolean FLAG1            =  DISABLED;
boolean FLAG2            =  DISABLED;
boolean FLAG3            =  DISABLED;
boolean FLAG4            =  DISABLED;

byte lastSwitch_Pin1     =  RELEASED;
byte lastSwitch_Pin2     =  RELEASED;
byte lastSwitch_Pin3     =  RELEASED;
byte lastSwitch_Pin4     =  RELEASED;

//timing stuff
unsigned long checkSwitchesTime;
unsigned long currentTime;

unsigned long relayTime1;
unsigned long relayTime2;
unsigned long relayTime3;
unsigned long relayTime4;

const long interval = 4000ul;

void setup()
{
  // begin serial port with baud rate 9600bps
  Serial.begin(9600);

  // configure inputs pins for switche's
  pinMode(Switch_Pin1, INPUT_PULLUP);
  pinMode(Switch_Pin2, INPUT_PULLUP);
  pinMode(Switch_Pin3, INPUT_PULLUP);
  pinMode(Switch_Pin4, INPUT_PULLUP);

  pinMode(Relay_Pin1, OUTPUT);
  pinMode(Relay_Pin2, OUTPUT);
  pinMode(Relay_Pin3, OUTPUT);
  pinMode(Relay_Pin4, OUTPUT);

  digitalWrite(Relay_Pin1, RelayON);
  digitalWrite(Relay_Pin1, RelayON);
  digitalWrite(Relay_Pin3, RelayON);
  digitalWrite(Relay_Pin4, RelayON);
}

void loop()
{
  //capture the current time in milliseconds
  currentTime = millis();

  //time to check our switches ?
  if (currentTime - checkSwitchesTime >= 50)
  {
    //restart this TIMER
    checkSwitchesTime = ResetTimer;

    checkSwitches();
  }

   //if enabled, is this TIMER expired ?
  if (FLAG1 == ENABLED && currentTime - relayTime1 >= interval)
  {
    //we are now finished with this TIMER
    FLAG1 = DISABLED;

    digitalWrite(Relay_Pin1, RelayON);
  }

    //if enabled, is this TIMER expired ?
  if (FLAG2 == ENABLED && currentTime - relayTime2 >= interval)
  {
    //we are now finished with this TIMER
    FLAG2 = DISABLED;

    digitalWrite(Relay_Pin2, RelayON);
  }
    if (FLAG3 == ENABLED && currentTime - relayTime3 >= interval)
  {
    //we are now finished with this TIMER
    FLAG3 = DISABLED;

    digitalWrite(Relay_Pin3, RelayON);
  }

  if (FLAG4 == ENABLED && currentTime - relayTime4 >= interval)
  {
    //we are now finished with this TIMER
    FLAG4 = DISABLED;

    digitalWrite(Relay_Pin4, RelayON);
  }

}

void checkSwitches()
{
  //*********************************                   Switch_Pin1
  byte currentState = digitalRead(Switch_Pin1);

  //did this switch change state ?
  if (lastSwitch_Pin1 != currentState)
  {
    //update to the new state
    lastSwitch_Pin1 = currentState;

    //if we are not timing, is the switch now pushed ?
    if (FLAG1 == DISABLED && currentState == PUSHED)
    {
      //enable this TIMER
      FLAG1 = ENABLED;

      digitalWrite(Relay_Pin1, RelayOFF);

      //reset this TIMER
      relayTime1 = ResetTimer;
    }

    else
    {
      //do nothing here
    }
  } //END of this switch

   //*********************************                   Switch_Pin2
  currentState = digitalRead(Switch_Pin2);
  

  //did this switch change state ?
  if (lastSwitch_Pin2 != currentState)
  {
    //update to the new state
    lastSwitch_Pin2 = currentState;

    //if we are not timing, is the switch now pushed ?
    if (FLAG2 == DISABLED && currentState == PUSHED)
    {
      //enable this TIMER
      FLAG2 = ENABLED;

      digitalWrite(Relay_Pin2, RelayOFF);

      //reset this TIMER
      relayTime2 = ResetTimer;
    }

    else
    {
      //do nothing here
    }
  }

    currentState = digitalRead(Switch_Pin3);
  

  //did this switch change state ?
  if (lastSwitch_Pin3 != currentState)
  {
    //update to the new state
    lastSwitch_Pin3 = currentState;

    //if we are not timing, is the switch now pushed ?
    if (FLAG3 == DISABLED && currentState == PUSHED)
    {
      //enable this TIMER
      FLAG3 = ENABLED;

      digitalWrite(Relay_Pin3, RelayOFF);

      //reset this TIMER
      relayTime3 = ResetTimer;
    }

    else
    {
      //do nothing here
    }

  }

    currentState = digitalRead(Switch_Pin4);
  

  //did this switch change state ?
  if (lastSwitch_Pin4 != currentState)
  {
    //update to the new state
    lastSwitch_Pin4 = currentState;

    //if we are not timing, is the switch now pushed ?
    if (FLAG4 == DISABLED && currentState == PUSHED)
    {
      //enable this TIMER
      FLAG4 = ENABLED;

      digitalWrite(Relay_Pin4, RelayOFF);

      //reset this TIMER
      relayTime4 = ResetTimer;
    }

    else
    {
      //do nothing here
    }

  }
}

If you are still confused about how Blink Without Delay works, study this excellent tutorial:

I have a doubt How many hardware timers are configured for four switches one or four?

There are no hardware timers being used.

There are 4 (four) software timers, one for each relay.

There is also 1 (one) software timer used for the checkSwitches( ) function.


In post #12, there is also 1 (one) software timer used for toggling the heartbeatLED.


In the sketch in post #12, we therefore have 6 software timers.

None, as you will understand after studying the tutorial linked in post #14.

Your additions look oaky.


Words of advice:

Review the following attached version.

Pay attention to the style and code formatting to see how you can achieve documentation and readability in your projects.

In the software that you write, try to follow a similar style.


Note:

  • In the following sketch pin 12 was moved over to pin 11.

  • A heartbeatLED was added.
    This LED gives a good indication if code is blocking; the LED should toggle every 1/2 second.


//********************************************^************************************************
//
//  https://forum.arduino.cc/t/how-to-get-rid-of-this-blocking-delay/1025651
//
//********************************************^************************************************
//  0_Arduino_Skeleton_Sketch.ino
//
//
//  Version   YY/MM/DD     Comments
//  =======   ========     ========================================================
//  1.01      22/08/25     Running code
//  1.02      22/08/25     Added code to handle relays 4 and 5, move from pin12 to pin 11
//  
//********************************************^************************************************

#define RESETtime          millis()

#define ENABLED            true
#define DISABLED           false

#define RelayOFF           HIGH
#define RelayON            LOW

#define PUSHED             LOW      //+5V---[Pullup]---[Input Pin]---[Switch]---GND
#define RELEASED           HIGH


//********************************************^************************************************

const byte Switch1       = 4;
const byte Switch2       = 5;
const byte Switch3       = 6;
const byte Switch4       = 7;

const byte Relay1        = 8;
const byte Relay2        = 9;
const byte Relay3        = 10;
const byte Relay4        = 11;

const byte heartbeatLED  = 13;

boolean TimerFlag1       =  DISABLED;
boolean TimerFlag2       =  DISABLED;
boolean TimerFlag3       =  DISABLED;
boolean TimerFlag4       =  DISABLED;

byte lastSwitch1         =  RELEASED;
byte lastSwitch2         =  RELEASED;
byte lastSwitch3         =  RELEASED;
byte lastSwitch4         =  RELEASED;

//timing stuff
const long interval      = 4000ul;

unsigned long currentTime;

unsigned long heartbeatTime;
unsigned long checkSwitchesTime;

unsigned long relay1Time;
unsigned long relay2Time;
unsigned long relay3Time;
unsigned long relay4Time;


//                                       s e t u p ( )
//********************************************^************************************************
void setup()
{
  Serial.begin(9600);

  pinMode(Switch1, INPUT_PULLUP);
  pinMode(Switch2, INPUT_PULLUP);
  pinMode(Switch3, INPUT_PULLUP);
  pinMode(Switch4, INPUT_PULLUP);

  pinMode(heartbeatLED, OUTPUT);

  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  pinMode(Relay3, OUTPUT);
  pinMode(Relay4, OUTPUT);

  digitalWrite(Relay1, RelayON);
  digitalWrite(Relay2, RelayON);
  digitalWrite(Relay3, RelayON);
  digitalWrite(Relay4, RelayON);

} //END of   setup()


//                                        l o o p ( )
//********************************************^************************************************
void loop()
{
  //capture the current time in milliseconds
  currentTime = millis();

  //*********************************                         heartbeat TIMER
  //is it time to toggle the heartbeatLED (every 500ms)?
  if (currentTime - heartbeatTime >= 500ul)
  {
    //restart this TIMER
    heartbeatTime = RESETtime;

    //toggle the heartbeatLED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //*********************************                         checkSwitches TIMER
  //time to check our switches ?
  if (currentTime - checkSwitchesTime >= 50ul)
  {
    //restart this TIMER
    checkSwitchesTime = RESETtime;

    checkSwitches();
  }

  //*********************************                         Relay1 TIMER
  //if enabled, is this TIMER expired ?
  if (TimerFlag1 == ENABLED && currentTime - relay1Time >= interval)
  {
    //we are now finished with this TIMER
    TimerFlag1 = DISABLED;

    digitalWrite(Relay1, RelayON);
  }

  //*********************************                         Relay2 TIMER
  //if enabled, is this TIMER expired ?
  if (TimerFlag2 == ENABLED && currentTime - relay2Time >= interval)
  {
    //we are now finished with this TIMER
    TimerFlag2 = DISABLED;

    digitalWrite(Relay2, RelayON);
  }

  //*********************************                         Relay3 TIMER
  //if enabled, is this TIMER expired ?
  if (TimerFlag3 == ENABLED && currentTime - relay3Time >= interval)
  {
    //we are now finished with this TIMER
    TimerFlag3 = DISABLED;

    digitalWrite(Relay3, RelayON);
  }

  //*********************************                         Relay4 TIMER
  //if enabled, is this TIMER expired ?
  if (TimerFlag4 == ENABLED && currentTime - relay4Time >= interval)
  {
    //we are now finished with this TIMER
    TimerFlag4 = DISABLED;

    digitalWrite(Relay4, RelayON);
  }

  
  //*********************************
  //other non blocking code goes here
  //*********************************


} //END of    loop()


//                               c h e c k S w i t c h e s ( )
//********************************************^************************************************
void checkSwitches()
{
    byte currentState;
    
  //*********************************                         Switch1
  currentState = digitalRead(Switch1);

  //did this switch change state ?
  if (lastSwitch1 != currentState)
  {
    //update to the new state
    lastSwitch1 = currentState;

    //if we are not timing, is the switch now pushed ?
    if (TimerFlag1 == DISABLED && currentState == PUSHED)
    {
      //enable this TIMER
      TimerFlag1 = ENABLED;

      digitalWrite(Relay1, RelayOFF);

      //reset this TIMER
      relay1Time = RESETtime;
    }

  } //END of this switch

  //*********************************                         Switch2
  currentState = digitalRead(Switch2);

  //did this switch change state ?
  if (lastSwitch2 != currentState)
  {
    //update to the new state
    lastSwitch2 = currentState;

    //if we are not timing, is the switch now pushed ?
    if (TimerFlag2 == DISABLED && currentState == PUSHED)
    {
      //enable this TIMER
      TimerFlag2 = ENABLED;

      digitalWrite(Relay2, RelayOFF);

      //reset this TIMER
      relay2Time = RESETtime;
    }

  } //END of this switch

  //*********************************                         Switch3
  currentState = digitalRead(Switch3);

  //did this switch change state ?
  if (lastSwitch3 != currentState)
  {
    //update to the new state
    lastSwitch3 = currentState;

    //if we are not timing, is the switch now pushed ?
    if (TimerFlag3 == DISABLED && currentState == PUSHED)
    {
      //enable this TIMER
      TimerFlag3 = ENABLED;

      digitalWrite(Relay3, RelayOFF);

      //reset this TIMER
      relay3Time = RESETtime;
    }

  } //END of this switch

  //*********************************                         Switch4
  currentState = digitalRead(Switch4);

  //did this switch change state ?
  if (lastSwitch4 != currentState)
  {
    //update to the new state
    lastSwitch4 = currentState;

    //if we are not timing, is the switch now pushed ?
    if (TimerFlag4 == DISABLED && currentState == PUSHED)
    {
      //enable this TIMER
      TimerFlag4 = ENABLED;

      digitalWrite(Relay4, RelayOFF);

      //reset this TIMER
      relay4Time = RESETtime;
    }

  } //END of this switch

  //*********************************

} //END of checkSwitches()


//********************************************^************************************************
1 Like

multiple switches reading work for me, How to read multiple serial port I want to avoid blocking delay mega2560 four ports

int count = 0;                                          // count = 0
char input[12];                                         // character array   

char * validTags[] = {"0A006FBE33E9", "0E004560173D"};

void setup()
{  
  Serial.begin(9600);               // begin serial port with baud rate 9600bps
  Serial1.begin(9600);    
}
void loop()
{     
if(Serial1.available()) 
{ 
  count = 0;  
  If(Serial1.available() && count < 12) // Read 12 characters and store them in input array      
  {        
    input[count] = Serial1.read(); 
    count++; 
    delay(5);     
  }    
    if  (count == 12){
        count = 0;
        String Value = input;
        Serial.println(Value);                     // Print RFID tag number
      }     
   
    for (int t = 0; t < 2; t++)
    {
       if (strcmp(validTags[t], input) == 0)
       {
          Serial.print("Tag ");
         
       }
     
    }
  }
  
}

You can use a non-blocking library such as LED library.

The below code:

// in loop function
digitalWrite(Relay_Pin1, HIGH);   
delay(4000); 
digitalWrite(Relay_Pin1, LOW);

Can be replaced by a non-blocking code:

relay.turnON();          // turn on immediately
relay.turnOFF(4000);     // turn off after 4 second

See this example for more detailed