Can you time when x increments within a for() loop? (Beginner)

I've tried to implement millis(); like such down below, but each variation failed.

void ledMode(byte mode)
{
    unsigned long currentTime = millis();
    if (currentTime - previousTime >= Interval)
    {
        switch (mode)
        {
        case 1:
            digitalWrite(RLED, HIGH); //Red
            digitalWrite(BLED, LOW);
            digitalWrite(GLED, LOW);
            Serial.println("Red!");
            previousTime = currentTime;
            break;

        case 2:
            digitalWrite(RLED, LOW);  //Green
            digitalWrite(BLED, LOW);
            digitalWrite(GLED, HIGH);
            Serial.println("GREEN!");
            previousTime = currentTime;
            break;

        case 3:
            digitalWrite(RLED, LOW);  //blue
            digitalWrite(BLED, HIGH);
            digitalWrite(GLED, LOW);
            Serial.println("BLUE!");
            previousTime = currentTime;
            break;

        case 4:
            digitalWrite(RLED, HIGH);  //orange
            digitalWrite(BLED, LOW);
            digitalWrite(GLED, HIGH);
            Serial.println("ORANGE!");
            previousTime = currentTime;
            mode = 5;
            break;

        case 5:
            digitalWrite(RLED, LOW);  //teal
            digitalWrite(BLED, HIGH);
            digitalWrite(GLED, HIGH);
            Serial.println("TEAL!");
            previousTime = currentTime;
            break;
        case 6:
            digitalWrite(RLED, HIGH); //purple
            digitalWrite(BLED, HIGH);
            digitalWrite(GLED, LOW);
            Serial.println("PURPLE!");
            previousTime = currentTime;
            break;

        case 7:
            digitalWrite(RLED, HIGH); //white
            digitalWrite(BLED, HIGH);
            digitalWrite(GLED, HIGH);
            Serial.println("WHITE!");
            previousTime = currentTime;
            break;
        }
    }
}

void RGBoff()
{
    digitalWrite(RLED, LOW);
    digitalWrite(BLED, LOW);
    digitalWrite(GLED, LOW);
}

void loop()
{
    button();
   if (count % 2 == 1)
{
    unsigned long currentTime = millis();
    for (byte x = 1; x < 8;)
    {
        ledMode(x);
        if (currentTime - previousTime >= Interval)
        {
            Serial.println(x);
            previousTime = currentTime;
            x++;
        }
    }   
}
}

I'd like x to increment every 1000 milliseconds...

Any help or suggestions would be greatly appreciated! Li-ion Battery

The speed at which the for loop executes in your code snippet is not controlled by millis() and it is not normal to change the value of a for loop control variable within the for loop

If you want a series of actions to take place at intervals then use millis() to time the intervals and let loop() do the looping. No for loop is needed

In answer to your question in the title the answer is no. But I suspect you don’t want to do this. The loop index, here called x increments at the end of the for loop.

You could simply put a delay of the appropriate value in the loop but that would cause blocking code which may or may not be a problem with what you are trying to do.

millis() function is not implementation function in this code, it is the function call.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.