/* code modified base on example from: * https://playground.arduino.cc/Code/HoldButton */ int buttonPin = 7; int ledPin = 3; int buttonState = 0; unsigned long firstTime; byte previous = HIGH; long millis_held; long secs_held; long prev_secs_held; void setup() { pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT); } void loop() { buttonState = digitalRead(buttonPin); /* if button gets pushed, update start time */ if (buttonState == LOW && previous == HIGH && (millis() - firstTime) > 10) { firstTime = millis(); } millis_held = (millis() - firstTime); secs_held = millis_held / 1000; /* if button pressed more than 10ms debounce */ if (millis_held > 10) { /* when button is pressed down */ if (buttonState == LOW && secs_held > prev_secs_held) { digitalWrite(ledPin, HIGH); } /* when button is released */ if (buttonState == HIGH && previous == LOW) { /* if hold button for more than 3 sec LED on */ if (secs_held >= 3) { digitalWrite(ledPin, HIGH); } /* if hold button from 1-3 sec quickly blink 10 times */ if (secs_held >= 1 && secs_held < 3) { ledblink(10,80); } /* if hold button less than 1 sec blink once */ if (secs_held < 1) { ledblink(1, 100); } } } /* update */ previous = buttonState; prev_secs_held = secs_held; } /* blink function */ void ledblink(int times, int lengthms){ for (int x=0; x