// https://forum.arduino.cc/t/how-to-delay-using-millis/1142938
// https://forum.arduino.cc/t/timer-on-pin-low-high-detection/1325679
// https://wokwi.com/projects/415656135572365313
// version 5. same same with variants
const byte buttonPin = 2;
const byte ledPin = 3;
const unsigned long actionDelay = 2500; // action 2.5 seconds for testing - life too short
void setup() {
Serial.begin(115200);
Serial.println("\nWake up!\n");
pinMode (buttonPin, INPUT_PULLUP);
pinMode (ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
static unsigned long startTime; // time at action
unsigned long now = millis(); // fancier 'currentMillis' ooh!
bool buttonIsPressed = digitalRead(buttonPin) == LOW; // button pulled up! true is button pressed
// when the button is _not_ pressed, reset the timer, turn OFF the LED
if (!buttonIsPressed) {
startTime = now;
digitalWrite(ledPin, LOW);
}
// when (if!) the timer expires, turn ON the LED
if (now - startTime >= actionDelay) {
digitalWrite(ledPin, HIGH);
}
}
// alternate method - throttled loop counter
# define frameTime 20 // milliseconds per loop
# define longEnough 100 // # of loops to recognize a button press 10 * 20 = 2000, two seconds
void loop0()
{
static unsigned long lastLoopTime;
unsigned long now = millis();
// time to do the loop again?
if (now - lastLoopTime < frameTime)
return;
// yes.
lastLoopTime = now;
static int buttonCounter = 0;
bool buttonIsPressed = digitalRead(buttonPin) == LOW; // button pulled up! true is button pressed
if (buttonIsPressed)
buttonCounter++;
else {
buttonCounter = 0;
digitalWrite(ledPin, LOW);
}
if (buttonCounter >= longEnough)
digitalWrite(ledPin, HIGH);
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
led1:A
led1:C
r1:1
r1:2