Light Sequence

Hello, I was wondering if you could help me out with a little bit of code... I am trying to get 7 LEDS to light up sequentially one after the other every second or so, and then sequentially back down again. I had originally achieved this by using the delay() function, like this...

  for(int i=0; i<7; i++){
    digitalWrite(ledPins[i], HIGH);
    delay(delayTime);
  }
  
  for(int i=6; i>0; i--){
    digitalWrite(ledPins[i], LOW);
    delay(delayTime);
  }

I want to be able to do this without the delay function and if possible, within a For loop. I thought I may be able to use the 'Blink Without Delay' sketch, but am having a few problems. This is kind of along the lines of what I tried...

currentTime = millis();

  if (currentTime - lastTime > delayTime){
    for (int i=1; i<7; i++){
       digitalWrite(ledPins[i], HIGH);
       lastTime = currentTime;
    }
  }

This doesn't work as I believe once in the For loop, the 'if' doesn't get checked again until the for loop has completed. Is this a true statement and am I on the right tracks? I have tried several different ways along these lines to no avail... Am I even close? Is this feasible?

Any guidance would be much appreciated...

EDIT: Posted slightly wrong code...

turning the lights on:

if (Light< 7){

if (currentTime - lastTime > delayTime){
ViewSetVisible(Light,1);
Light++;
lastTime = currentTime;
}

}

Something like that may works with up an down led lighting.

char cpt=0;     // to count the led
char way=1;    // from 0->6 or 6->0
currentTime = millis();

  if (currentTime - lastTime > delayTime)
  {
    for (int i=0; i<7; i++)  ViewSetVisible(Light[i],0);    // switch off the lights        
       
          
    ViewSetVisible(Light[cpt], 1);   // Light current Light

    if (way==1)     // we count from 0->6
    {
           cpt++;     // inc the index of led

           if (cpt>6)       // no more led ? 
           {  
             cpt=6;
             way=0;     //  go back
           }
       }
       else   // reverse way 6->0
       {
           cpt--;   // decrease index

           if (cpt<0)  // minus index mean no more led
           {   
             cpt = 0;
             way=1;     // ok lets go up from 0
           }
       }

  }

If the sequence want want is (lets with 4 leds)

0000
1000
1100
1110
1111
1110
1100
1000
0000

you must change int the line 1 by way !

ViewSetVisible(Light[cpt], way);

and remove the for(....) line.

What you are doing in the Blink Without Delay example is basically defining a state machine. Based on the time, the program is in some state. So, at intervals, like every time through loop, you need to see if it is time to change states. If so, change the state.

Then, completely independent of the time, based solely on state, do something - light the first 3 LEDs or light the first 7 LEDs or light the last 3 LEDs or...

@ Grag38...

Thanks for the response... As you can see in my original post I pasted one line of code incorrectly (ViewSetVisible isn't even Arduino language... I'm obvioulsy trying to learn too much at once!!). Anyway, in your example, where is lastTime being reset? What makes this code wait a second or so before lighting the second led, then the 3rd etc etc...

What I am trying to do is...

1000 (wait a second or two)
1100 (wait a second or two)
1110 (wait a second or two)
1111 (wait a second or two)
1110 (wait a second or two)...

Thanks again.

Program is missing
last_time =current_time;
somewhere in the if condition, before the int i = 0 to 7 statement.

I give you not all of the code, just the part that you need, you must add some lines !!!

put :

in your loop()

currentTime = millis();

and put :

lastTime = currentTime;

in the body of :

if (currentTime - lastTime > delayTime)
{

don't forget to declar these variable as long;

If you want 1 second of delay, declare it as :

long delayTime=1000;

I would go further and make All the 'time' related
unsigned long types
so that if your program ever happens to run for 49+ days, when the counter rolls over from
FFFF FFFF to 0, you will still have valid results.
for example, 0000 0010 - FFFF FFF0 (later time - earlier time) will result in 0000 0020.

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

enum
{
      pinLED1 = 0   // SET YOUR PIN NUMBER HERE
    , pinLED2 = 0   // SET YOUR PIN NUMBER HERE
    , pinLED3 = 0   // SET YOUR PIN NUMBER HERE
    , pinLED4 = 0   // SET YOUR PIN NUMBER HERE
    , pinLED5 = 0   // SET YOUR PIN NUMBER HERE
    , pinLED6 = 0   // SET YOUR PIN NUMBER HERE
    , pinLED7 = 0   // SET YOUR PIN NUMBER HERE
};

const unsigned long     k_DELAY         = 1000;

uint8_t                 g_pinsLED[]     = { pinLED1, pinLED2, pinLED3, pinLED4, pinLED5, pinLED6, pinLED7 };

bool has_time_elapsed(unsigned long& time_last, unsigned long dt)
{
    return (((millis() - time_last) > dt) ? true : false);
}

void loop()
{
    static int8_t           state_number        =  0;
    static int8_t           state_step_value    = -1;
    static unsigned long    last_update_time    = millis();
    
    if ( has_time_elapsed(last_update_time, k_DELAY) )
    {
        // remeber the time of this update
        last_update_time = millis();
        
        // update the LED's
        for ( int i = 0; i < SIZEOF_ARRAY(g_pinsLED); i++ )
        {
            // turn ON LED's below 'state_number' and those at or above OFF 
            digitalWrite(g_pinsLED[i], ((i >= state_number) ? LOW : HIGH));
        }

        // if state_number is at its first state or one past the number of LED's NEGATE state_step_value
        if ( (state_number == 0) || (state_number >= SIZEOF_ARRAY(g_pinsLED)) )
        {
            // NEGATE state_step_value's direction:
            //  1   forward
            // -1   reverse
            state_step_value = -state_step_value;
        }
        
        // transition to next state
        state_number += state_step_value;
    }
}

void setup()
{
    for ( int i = 0; i < SIZEOF_ARRAY(g_pinsLED); i++ )
    {
        pinMode(g_pinsLED[i], OUTPUT);
    }
}

How about sprinkling in a few comments? Cryptic C code is hard to follow.

Code above modified from original posting with possibly more instructive names and as minimal comments as the current pain level permits.

Sorry but neck injuries are making it difficult to think at the moment!

For some reason it's easier to write code then document properly.

enum
{
      pinLED1 = 0   // SET PIN NUMBER
    , pinLED2 = 0   // SET PIN NUMBER
    , pinLED3 = 0   // SET PIN NUMBER
    , pinLED4 = 0   // SET PIN NUMBER
    , pinLED5 = 0   // SET PIN NUMBER
    , pinLED6 = 0   // SET PIN NUMBER
    , pinLED7 = 0   // SET PIN NUMBER
};

Kind of hard to distinguish one pin from another when they all have the same value. Of course, if the point was to have 7 names for the same pin, I guess this is one way to do it. A strange way...

Not to be a smart ass but what DID the comment say to the right? "SET PIN NUMBER"!
This was to imply that the user is set these to their own values!

I seem to recall a similar problem here:

My post near the bottom (at present) shows how you can light a sequence of LEDs without a delay.