hello to everybody.i am new with arduino and coding generally and i could use some help about something.how can i program a simple digital blink code (such as the example ) but using two or more blink leds with different delays each and to work simultaneously?every time i try the one led waits for the other to finish the loop and vice versa.i want them to work simultaneously and with my prefered delays.thanks in advance.
Here's a little useful trick.
const unsigned long delays[3] = {200, 500, 714}; // The delays for each LED
unsigned long last_update_time[3] = {0, 0, 0}; // Track the last update time for each LED
int state[3] = {LOW, LOW, LOW}; // Track the current state of each LED
const pins[3] = {9, 10, 11}; // Let's say these are your pins
void setup()
{
for (int i=0; i<3; ++i)
pinMode(pins[i], OUTPUT);
}
void loop()
{
for (int i=0; i<3; ++i)
{
unsigned long m = millis();
if (m - last_update_time[i] >= delays[i]) // is it time for LED #i to change state?
{
state[i] = state[i] == LOW ? HIGH : LOW; // invert state
digitalWrite(pins[i], state[i]); // write pin
last_update_time[i] = m; // record update time
}
}
}
Are you fluent enough with reading code to understand how that works? If not, let me know and I can explain.
Mikal
thank you for your help! but unfortunately it does not compile it.i get this message: "error: ISO C++ forbids declaration of 'pins' with no type" refering to the 4 line where you set your pins.
thank you again.don't bother anymore. i did it.it just wanted an int at the beginning of 4 line.logic.i am greatfull.!!!!!