const int BUTTON_PIN = 27; // the number of the pushbutton pin const int VACUUM_PIN = 26; // the number of the pump pin (28 for pump, 26 for LED) int buttonState = 0; // variable for reading the pushbutton status bool lastButtonState = LOW; bool pumpState = LOW; unsigned long lastDebounceTime = 0; // the last time the output pin was toggled unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers void setup() { pinMode(VACUUM_PIN, OUTPUT); // pump is defined as output pinMode(BUTTON_PIN, INPUT); // pushbutton is defined as input Serial.begin(9600); } void loop() { // read button status int reading = digitalRead(BUTTON_PIN); // check to see if you just pressed the button // (i.e. the input went from LOW to HIGH), and you've waited long enough // since the last press to ignore any noise: // If the switch changed, due to noise or pressing: if (reading != lastButtonState) { // reset the debouncing timer lastDebounceTime = millis(); } if ((millis() - lastDebounceTime) > debounceDelay) { // whatever the reading is at, it's been there for longer than the debounce delay, // so take it as the actual current state: // if the button state has changed: if (reading != buttonState) { buttonState = reading; // only toggle the pump if the new button state is HIGH (pressed) if (buttonState == HIGH) { pumpState = !pumpState; digitalWrite(VACUUM_PIN, pumpState); } } } // save the reading. Next time through the loop, it'll be the lastButtonState: lastButtonState = reading; }