const int relayPin = 3; // Pin connected to the relay (controls the pump)const int buttonPin = 2; // Pin connected to the buttonbool automationActive = false; // Tracks whether automation is activebool buttonPressed = false; // Tracks button press state for toggling
void setup() { pinMode(relayPin, OUTPUT); // Set relay pin as output pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with pull-up resistor digitalWrite(relayPin, LOW); // Ensure pump is off initially Serial.begin(9600); // Initialize serial monitor for debugging}
void loop() { // Check if the button is pressed (LOW state) if (digitalRead(buttonPin) == LOW && !buttonPressed) { delay(50); // Debounce delay if (digitalRead(buttonPin) == LOW) { // Confirm the button press buttonPressed = true; // Prevent multiple triggers for one press automationActive = !automationActive; // Toggle automation state
// Print current state for debugging if (automationActive) { Serial.println("Automation system activated"); } else { Serial.println("Automation system deactivated"); digitalWrite(relayPin, LOW); // Ensure pump is off when automation stops } } }
// Reset button state after release if (digitalRead(buttonPin) == HIGH) { buttonPressed = false; }
// Run automation system if active if (automationActive) { digitalWrite(relayPin, HIGH); // Turn on the pump Serial.println("Pump ON"); delay(2000); // Run pump for 2 seconds digitalWrite(relayPin, LOW); // Turn off the pump Serial.println("Pump OFF"); delay(10000); // Wait for 10 seconds }}
void setup() { pinMode(relayPin, OUTPUT); // Set relay pin as output pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with pull-up resistor digitalWrite(relayPin, LOW); // Ensure pump is off initially Serial.begin(9600); // Initialize serial monitor for debugging}
void loop() { // Check if the button is pressed (LOW state) if (digitalRead(buttonPin) == LOW && !buttonPressed) { delay(50); // Debounce delay if (digitalRead(buttonPin) == LOW) { // Confirm the button press buttonPressed = true; // Prevent multiple triggers for one press automationActive = !automationActive; // Toggle automation state
// Print current state for debugging if (automationActive) { Serial.println("Automation system activated"); } else { Serial.println("Automation system deactivated"); digitalWrite(relayPin, LOW); // Ensure pump is off when automation stops } } }
// Reset button state after release if (digitalRead(buttonPin) == HIGH) { buttonPressed = false; }
// Run automation system if active if (automationActive) { digitalWrite(relayPin, HIGH); // Turn on the pump Serial.println("Pump ON"); delay(2000); // Run pump for 2 seconds digitalWrite(relayPin, LOW); // Turn off the pump Serial.println("Pump OFF"); delay(10000); // Wait for 10 seconds }}