Making three LED's blink at given times

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.

Is this what you want?

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);
  
  digitalWrite(ledPin2, HIGH);
  delay(1000);
  digitalWrite(ledPin2, LOW);
  
  digitalWrite(ledPin3, HIGH);
  delay(1500);
  digitalWrite(ledPin3, LOW);
      }

Hi,

220k resistors connected to an led

I think you should have a 220R resistor connected to each LED.

Tom... :slight_smile:

Hi,

Now, I want led 1 to turn on for one second, the next led for two seconds and the next for three seconds

Do you want?
LED1 to flash ON for 1S and OFF for 1S continuosly.
LED2 to flash ON for 2S and OFF for 2S continuosly.
LED1 to flash ON for 3S and OFF for 3S continuosly.

ALL starting at the same time, but flashing at their own rate.
You cannot use DELAY as that is a BLOCKING instruction, it stops the loop when ever it is encountered.
If you look in the EXAMPLES section of the IDE, you will find a code called "Blink without Delay" you need to start there.

Hope It helps.. Tom.. :slight_smile:

As everything happens on the same time I would use counter increase every second.
Led1 turn on and off at every tick.
Led2 turn on and off at every second tick.
Led3 turn on and off every third tick.

1s led1 on
2s led1 off, led2 on
3s led1 on, led3 on
4s led1 off, led 2 off
5s led1 on
6s led1 off, led2 on, led3 off
7s led1 on
8s led1 off, led2 off
9s led1 on, led3 on
10s led1 off, led2 on
11s led1 on,
12s led1 off, led2 off, led3 off.

From now everything repeat.
Modulo can be used to detect every second and every third tick to set the leds.

edit, something like this. It compiles, but I don't know if it runs as expected.

int ledPin1 = 2;
int ledPin2 = 4;
int ledPin3 = 7;
int counter = 0;
unsigned long prevTime;
int interval = 1000;
bool tick = false;

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

void loop() {
  if (millis() - prevTime > interval) {
    counter++;
    tick = true;
    prevTime += interval;
  }
  if (tick) {
    digitalWrite (ledPin1, !digitalRead(ledPin1));
    if (counter % 2 == 0) {
      digitalWrite (ledPin2, !digitalRead(ledPin2));
    }
    if (counter % 3 == 0) {
      digitalWrite (ledPin3, !digitalRead(ledPin3));
    }

    tick = false;
    if (counter == 12) {
      counter = 0;
    }
  }
}

I'd do it like this:

#define LED_PIN_1 13  // set the three led pins here
#define LED_PIN_2 12
#define LED_PIN_3 11

#define IN_SYNC YES  // comment out to blink asyncronously

enum PowerState{
  LED_OFF,
  LED_ON
};

class BWOD{
  public:
    BWOD(byte ledPin){_ledPin = ledPin;pinMode(_ledPin, OUTPUT);};
    void begin(int onTime, int offTime, PowerState powerState){
      this->begin(onTime, offTime, powerState, millis());
    };
    void begin(int onTime, int offTime, PowerState powerState, uint32_t syncMillis){
      _onTime = onTime; 
      _offTime = offTime; 
      _powerState = powerState; 
      digitalWrite(_ledPin, static_cast<byte>(_powerState));
      _lastChangeTime = syncMillis;
    };
    void update(){this->update(millis());};
    void update(uint32_t refreshTime){
      if(static_cast<int>(_powerState))
      {
        if(refreshTime - _lastChangeTime > _onTime)
        {
          _powerState = LED_OFF;
          digitalWrite(_ledPin, static_cast<byte>(_powerState));
          _lastChangeTime = millis();
        }
      }
      else
      {
        if(refreshTime - _lastChangeTime > _offTime)
        {
          _powerState = LED_ON;
          digitalWrite(_ledPin, static_cast<byte>(_powerState));
          _lastChangeTime = millis();
        }
      }
    };
      
  private:
    byte _ledPin;
    int _offTime;
    int _onTime;
    uint32_t _lastChangeTime;
    PowerState _powerState;
};

BWOD led1(LED_PIN_1);
BWOD led2(LED_PIN_2);
BWOD led3(LED_PIN_3);

void setup() 
{
#ifndef IN_SYNC
  led1.begin(1000, 1000, LED_ON);  // on-time, off-time, starts ON or OFF
  led2.begin(2000, 2000, LED_ON);
  led3.begin(3000, 3000, LED_ON);
#else
  uint32_t syncTime = millis();
  led1.begin(1000, 1000, LED_ON, syncTime);
  led2.begin(2000, 2000, LED_ON, syncTime);
  led3.begin(3000, 3000, LED_ON, syncTime);
#endif
}

void loop() 
{
#ifndef IN_SYNC
  led1.update();
  led2.update();
  led3.update();
#else
  uint32_t currentMillis = millis();
  led1.update(currentMillis);
  led2.update(currentMillis);
  led3.update(currentMillis);
#endif
}