Hi guys,
I want to write a program when board powered up turn on a led for a one minute and Meanwhile button press (push button) immediately turn off even before automaticallyturn off. How could I do that? I’ve heard something about millis() but I don’t know how to implement that.
Thanks
Millis() is a timer that increments every millisecond and can be used to check progress in a sketch …so….
Turn your led on in setup and at the same time make some “unsigned long” variable , say NOW equal to millis.
In loop check to see if millis is then greater than NOW + 60000 , “if” it is turn the led off .
Also in loop ( next line …) check your button input ( button state ?) , “if” that has changed turn the led off .
I’ve deliberately not written the code here , that’s for you to try . It is always best to write your own code than adapt something you don’t understand
a button switch is typically wired between the pin and ground, the pin configured as INPUT_PULLUP so that the pin is normally pulled HIGH and when the button is pressed the pin is pulled LOW
i usually initialize the button state in setup by reading the pin
i just add a 10 msec delay when a button is pressed to debounce
turn LED off if button pressed
need to turn the LED on in setup
should capture a timestamp in setup
loop() can use millis() to see if a minute has passed before turning the LED off
The example-code that you posted uses the function millis() for debouncing the button.
This is something different that "waiting for a button-press to switch of an LED"
For the task "waiting for a button-press to switch of an LED" debouncing is not nescessary.
As a pseudo-code
in a arduino-board is powered up the function setup() is executed once
So inside function setup() you code:
switch LED ON
if power is supplied to the arduino-board the function loop() is doing what its name says:
looping infinitely.
So inside loop() you have to do two things
checking if the button is pressed
checking how much time has gone by since powerup
if button is pressed: switch off LED
if one minute has passed by: switch LED off.
The established users just link to this very unsatisfying BWD-example.
Here is a short tutorial that is easier to understand as the "standard" Blink-without-delay-example. (I'd like to call it "blink without explain" )
So please start reading the example-code and then ask specific questions if you do not understand something