I have an interesting conceptual question , and I was wondering if it would be possible to do with Arduino Uno.
Say we take a "blink" sketch, for instance, and set it up so that an LED blinks every 10 sec. Would it be possible to have the Arduino start a timer during the set-up (to start keeping track for the time that it is "on"), and then have it do an action (i.e.: print "Hello" in the serial output) after 5 seconds of the Arduino "power on" REGARDLESS of what is happening in the "void loop"?
I guess this conceptual question would be applicable in case there is a command in the "void loop" section that takes "too long " (in this case more than 5sec) to complete. Perhaps this would be applicable for some kind of sensor that takes a long time to collect data.
You're describing a watchdog kind of situation. Code part A sets a flag HIGH or LOW (or a byte to 0 or 1) every so often. Code part B monitors the flag, if it sees the flag is not in the correct state, then an error is declared.
Great page, Nick. Good contrast between the stock "Blink" example vs "Blink Without Delay". I wish they'd take the "Blink" Sketch out of the examples folder, it is a waste to start a newbie off by teaching them the worst way to do things.
If all I wanted to do was blink an LED, I'd wire up a 555 timer.
Correct, as it proves to the beginner that the IDE is correctly installed and that the serial com port and board type is selected correctly and that the arduino board can communicate with the PC. The basic Blink sketch should never be deleted from the example sketches.
retrolefty:
The basic Blink sketch should never be deleted from the example sketches.
The point was made - it should be removed, and replaced by "Blink without delay", if only to dissuade newbies from proceeding to use the "delay" function in serious projects. "Blink without delay" is no less effective in demonstrating that the board works, and certainly no more difficult to compile and load.
In a similar vein, I should like to see the "button" examples replaced by ones which have the button connected to ground and using internal pull-ups, to avoid people running live supply rails to external contacts.
If the concept of inverted logic needs to be learned earlier than later - so be it!
retrolefty:
The basic Blink sketch should never be deleted from the example sketches.
The point was made - it should be removed, and replaced by "Blink without delay", if only to dissuade newbies from proceeding to use the "delay" function in serious projects. "Blink without delay" is no less effective in demonstrating that the board works, and certainly no more difficult to compile and load.
In a similar vein, I should like to see the "button" examples replaced by ones which have the button connected to ground and using internal pull-ups, to avoid people running live supply rails to external contacts.
If the concept of inverted logic needs to be learned earlier than later - so be it!
We wouldn't teach first time drivers to stop using the emergency brake. So why do we continue to teach them to blink an LED using delay()?
And I agree wholeheartedly about the button examples. In addition to the excellent reason given above, they'll have to learn inverted logic the moment they try to use the internal pullup.
That sounds like some are of the opinion that the delay() function should never be used and therefore no arduino sketch example should ever show the utilization of one? That sounds a little elitist to me as there can and will be perfectly sound reasons one may want to utilize delay() in a sketch.
I use delay() quite frequently, in cases where it is a simple solution to a problem, eg.:
Debounce a switch for 10 mS
Flash a warning LED 5 times
If there is nothing else to be done, why not use delay() ?
However I don't agree about using it where it fudges things that otherwise wouldn't work. For example, doing a delay "to allow serial data time to arrive". In the case of receiving serial data, even if you have nothing else to do at the time, a delay is simply hoping, rather than knowing, that the next byte has arrived.
Elitist? What does that mean, in this context? Please don't read extreme opinions into what I wrote.
I use my car's emergency brake, but only in specific circumstances. I wouldn't teach someone to drive using the emergency brake, -then- later teach them to use the brake pedal.
If delay() works for the task, use it. But in a noisy environment, using delay(10) to debounce a switch will also read two noise pulses, 10ms apart, as a switch press.