const int pumpPin = 3; // the number of the PUMP pin
const int pulseInput = 2; // the number of the PULSE pin
// Variables will change:
int pumpState = LOW; // pumpState used to set the PUMP
long startMillis = 0; // will store last time PUMP was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long timer = 1000*60*10; // timer of 10 minutes
void setup() {
// set the digital pin as output:
pinMode(pumpPin, OUTPUT);
digitalWrite(pumpPin, LOW);
pinMode(pulseInput, INPUT);
}
void loop()
{
// Checking for the pulse
if (digitalRead(pulseInput) == HIGH){
if (pumpState == LOW)
pumpState = HIGH;
else
pumpState = LOW;
}
// Update, update, update...
unsigned long currentMillis = millis();
// Load startMillis
if (pumpState == HIGH & startMillis == 0)
startMillis = currentMillis;
// If PumpState HIGH and Timer less than 10 minutes
if (pumpState == HIGH & currentMillis - startMillis < timer){
// Turn ON pump
digitalWrite(pumpPin, HIGH);
}
// Else turn OFF pump, reset startMillis and pumpState
else {
digitalWrite(pumpPin, LOW);
startMillis = 0;
pumpState = LOW;
}
}