I understand being confused, I get confused, too with "millis()". There is no "right" way to do this, but some methods are preferred. The more you tinker with it, the more you will understand. Here is my spin on "blink without delay" that you can plug into WOKWI.COM for testing. The LED_BUILTIN blinks all the time UTIL the button is pressed, and the LED pauses in the state it was in UNTIL the button is released. The Serial Monitor also shows the state of the LED.
sketch.ino
unsigned long newMillis = 0, oldMillis = 0; // event timers
unsigned long interval = 500; // milliseconds
bool state; // the state of the LED
byte buttonPin = 2; // button pin
void setup() {
Serial.begin(115200); // configure Serial Monitor baud
pinMode(LED_BUILTIN, OUTPUT); // configure LED_BUILTIN/DIO pin 13 as OUTPUT
pinMode(buttonPin, INPUT_PULLUP); // configure button pin as INPUT with internal pullup resistor
}
void loop() {
blink(); // call the "blink" function
}
void blink() {
newMillis = millis(); // start an event
if (newMillis - oldMillis > interval) { // compare difference in event times to interval
oldMillis = newMillis; // store the new event start time
state = !state; // flip the current state for the LED
digitalWrite(LED_BUILTIN, state); // set the LED to the current state
if (state) Serial.print("1"); // show current state in Serial Monitor
else Serial.print("0"); // show current state in Serial Monitor
}
while (!digitalRead(buttonPin)) {} // hold here while button is pressed
}
diagram.json
{
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-nano", "id": "nano", "top": -110.4, "left": 201.1, "attrs": {} },
{
"type": "wokwi-pushbutton-6mm",
"id": "btn1",
"top": -136.6,
"left": 336,
"attrs": { "color": "green" }
},
{
"type": "wokwi-text",
"id": "text1",
"top": -163.2,
"left": 326.4,
"attrs": { "text": "pause" }
}
],
"connections": [ [ "nano:GND.2", "btn1:2.l", "black", [ "v0" ] ], [ "nano:2", "btn1:1.l", "green", [ "v0" ] ] ],
"dependencies": {}
}
