Can time be used in a for() loop instead of delay() ?

hello,
i am trying to get rid of using delay() and to enhance my programming by using arrays, so connected 12 LEDs directly to the arduino of course with resistors, and i made a small sketch to try to control them in an advanced way, so in my sketch i use for() loop to go threw the LEDs and inside i used the timing instead of delay() but things doesn't work well, instead the first LED connected to pin 2 lights up only, LEDs are fine all hardware things are fine, i checked each one, but it seems a matter of programming, so i need please to know if it is possible to use the time inside a for loop if not what shall be done ? because using the for loop inside time is doesn't work as it is needed

here is the sketch:

//RED LEDs
byte Pin2 = 2;
byte Pin3 = 3;
byte Pin4 = 4;
byte Pin5 = 5;

//GREEN LEDs
byte Pin6 = 6;
byte Pin7 = 7;
byte Pin8 = 8;
byte Pin9 = 9;

//YELLOW LEDs
byte Pin10 = 10;
byte Pin11 = 11;
byte Pin12 = 12;
byte Pin13 = 13;

//LED Arrays
byte redLEDArray[4] = { Pin2, Pin3, Pin4, Pin5 };
byte blueLEDArray[4] = { Pin6, Pin7, Pin8, Pin9 };
byte yellowLEDArray[4] = { Pin10, Pin11, Pin12, Pin13 };

//Time Variables
unsigned long previousTime = 0;
unsigned long currentTime;
const long gap = 500;

void setup(){
    
    Serial.begin(9600);
    for( int i=2; i<14; i++ ){
      
      pinMode(i, OUTPUT);
      
    }

}

void loop(){

    currentTime = millis();
    
    for( int REDpin = 0; REDpin < 4; REDpin++ ){
          
          if( currentTime - previousTime >= gap ){
        
            previousTime = currentTime;
            digitalWrite(redLEDArray[REDpin], HIGH);
            //delay(100);
            //Serial.println(redLEDArray[REDpin]);
            Serial.println(previousTime);
            //Serial.println(currentTime);
          }
          
          
      }
      
      
      /*if( REDpin == 3 ){
          
          for( int BLUEpin=0; BLUEpin<4; BLUEpin++ ){
    
            digitalWrite(blueLEDArray[BLUEpin], HIGH);
            
          }
      }*/
}

you are making some unorthodox choices with naming and sequencing those arrays...

byte redLED[4] = {2, 3, 4, 5};
byte blueLED[4] = {6, 7, 8, 9};
byte yellowLED[4] = {10, 11, 12, 13};

//Time Variables
unsigned long previousTime = 0;
unsigned long currentTime;
const long gap = 500;

void setup() 
{
  Serial.begin(9600);
  for ( int i = 0; i < 4; i++ ) 
  {
    pinMode(redLED[i], OUTPUT);
    pinMode(blueLED[i], OUTPUT);
    pinMode(yellowLED[i], OUTPUT);
  }
}

void loop() 
{
 
}
            digitalWrite(redLEDArray[REDpin], HIGH);

Nice, but this only ever turns the LEDs on. Where do you write them LOW to turn them off?

Think of it this way:

  • The loop() runs thousands of times per second

  • Every time, it looks at the millis() to find the current time

  • If it's time to do something, then do that something

  • record the time when you did the something- If it's time to do something else, do that too

In practice, it never does two things in one iteration of the loop, but it must check the time for everything that it might possibly do.

BulldogLowell:
you are making some unorthodox choices with naming and sequencing those arrays...

byte redLED[4] = {2, 3, 4, 5};

byte blueLED[4] = {6, 7, 8, 9};
byte yellowLED[4] = {10, 11, 12, 13};

//Time Variables
unsigned long previousTime = 0;
unsigned long currentTime;
const long gap = 500;

void setup()
{
  Serial.begin(9600);
  for ( int i = 0; i < 4; i++ )
  {
    pinMode(redLED[i], OUTPUT);
    pinMode(blueLED[i], OUTPUT);
    pinMode(yellowLED[i], OUTPUT);
  }
}

void loop()
{

}

well thank you, your method seems better :slight_smile:

MorganS:

            digitalWrite(redLEDArray[REDpin], HIGH);

Nice, but this only ever turns the LEDs on. Where do you write them LOW to turn them off?

well about that, i just want to turn the LEDs on but with delay between each one, but in fact just 1 lighting up and the others not and the loop is stuck on the pin 2 only when i Serial.print the value

MorganS:
Think of it this way:

  • The loop() runs thousands of times per second

  • Every time, it looks at the millis() to find the current time

  • If it's time to do something, then do that something

  • record the time when you did the something- If it's time to do something else, do that too

In practice, it never does two things in one iteration of the loop, but it must check the time for everything that it might possibly do.

so the loop() does 1 thing at a time line by line like we know, but it does that very fast that it looks like everything is simultaneously and because we do not have a delay, it just continue looping until it is time for something to happen, then it happens and then something else happens when it is the time, but the main loop keeps looping right ?

When currentTime - previousTime >= gap is true you reset previousTime, so it only evaluates to true the first time through the for loop and only the first LED is turned on. There is no need to use a for loop, since you know that you'll only be turning one LED on. The following should turn on all the red LEDs with the proper timming:

int ledIdx=0;
void loop(){

  currentTime = millis();

    if( (currentTime - previousTime >= gap) && (ledIdx < sizeof(redLEDArray)) ) {
      previousTime = currentTime;
      Serial.println(ledIdx);
      digitalWrite(redLEDArray[ledIdx++], HIGH);

      //delay(100);
      //Serial.println(redLEDArray[REDpin]);
      Serial.println(previousTime);
      Serial.println(currentTime);
    }

}

You may want to rethink that way you are storing the pin numbers, maybe putting them in one array or using a two dimensional array. This is a good programming exercise.

The demo Several Things at a Time controls 3 LEDs with millis().

...R

i do not understand why it just load the first LED, why it is stuck ?! i need to know why ?
the time is not reset to 0, it reset to the new value
the for loop starts, inside the time checks if we hit the gap needed and then digitalWrite the first LED then loop continues to the other LED

Robin2:
The demo Several Things at a Time controls 3 LEDs with millis().

...R

thanks Robin, yes i remember i saved it on my desktop i will check it again