Go Down

Topic: Possible to have Arduino time-itself & carry out an action? (Read 1 time) previous topic - next topic

coding1227

Hi guys,

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.

Would something like this be possible?

Henry_Best


Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

CrossRoads

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.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

coding1227

Thanks for the tip about the blink example! So cool... I didn't know it existed.

This is just what I was looking for. Thanks for all the suggestions!

polymorph

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.
Steve Greenfield AE7HD
CET Consumer Electronics and Computer
Please don't read your attitudes into my messages

Nick Gammon

Blink is like "hello, world". It proves the damn thing works.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

retrolefty


Blink is like "hello, world". It proves the damn thing works.


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.

Lefty


Paul__B


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!

Nick Gammon

"Blink without delay" has a bug in it. *shrug*

Code: [Select]

long previousMillis = 0;        // will store last time LED was updated
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Paul__B

Then mend it, dear Henry, dear Henry, dear Henry,
Then mend it, dear Henry, dear Henry, mend it.
(Courtesy Wikipedia.)

Where is the source?  Is no-one responsible for maintaining the project?

Clearly the suggestion remains the same - replace "Blink" with a suitably fixed version of "Blink without delay".

Nick Gammon

Quote

Then mend it, dear Henry, dear Henry, dear Henry,
Then mend it, dear Henry, dear Henry, mend it.


I tried, a few minutes ago:

https://github.com/arduino/Arduino/issues/1618
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

polymorph



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.
Steve Greenfield AE7HD
CET Consumer Electronics and Computer
Please don't read your attitudes into my messages

retrolefty

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.

Lefty

Nick Gammon

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up