blink with out delay
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
-
- Posts: 50
- Joined: Tue Nov 29, 2011 11:59 pm
blink with out delay
i am setting up my uno r3 for light flashing using blink without delay sketch and wondering if I can program for 8 light circuits with each a different flash rate, or can this be done with a different type code type? Charlie J
- adafruit_support_mike
- Posts: 67485
- Joined: Thu Feb 11, 2010 2:51 pm
Re: blink with out delay
The code in Blink Without Delay should work.. that's pretty much the use case it was designed for. You'll just need to keep track of the same information for eight different LEDs.
Your storage would look something like so:
and you'd call a function like this:
with the values 0-7 and the latest reading from millis() each time through the main loop.
You could get rid of the function and unroll the loop by writing a version the test-and-blink code for each LED and delay, but in coding it's useful to develop a habit of thinking, "if I'm going to do it more than once, put it in a function."
NOTE: I haven't actually compiled or tested that code, so I can't say it works with certainty. The general ideas are solid, but I've been writing code in so many different languages recently that I originally tried to declare the lists in Perl syntax (@array = ( . . . )).
Use at own risk, your mileage may vary, may contain nuts, etc.
Your storage would look something like so:
Code: Select all
int ledPin[8] = { 12, 11, 10, 9, 8, 7, 6, 5 };
int ledState[8] = { LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW };
int interval[8] = { 100, 300, 500, 700, 1100, 1300, 1700, 1900 };
long lastChange[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Code: Select all
void checkLedNAtTimeT( int n, long t) {
if (t - lastChange[ n ] > interval[ n ]) {
lastChange[ n ] = t;
ledState[ n ] = (HIGH == ledState[ n ]) ? LOW : HIGH;
digitalWrite( ledPin[ n ] , ledState[ n ]);
}
}
You could get rid of the function and unroll the loop by writing a version the test-and-blink code for each LED and delay, but in coding it's useful to develop a habit of thinking, "if I'm going to do it more than once, put it in a function."
NOTE: I haven't actually compiled or tested that code, so I can't say it works with certainty. The general ideas are solid, but I've been writing code in so many different languages recently that I originally tried to declare the lists in Perl syntax (@array = ( . . . )).
Use at own risk, your mileage may vary, may contain nuts, etc.
Please be positive and constructive with your questions and comments.