Hi, I'm a starter for Arduino and it's quite fun for now. I'm now doing a delay-hold button where the mechanism is when I push and hold the button for 10 second, LED will lights up and as soon I release the button, it will soon terminate the light without any delay. I've been doing the code but sadly, it does not terminate the light and it takes 10 seconds back to dim off. Please help. Thanks sketch_may21g.ino (595 Bytes)
hi, thanks for the reply. I try to use it yesterday. But it seems that many example on this forum that I have found use codes that needs to have a cycle (push and release) at the input button (for the system to be running), while for this project, the cycle ends (the button is released from hold condition) means the system running is terminated afterwards.
If you can provide any example for the hold condition, I would be grateful. again, thanks for the reply
The problem here is that, once the button has been pressed, you then need your sketch to start doing 2 things at once: 1. checking to see if the button has been released and 2. checking if 10 seconds has passed.
Using delay() "blocks" the code from doing anything else, even checking if the button is released. So you can't use delay() for timing the 10 second wait. Instead, you must use millis(). Plus, if the button is released in less than 10 seconds, you don't want to continue waiting for the remainder of the 10s. Using delay() prevents you doing that also.
Using millis() is a bit like using a stop-watch, except for 2 things: 1. it "ticks" 1000 times per second and 2. you can't reset it to zero, so if you want to use it to time 10s, you have to make a note of the time when the button was pressed, and then keep comparing the current time back to that time to figure out when 10s has passed.