Offline
Newbie
Karma: 0
Posts: 9
|
 |
« on: December 04, 2012, 01:23:33 pm » |
Greetings:
I am currently working on a timer that will operate a switch at fixed intervals. While that switch is operating, I would also like to take sensor readings that will operate a separate set of switches. Given that I want to perform other tasks while the timer is performing other functions, it is pretty clear I need to avoid the delay function. Right now, I have been examining the "Blink without Delay" code and seem to understand how that is operating. However, I am having a difficult time extending this example to other intervals. My hope is that I can get a few suggestions to help sort out this problem.
So, right now I am looking at the standard 'Blink Without Delay' code that is timing the LED to go on and off at 1 second intervals. What should I be doing to make the light operate as follows:
Step 1: On for 5 seconds, off for 10 seconds Step 2: On for 2 seconds, off for 5 seconds Step 3: On for 7 seconds, off for 2 seconds
Those are just arbitrary values, but if I get the general concept with these values, I can easily adapt the code to my needs -- right now, I just can't seem to get the logic for establishing these intervals! Ok, if that makes sense, a critical next step is to be able to perform simultaneous readings of a sensor that operates a different switch independent of this timer. I don't have to solve this issue now, but I want to ensure that the code I write can incorporate this independent task.
I hope this makes sense. Any bit of guidance you can offer at this point would be GREATLY appreciated.
Thanks in advance.
Brian
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 90
Posts: 9401
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #1 on: December 04, 2012, 01:39:13 pm » |
you need to make an array of ON and OFF intervals and take after every time the next interval if (millis() - lasttime > interval[idx]) { lasttime += interval[idx]; idx++; if (idx == idxmax) idx = 0; blink = ! blink; digitalWrite(13, blink); }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #2 on: December 04, 2012, 01:52:45 pm » |
Ok, I will take a look into the array, although I admit that I don't fully understand it at this point. If there is any further sample code that anybody has to guide me, that will be useful. During the interim, I will be studying the array function and the snippet provided.
Thanks! Brian
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13897
Lua rocks!
|
 |
« Reply #3 on: December 04, 2012, 02:19:13 pm » |
|
|
|
|
|
Logged
|
|
|
|
|
California
Offline
Edison Member
Karma: 41
Posts: 1869
|
 |
« Reply #4 on: December 04, 2012, 04:10:38 pm » |
You need a Finite State Machine. In the example, state 0 would contain an interval value of 5, state 1 would be 10, etc.
|
|
|
|
|
Logged
|
|
|
|
|
Sydney
Offline
God Member
Karma: 14
Posts: 717
Big things come in large packages
|
 |
« Reply #5 on: December 05, 2012, 02:13:13 am » |
My Multiblink example in the Playground uses a finite state machine and arrays of structures to keep track of multiple blink rates and timers. Not beginners code, but I am happy to help if you need it.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #6 on: December 05, 2012, 08:30:48 pm » |
Thanks for these suggestions. I am looking at the examples provided and will work toward a workable solution -- indeed, your help is appreciated. Just curious, do you think that a real time clock (rtc) is an option?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13897
Lua rocks!
|
 |
« Reply #7 on: December 05, 2012, 09:59:26 pm » |
You could use a RTC, but for "on for 10 seconds, off for 5 seconds" sort of stuff, using millis() in an appropriate way is all you need.
|
|
|
|
|
Logged
|
|
|
|
|
|