Newbe multiple loop question

Hi all

i am not a pro in programming but i have a small question.

for my project i need to blink one led.
to make it easy 1000ms on, 1000ms off
now i need to run a second code simultaneously
lets say a 6 led pattern switching every 1500ms

in a usual loop in can let one led blink every second.
if i use delay (like the blink sketch) i cant run a second loop

how can i keep one led blinking while executing a second loop

with kind regards
Robbert Loos

Go read this thread

You can also study blink without delay (or here for the original)

There are many ways to do what you want.

If this is a personal project here's something to learn from.

If it's homework don't turn in the following unless you can explain how it works!

#define NUM_ENTRIES(ARRAY)  (sizeof(ARRAY) / sizeof(ARRAY[0]))

const uint8_t           pinLED      = 13;

const uint8_t           pinLED_1    = 33;
const uint8_t           pinLED_2    = 32;
const uint8_t           pinLED_3    = 31;
const uint8_t           pinLED_4    = 30;
const uint8_t           pinLED_5    = 29;

const uint8_t           LED_OFF     = LOW;
const uint8_t           LED_ON      = HIGH;

const unsigned long     HALF_SECOND =  500UL;
const unsigned long     ONE_SECOND  = 1000UL;

const uint8_t           pinsLEDS[]  = { pinLED_1, pinLED_2, pinLED_3, pinLED_4, pinLED_5 };


class timerT
{
    bool             m_triggered;
    unsigned long    m_tmsTarget;

public:
    timerT(unsigned long tms) : m_triggered(false), m_tmsTarget(millis() + tms)
    {   }

    bool has_expired()
    {
        if ( ! m_triggered )
        {
            m_triggered = ((long)(millis() - m_tmsTarget) >= 0);
        }

        return m_triggered;
    }
};

void setup()
{
    pinMode(pinLED, OUTPUT);
    digitalWrite(pinLED, LED_ON);

    for ( size_t i = NUM_ENTRIES(pinsLEDS); i--; )
    {
        pinMode(pinsLEDS[i], OUTPUT);
    }
}

void loop()
{
    // ... BLINK SINGLE LED ...
    static timerT   timerLED(ONE_SECOND);

    if ( timerLED.has_expired() )
    {
        timerLED = timerT(ONE_SECOND);

        digitalWrite(pinLED, !digitalRead(pinLED));
    }
    

    // ... SEQUENCE 5 LEDS ...
    static timerT   timerLEDS   = timerT(HALF_SECOND);
    static size_t   iLED    = NUM_ENTRIES(pinsLEDS) - 1;

    if ( timerLEDS.has_expired() )
    {
        for ( size_t i = NUM_ENTRIES(pinsLEDS); i--; )
        {
            digitalWrite(pinsLEDS[i], LED_OFF);
        }


        iLED = (iLED + 1) % NUM_ENTRIES(pinsLEDS);
        digitalWrite(pinsLEDS[iLED], LED_ON);

        timerLEDS = timerT(HALF_SECOND);
    }
}

@lloyddean

Your code is at risk with wrapping millls() and unsigned long

Would suggest

timerT(unsigned long tms) : m_triggered(false), m_tmsTarget(millis())
// ...
        m_triggered = (millis() - m_tmsTarget) >= tms;

Instead

thank you very much, really appreciate the info, gonna study tonight

Thanks for the catch, I pulled it from a 8 year demo I did.