Here is something to play with.
See if you can work your way through the sketch.
Ask questions if you don't fully understand things.
Closing a switch on pin #2 starts pump action.
//********************************************^************************************************
// URL
//
// LarryD
//
// Version YY/MM/DD Comments
// ======= ======== ====================================================================
// 1.00 YY/MM/DD Running code
//
//
//
//********************************************^************************************************
#define RESTART millis()
#define pumpON HIGH //PIN---[220R]---A[LED]K---GND
#define pumpOFF LOW
#define LEDon HIGH //PIN---[220R]---A[LED]K---GND
#define LEDoff LOW
#define PUSHED LOW //+5V---[Internal 50k]---PIN---[switch]---GND
#define RELEASED HIGH
#define CLOSED LOW //+5V---[Internal 50k]---PIN---[switch]---GND
#define OPEN HIGH
#define ENABLED true
#define DISABLED false
#define YES true
#define NO false
#define EXPIRED true
#define stillTIMING false
// m i l l i s ( ) B a s e d T I M E R S
//********************************************^************************************************
//
struct makeTIMER
{
//#define ENABLED true
//#define DISABLED false
//
//#define YES true
//#define NO false
//
//#define EXPIRED true
//#define stillTIMING false
unsigned long Time; //when the TIMER started
unsigned long Interval; //delay time in ms which we are looking for
bool TimerFlag; //is the TIMER enabled ? ENABLED/DISABLED
bool Restart; //restart this TIMER ? YES/NO
//****************************************
//fuction to check if the TIMER is enabled and check if the TIMER has expired
bool checkTIMER()
{
//*********************
//is this TIMER enabled and has this TIMER expired ?
if (TimerFlag == ENABLED && millis() - Time >= Interval)
{
//*********************
//should this TIMER restart again?
if (Restart == YES)
{
//restart this TIMER
Time = millis();
}
//this TIMER is enabled and has expired
return EXPIRED;
}
//this TIMER is disabled and/or has not expired
return stillTIMING;
} //END of checkTime()
}; //END of struct makeTIMER
//********************************************^************************************************
/*example
// *******************
makeTIMER toggleLED =
{
0, 500ul, ENABLED/DISABLED, YES/NO //.Time, .Interval, .TimerFlag, .Restart
};
*/
//*******************
makeTIMER heartbeatTIMER =
{
0, 500ul, ENABLED, YES //.Time, .Interval, .TimerFlag, .Restart
};
//*******************
makeTIMER scanSwitchesTIMER =
{
0, 50ul, ENABLED, YES //.Time, .Interval, .TimerFlag, .Restart
};
//*******************
makeTIMER pumpOnTIMER =
{
//5 minutes
//0, 5ul * 60 * 1000, DISABLED, NO //.Time, .Interval, .TimerFlag, .Restart
//5 seconds for testing
0, 5ul * 1000, DISABLED, NO //.Time, .Interval, .TimerFlag, .Restart
};
//*******************
makeTIMER pumpOffTIMER =
{
//5 hours
//0, 5ul * 60 * 60 * 1000, DISABLED, NO //.Time, .Interval, .TimerFlag, .Restart
//10 seconds for testing
0, 10ul * 1000, DISABLED, NO //.Time, .Interval, .TimerFlag, .Restart
};
//*******************
//********************************************^************************************************
const byte mySwitch = 2;
const byte waterPump = 12;
const byte heartbeatLED = 13;
byte lastMySwitch = OPEN;
// s e t u p ( )
//********************************************^************************************************
void setup()
{
Serial.begin(115200);
pinMode(mySwitch, INPUT_PULLUP);
pinMode(heartbeatLED, OUTPUT);
//pump off a powerup
digitalWrite(waterPump, pumpOFF);
pinMode(waterPump, OUTPUT);
} //END of setup()
// l o o p ( )
//********************************************^************************************************
void loop()
{
//************************************************ T I M E R heartbeatLED
//when enabled, is it time to toggle the heartbeat LED ?
if (heartbeatTIMER.checkTIMER() == EXPIRED)
{
//toggle the LED
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
//************************************************ T I M E R scanSwitches
//is this TIMER enabled and is it time to scan our switches ?
if (scanSwitchesTIMER.checkTIMER() == EXPIRED)
{
checkSwitches();
}
//************************************************ T I M E R pumpOn
//is this TIMER enabled and is it time to turn off the pump ?
if (pumpOnTIMER.checkTIMER() == EXPIRED)
{
digitalWrite(waterPump, pumpOFF);
//we are finished with this TIMER now
pumpOnTIMER.TimerFlag = DISABLED;
//enable the OFF TIMER
pumpOffTIMER.TimerFlag = ENABLED;
//restart the OFF TIMER
pumpOffTIMER.Time = RESTART;
}
//************************************************ T I M E R pumpOff
//is this TIMER enabled and is it time to turn on the pump ?
if (pumpOffTIMER.checkTIMER() == EXPIRED)
{
digitalWrite(waterPump, pumpON);
//we are finished with this TIMER now
pumpOffTIMER.TimerFlag = DISABLED;
//enable the ON TIMER
pumpOnTIMER.TimerFlag = ENABLED;
//restart the ON TIMER
pumpOnTIMER.Time = RESTART;
}
//************************************************
//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 state;
//************************************************ mySwitch
state = digitalRead(mySwitch);
//has there been a change in "mySwitch" state ?
if (lastMySwitch != state)
{
//update to this new state
lastMySwitch = state;
//*************************************
//is the reset switch closed ?
if (state == CLOSED)
{
//water pump
digitalWrite(waterPump, pumpON);
//disable the OFF TIMER
pumpOffTIMER.TimerFlag = DISABLED;
//enable the ON TIMER
pumpOnTIMER.TimerFlag = ENABLED;
//restart the ON TIMER
pumpOnTIMER.Time = RESTART;
}
} //END of this switch
//************************************************ nextSwitch
} //END of checkSwitches()
//********************************************^************************************************
EDIT
Fixed a small bug