# Making three LED's blink at given times [duplicate]

I have a breadboard connected to an Arduino Uno, 220k resistors connected to an led, of course it has a common ground and a pin connected to 5v. All pins are digital.

Now, I want led 1 to turn on for one second, the next led for two seconds and the next for three seconds, but that means, when the second led which has to be on for two seconds then the first led will come back on after one second has past on the second led, the third led would be on for three seconds, so the second led will also be on when two seconds pass and the first would be on when one second passes. So basically, all leds are on for their given duration.

How do I do this? I have been trying to do this since three days. It would be a pleasure if someone helps me out. Thanks.

``````int ledPin1= 2;
int ledPin2 = 4;
int ledPin3 = 7;

void setup(){
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}

void loop(){
digitalWrite(ledPin1, HIGH);
delay(500);
digitalWrite(ledPin1, LOW);
delay(500);

digitalWrite(ledPin2, HIGH);
delay(1000);
digitalWrite(ledPin2, LOW);
delay(1000);

digitalWrite(ledPin3, HIGH);
delay(1500);
digitalWrite(ledPin3, LOW);
delay(1500);
}
``````

So far this is the code. But it delays the whole program. Need leds on at given times.

You could start by drawing a time graph, like this:

``````LED 1   # # # # # # # # # # # # # # # # # #
LED 2    ##  ##  ##  ##  ##  ##  ##  ##  ##
LED 3      ###   ###   ###   ###   ###   ###
<-period 1-><-period 2-><-period 3->
``````

As you see on the graph, the whole sequence repeats itself every 12 seconds. Then, you can just write down all the pin toggles for 12 seconds and let the loop repeat itself:

``````void loop() {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin3, LOW);
delay(1000);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
delay(1000);
digitalWrite(ledPin1, HIGH);
delay(1000);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
delay(1000);
digitalWrite(ledPin1, HIGH);
delay(1000);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
delay(1000);
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin3, LOW);
delay(1000);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
delay(1000);
digitalWrite(ledPin1, HIGH);
delay(1000);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
delay(1000);
digitalWrite(ledPin1, HIGH);
delay(1000);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
delay(1000);
}
``````

This was the simple, albeit tedious solution.

The smart solution is to go through the Blink Without Delay Arduino tutorial. You will see that, once you start using `millis()` instead of `delay()` for managing your timings, blinking three LEDs instead of one is completely trivial.

How about this? Basically each loop is 1sec, and each LED has it's own counter which increases. If the counter is higher than the LED number/onTime, the LED is off for that loop and the conter reset, otherwise LED is on.

``````uint8_t LED[3] = { 2, 4, 7 };
uint8_t LED_COUNTER[3] = { 0, 0, 0 };

void setup() {
for (int i = 0; i < 3; i++) {
pinMode(LED[i], OUTPUT);
}
}

void loop() {
for (int i = 0; i < 3; i++) {
if (LED_COUNT[i] = i) { LED_COUNT[i] = 0; }
else {
digitalWrite(LED[i], 1);
LED_COUNT[i] += 1;
}
}
delay(1000);
}
``````
• You are never switching the LEDs off. – Edgar Bonet Sep 17 '16 at 11:50
• There is a problem with the led count – Rusty Ryan Sep 17 '16 at 11:56
• Yes sorry it was mainly intended to explain the concept, I didn't go much in detail. Either way I see on the duplicate of this question there is a far better solution... – nxet Sep 17 '16 at 12:05

EDIT: I just love Finit State Machines and templates to do some simple tasks:

``````class FSM {
public:
using Handler = void(*)(FSM &);

FSM(Handler hnd) : m_func{hnd} {}

void check(uint32_t ts = millis()) {
if (m_next < ts) {
m_func(*this);
}
}

void delay(uint32_t _delay) {
m_next += _delay;
}

void nextState(uint8_t state) {
m_state = state;
}

uint8_t state() { return m_state; }

protected:
Handler   m_func;
uint8_t  m_state = 0;
uint32_t  m_next = 0;
};

template <uint8_t PIN, uint32_t HIGH_DELAY, uint32_t LOW_DELAY> void pin_fsm(FSM & fsm) {
switch (fsm.state()) {
case 0:
pinMode(PIN, OUTPUT);
// fall through
case 1:
digitalWrite(PIN, HIGH);
fsm.delay(HIGH_DELAY);
fsm.nextState(2);
break;
case 2:
digitalWrite(PIN, LOW);
fsm.delay(LOW_DELAY);
fsm.nextState(1);
break;
}
}

FSM fsms[] = {{&pin_fsm<2,500,500>}, {&pin_fsm<4,1000,1000>}, {&pin_fsm<7,1500,1500>}};

void setup() {

}

void loop() {
auto ts = millis();
for (FSM& fsm : fsms) fsm.check(ts);
delay(1);
}
``````

The code should be working, so as mentioned before in that duplicate thread, `220k` is far bigger than it should be. So just use `220R` (`= 220 Ohms`) instead of `220k Ohms` (`= 220000 Ohms`).

And if it's not working even with correct resistor, your LED is connected in reversed direction. Try the builtin LED on pin 13. That one must be working. Anyway you should read something about LEDs.