Arduino with relay and on-off rocker switch Help!

Cleaned up and ready to roll.

A present for you:

//https://forum.arduino.cc/t/arduino-with-relay-and-on-off-rocker-switch-help/907740/38

//Two pumps toggling ON/OFF at a given rate, manual switches overrides the automatic operation

//********************************************************************************
//Version    YY/MM/DD    Description
//1.00       21/09/21    Running sketch
//1.01       21/09/21    Added 2nd relay/pump and 2nd switch
//
//

#define CLOSED                       LOW
#define OPENED                       HIGH

#define ENABLED                      true
#define DISABLED                     false

#define PUMPon                       LOW
#define PUMPoff                      HIGH

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

boolean switch1Flag                = !ENABLED;
boolean switch2Flag                = !ENABLED;

byte lastSwitch1State              = OPENED;
byte lastSwitch2State              = OPENED;

byte pump1State                    = PUMPoff;
byte pump2State                    = PUMPoff;

const byte pump1Relay              = 2;
const byte pump2Relay              = 4;
const byte SWITCH1                 = 7;
const byte SWITCH2                 = 8;
const byte heartbeatLED            = 13;

//timing stuff
unsigned long pump1Millis;
unsigned long pump2Millis;
unsigned long heartbeatMillis;
unsigned long switchMillis;

unsigned long pump1_interval;
unsigned long pump2_interval;

// PUMP 1
const unsigned long pump1_ON_time  = 2000;
const unsigned long pump1_OFF_time = 5000;

// PUMP 2
const unsigned long pump2_ON_time  = 2000;
const unsigned long pump2_OFF_time = 5000;


//***************************************************************
void setup()
{
  Serial.begin(115200);

  pinMode(heartbeatLED, OUTPUT);
  pinMode(pump1Relay, OUTPUT);
  pinMode(pump2Relay, OUTPUT);

  pinMode(SWITCH1, INPUT_PULLUP);
  pinMode(SWITCH2, INPUT_PULLUP);

  digitalWrite(pump1Relay, PUMPoff);
  pump1State = PUMPoff;
  pump1_interval = pump1_OFF_time;

  digitalWrite(pump2Relay, PUMPoff);
  pump2State = PUMPoff;
  pump2_interval = pump2_OFF_time;

} //END of setup()

//***************************************************************
void loop()
{
  //*************************************                          h e a r t b e a t   T I M E R
  //to see if the sketch is blocking,
  //toggle the heartbeat LED every 500ms
  if (millis() - heartbeatMillis >= 500)
  {
    //restart the TIMER
    heartbeatMillis = millis();

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

  //*************************************                          c h e c k S w i t c h   T I M E R
  //is it time to read the switches ?
  if (millis() - switchMillis >= 50)
  {
    //restart the TIMER
    switchMillis = millis();

    checkSwitches();
  }

  //*************************************                          P U M P   1 
  //if the switch is 'not enabled' proceed with autmatic operation
  if (switch1Flag == !ENABLED)
  {
    //***********************************                          p u m p 1   T I M E R
    //is it time to toggle the pump relay ?
    if (millis() - pump1Millis >= pump1_interval)
    {
      //restart the TIMER
      pump1Millis = millis();

      //is pump1 now OFF ?
      if (pump1State == PUMPoff)
      {
        digitalWrite(pump1Relay, PUMPon);

        pump1State = PUMPon;

        pump1_interval = pump1_ON_time;
      }

      else
      {
        digitalWrite(pump1Relay, PUMPoff);

        pump1State = PUMPoff;

        pump1_interval = pump1_OFF_time;
      }
    }
  } //END of   if(switch1Flag == !ENABLED)


  //*************************************                          P U M P   2 
  //if the switch is 'not enabled' proceed with autmatic operation
  if (switch2Flag == !ENABLED)
  {
    //***********************************                          p u m p 2   T I M E R
    //is it time to toggle the pump relay ?
    if (millis() - pump2Millis >= pump2_interval)
    {
      //restart the TIMER
      pump2Millis = millis();

      //is pump2 now OFF ?
      if (pump2State == PUMPoff)
      {
        digitalWrite(pump2Relay, PUMPon);

        pump2State = PUMPon;

        pump2_interval = pump2_ON_time;
      }

      else
      {
        digitalWrite(pump2Relay, PUMPoff);

        pump2State = PUMPoff;

        pump2_interval = pump2_OFF_time;
      }
    }
  } //END of    if(switch2Flag == !ENABLED)

} //END of loop()


//********************************************************************************
void checkSwitches()
{
  //*********************************************                    S W I T C H 1
  //SWITCH1 code
  byte currentState = digitalRead(SWITCH1);

  //**********************
  //was there a change in state ?
  if (lastSwitch1State != currentState)
  {
    //update to the new state
    lastSwitch1State = currentState;

    //**********************                                         C L O S E D
    //is the switch closed ?
    if (currentState == CLOSED)
    {
      digitalWrite(pump1Relay, PUMPon);

      pump1State = PUMPon;

      //allow manual switch bypass
      switch1Flag = ENABLED;
    }

    //**********************                                         O P E N E D
    //the switch is opened
    else
    {
      digitalWrite(pump1Relay, PUMPoff);

      pump1State = PUMPoff;

      pump1_interval = pump1_OFF_time;

      pump1Millis = millis();

      //cancel manual switch bypass
      switch1Flag = DISABLED;
    }

  } //END of SWITCH1 code


  //*********************************************                    S W I T C H 2
  //SWITCH2 code
  currentState = digitalRead(SWITCH2);

  //**********************
  //was there a change in state ?
  if (lastSwitch2State != currentState)
  {
    //update to the new state
    lastSwitch2State = currentState;

    //**********************                                         C L O S E D
    //is the switch closed ?
    if (currentState == CLOSED)
    {
      digitalWrite(pump2Relay, PUMPon);

      pump2State = PUMPon;

      //allow manual switch bypass
      switch2Flag = ENABLED;
    }

    //**********************                                         O P E N E D
    //the switch is opened
    else
    {
      digitalWrite(pump2Relay, PUMPoff);

      pump2State = PUMPoff;

      pump2_interval = pump2_OFF_time;

      pump2Millis = millis();

      //cancel manual switch bypass
      switch2Flag = DISABLED;
    }

  } //END of SWITCH2 code



  //*********************************************                    o t h e r S w i t c h e s
  //next switch code
  //*********************************************

} //END of   checkSwitches()



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