const int buttonPin = 10; // Pin for the switch or button
const int ledPin = 2; // Pin for the red LED
unsigned long lastWateringTime = 0; // Variable to store the last watering time
const unsigned long wateringInterval = 5000; // Interval for watering in milliseconds (5 seconds)
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Set the switch as input with internal pull-up resistor
pinMode(ledPin, OUTPUT); // Set the LED as output
digitalWrite(ledPin, HIGH); // Turn on the LED initially
}
void loop() {
int buttonState = digitalRead(buttonPin); // Read the state of the switch or button
// If the button is pressed for the first time, turn off the LED and record the time of watering
if (buttonState == LOW) {
digitalWrite(ledPin, LOW); // Turn off the LED
lastWateringTime = millis(); // Record the time of watering
}
// If it's time to water (5 seconds passed), turn on the LED
if (millis() - lastWateringTime >= wateringInterval) {
digitalWrite(ledPin, HIGH); // Turn on the LED
}
delay(10); // Update status every 10 milliseconds
}