Simultaneously led blinking

i am wondering how to create an aurdino code where an led blinks every half second forever, while another led blinks 5 times every seconds, then stops. how can i do these simultaneously?

School assignment?

If you have an Arduino UNO/NANO/MEGA board, you can accomplish the task using the millis() function.

You have to write code which is doing the blinking on one LED while blinking the other LED in a different interval.

Start with the Example from the IDE "Blink Without Delay"

Hello anonymous_232665

Welcome to the world's best Arduino forum ever.

Consider this example for your own sketch design studies.

//https://forum.arduino.cc/t/simultaneously-led-blinking/1214713
//https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
#define ProjectName "Simultaneously led blinking"
#define NotesOnRelease "Arduino MEGA tested"
enum TimerControl {Halt, Run};
enum Timer {One, Two};
enum OnOff {Off, On};
// make variables
constexpr uint8_t LedPins[] {9, 12};
constexpr uint8_t NumberOfBlinks {7};
constexpr uint32_t Interval[] {1000, 1000};
constexpr uint32_t SwitchedOnTime[] {50, 50};
// make structures
struct LEDBLINKS
{
  uint8_t name;
  uint8_t pin;
  uint32_t interval[2];
  uint8_t control;
  uint32_t now;
  void run(uint32_t currentMillis)
  {
    if (currentMillis - now >= interval[digitalRead(pin)] and control)
    {
      now = currentMillis;
      digitalWrite(pin, digitalRead(pin) ? LOW : HIGH);
      if (name == One) control--;
    }
  }
};
LEDBLINKS ledBlinks[]
{
  {One, LedPins[One], Interval[One] - SwitchedOnTime[One], SwitchedOnTime[One] , NumberOfBlinks * 2, 0},
  {Two, LedPins[Two], Interval[Two] - SwitchedOnTime[Two], SwitchedOnTime[Two]  , Run, 0},
};
// make support
void heartBeat(const uint8_t LedPin, uint32_t currentMillis)
{
  static bool setUp = false;
  if (setUp == false) pinMode (LedPin, OUTPUT), setUp = true;
  digitalWrite(LedPin, (currentMillis / 500) % 2);
}
// make application
void setup()
{
  Serial.begin(115200);
  Serial.print("Source: "), Serial.println(__FILE__);
  Serial.print(ProjectName), Serial.print(" - "), Serial.println(NotesOnRelease);
  Serial.print("led test");
  for (auto LedPin : LedPins)
  {
    Serial.print(" . ");
    pinMode(LedPin, OUTPUT);
    digitalWrite(LedPin, HIGH);
    delay(1000);
    digitalWrite(LedPin, LOW);
    delay(1000);

  }
  Serial.println("done\n");
  Serial.println(" =-> and off we go\n");
}
void loop()
{
  uint32_t currentMillis = millis();
  heartBeat(LED_BUILTIN, currentMillis);
  for (auto &ledBlink : ledBlinks) ledBlink.run(currentMillis);
}

Have a nice day and enjoy coding in C++.

@anonymous_232665

Sketch:

unsigned long present1Millis = millis();  //record current time
unsigned long present2Millis = millis();  //record current time
#define LED1 13
#define LED2 10
byte blinkCounter = 0;
bool flag = false;
int led1State = LOW;
int led2State = LOW;

void setup()
{
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  digitalWrite(LED1, led1State);   //LED1 is off
  digitalWrite(LED2, led2State);
}

void loop()
{
  if (millis() - present1Millis >= 250) //testing
  {
    led1State = !led1State;
    digitalWrite(LED1, led1State);
    present1Millis = millis();
  }

  if (flag == false)
  {
    if (millis() - present2Millis >= 100) //testing
    {
      led2State = !led2State;
      digitalWrite(LED2, led2State);
      present2Millis = millis();
      blinkCounter++;
      if (blinkCounter == 10)
      {
        blinkCounter = 0;
        flag = true;
      }
    }
  }
}

Did you compile your sketch -- I have some errors?

It´s fine for me.

Which Arduino you have used?

Arduino MEGA tested

Sketch uses 3508 bytes (1%) of program storage space. Maximum is 253952 bytes.
Global variables use 469 bytes (5%) of dynamic memory, leaving 7723 bytes for local variables. Maximum is 8192 bytes.

This time, the sketch is compiled/Upload fine in my UNO; however, both LEDs are blinking indefenitely at their respective rates. Short intervaled LED should stop after making five blinks.

Configure it:

constexpr uint8_t LedPins[] {9, 12};
constexpr uint8_t NumberOfBlinks {7};
constexpr uint32_t Interval[] {1000, 1000};
constexpr uint32_t SwitchedOnTime[] {50, 50};

Code dump pileon... just using the Uno/Nano...

#define LED_BUILTIN 13 // for clarity
#define FIVETIM 5    // "blink" five times
#define FIVESEC 5000 // five seconds timer
#define HALFsec 500  // half second blink

long oldTimerHALFsec = 0; // HALF second timer
long oldTimerFIVEsec = 0; // FIVE seconds timer

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  if (millis() - oldTimerHALFsec > HALFsec) {
    oldTimerHALFsec = millis();
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
    if (digitalRead(LED_BUILTIN))
      Serial.print("ON");
    else  Serial.println(" OFF");
  }
  if (millis() - oldTimerFIVEsec > FIVESEC) {
    oldTimerFIVEsec = millis();
    for (int i = 0; i < FIVETIM; i++) {
      Serial.print(" blink_");
      Serial.print(i + 1); // 1, 2, 3, 4, 5
      delay(50);
    }
    Serial.println();
  }
}

If that's what we're doing... Which version does not drift? And how can that drift be avoided?

The one using cesium.