Question Details

No question body available.

Tags

loops arduino arduino-uno

Answers (2)

January 26, 2026 Score: 0 Rep: 50,022 Quality: Medium Completeness: 60%

The Arduino environment doesn't provide a way to have independent tasks running simultaneously. When your program uses delay, the processor won't do anything else but wait. If you want to do multiple things at once, you have to manage it yourself.

So first let's blink just the blue LED without calling delay(). We'll need some variables to keep track of the state.

const unsigned long bluTimes[2] = { 1000, 100 };
unsigned long bluExpiry = 0;
bool bluOn = false;

void loop() { unsigned long now = millis(); if (now >= bluExpiry) { bluExpiry = now + bluTimes[bluOn]; bluOn = !bluOn ; digitalWrite(bluLED, bluOn ? HIGH : LOW); } }

The millis() function tells you how many milliseconds have passed since the processor turned on. Each time through the loop, we check whether the current time has reached the expiration time for the blue LED. If so, then we compute the next expiration time, flip the state, and set the pin.

Most of the time, there will be nothing to do, and the loop will return without having done anything. But it's immediately called again and again. But since it's not waiting in a delay, it's available to manage other tasks, like blinking the other two LEDs.

const unsigned long bluTimes[2] = { 1000, 100 };
unsigned long bluExpiry = 0;
bool bluOn = false;
const unsigned long grnTimes[2] = { 500, 500 };
unsigned long grnExpiry = 0;
bool grnOn = false;
const unsigned long redTimes[2] = { 100, 100 };
unsigned long redExpiry = 0;
bool redOn = false;

void loop() { unsigned long now = millis(); if (now >= bluExpiry) { bluExpiry = now + bluTimes[bluOn]; bluOn = !bluOn ; digitalWrite(bluLED, bluOn ? HIGH : LOW); } if (now >= grnExpiry) { grnExpiry = now + grnTimes[grnOn]; grnOn = !grnOn ; digitalWrite(grnLED, grnOn ? HIGH : LOW); } if (now >= redExpiry) { redExpiry = now + redTimes[redOn]; redOn = !redOn ; digitalWrite(redLED, redOn ? HIGH : LOW); } }

Triggers don't have to be based just on time. For example, you could have other code that responds to a button press, even as the LEDs continue to blink at their independent rates.

January 26, 2026 Score: 0 Rep: 449 Quality: Low Completeness: 80%

In a single core CPU of the Atmega 328p processor, we only have a single core with no support for hardware threads, which is one popular way to implement concurrent software in traditional x86 methods. While it's also possible to do software threading, there's a bunch easier way without importing external libraries.

The key problem with your current implementation is delay() is a blocking call and will not allow you to have "multiple loops" as you have described.

You can simply structure your code to be single a loop that calls delay() once per iteration and then set your led state accordingly for every tick. The math logic is set based on your the delays you specified in your example: blue will be on for 10 ticks and then off for 1 while green and red both have 50% duty cycle.

void loop() {
  int ticks = 0;

if ( ticks % 11 < 10 ) digitalWrite(bluLED, HIGH); else digitalWrite(bluLED, LOW);

if ( ticks % 10 < 5 ) digitalWrite(greenLED, HIGH); else digitalWrite(greenLED, LOW);

if ( ticks % 2 == 0 ) digitalWrite(redLED, HIGH); else digitalWrite(redLED, LOW);

delay(100); ticks++; }