Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple watering system using a relay to activate a peristaltic pump
const int RELAY_PIN = 2;
unsigned long LONG_DELAY_MS = 43200000; // 12 hours expressed in milliseconds
unsigned int WATERING_TIME = 2000;
unsigned long startMillis;
void setup() {
pinMode(RELAY_PIN, OUTPUT);
startMillis = millis();
Serial.begin(9600);
digitalWrite(RELAY_PIN, LOW);
Serial.print("Long delay: ");
Serial.println(LONG_DELAY_MS);
}
void loop() {
if (millis() - startMillis > LONG_DELAY_MS){
digitalWrite(RELAY_PIN, HIGH);
Serial.println(millis());
Serial.println("---------");
Serial.println("watering!");
Serial.println("---------");
// reset the timer
startMillis = millis();
// wait the required time
delay(WATERING_TIME);
}
else {
digitalWrite(RELAY_PIN, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment