Pages: [1] 2   Go Down
Author Topic: Possible to have Arduino time-itself & carry out an action?  (Read 957 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

London
Offline Offline
Edison Member
*
Karma: 47
Posts: 1442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

See the 'blink without delay' example.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18810
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://www.gammon.com.au/blink
Logged


Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 538
Posts: 27140
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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.

Offline Offline
Full Member
***
Karma: 0
Posts: 104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Lacey, Washington, USA
Offline Offline
Edison Member
*
Karma: 155
Posts: 2364
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Steve Greenfield AE7HD
CET Consumer Electronics and Computer
Please don't read your attitudes into my messages

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18810
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

NSW Australia
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3566
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18810
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
long previousMillis = 0;        // will store last time LED was updated
Logged


NSW Australia
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3566
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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".
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18810
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Lacey, Washington, USA
Offline Offline
Edison Member
*
Karma: 155
Posts: 2364
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Steve Greenfield AE7HD
CET Consumer Electronics and Computer
Please don't read your attitudes into my messages

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18810
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Pages: [1] 2   Go Up
Jump to: