8 LED Sequence using Millis

Hello i need help, i have a project compose of 1 switch and 8 LED using Millis. The light sequence is it needs to be on from LED1-8 then off from LED8-1. I have a code here showing the LEDS turning on from 1-8, but i dont know how to turn it off from 8-1.

Here's my code:
\ \ \
unsigned long ms_from_start =0;
unsigned long ms_previous_read_LED1 = 0;
unsigned long LED1_interval=0000;
unsigned long ms_previous_read_LED2 = 0;
unsigned long LED2_interval=1000;
unsigned long ms_previous_read_LED3 = 0;
unsigned long LED3_interval=2000;
unsigned long ms_previous_read_LED4 = 0;
unsigned long LED4_interval=3000;
unsigned long ms_previous_read_LED5 = 0;
unsigned long LED5_interval=4000;
unsigned long ms_previous_read_LED6= 0;
unsigned long LED6_interval=5000;
unsigned long ms_previous_read_LED7 = 0;
unsigned long LED7_interval=6000;
unsigned long ms_previous_read_LED8 = 0;
unsigned long LED8_interval=7000;

#define LED1 8
#define LED2 7
#define LED3 6
#define LED4 5
#define LED5 4
#define LED6 3
#define LED7 2
#define LED8 1

int LED1_state=1;
int LED2_state=2;
int LED3_state=3;
int LED4_state=4;
int LED5_state=5;
int LED6_state=6;
int LED7_state=7;
int LED8_state=8;

void setup() {

pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
pinMode(LED8, OUTPUT);
}
void loop() {
ms_from_start = millis();

if (ms_from_start-ms_previous_read_LED1> LED1_interval){
ms_previous_read_LED1=ms_from_start;
if (LED1_state==1) LED1_state=1; else LED1_state=0;
digitalWrite(LED1,LED1_state);
}

if (ms_from_start-ms_previous_read_LED2> LED2_interval){
ms_previous_read_LED2=ms_from_start;
if (LED2_state==2) LED2_state=2; else LED2_state=0;
digitalWrite(LED2,LED2_state);
}
if (ms_from_start-ms_previous_read_LED3> LED3_interval){
ms_previous_read_LED3=ms_from_start;
if (LED3_state==3) LED3_state=3; else LED3_state=0;
digitalWrite(LED3,LED3_state);
}
if (ms_from_start-ms_previous_read_LED4> LED4_interval){
ms_previous_read_LED4=ms_from_start;
if (LED4_state==4) LED4_state=4; else LED4_state=0;
digitalWrite(LED4,LED4_state);
}
if (ms_from_start-ms_previous_read_LED5> LED5_interval){
ms_previous_read_LED2=ms_from_start;
if (LED5_state==5) LED5_state=5; else LED5_state=0;
digitalWrite(LED5,LED5_state);
}
if (ms_from_start-ms_previous_read_LED6> LED6_interval){
ms_previous_read_LED6=ms_from_start;
if (LED6_state==6) LED6_state=6; else LED6_state=0;
digitalWrite(LED6,LED6_state);
}
if (ms_from_start-ms_previous_read_LED7> LED7_interval){
ms_previous_read_LED7=ms_from_start;
if (LED7_state==7) LED7_state=7; else LED7_state=0;
digitalWrite(LED7,LED7_state);
}
if (ms_from_start-ms_previous_read_LED8> LED8_interval){
ms_previous_read_LED8=ms_from_start;
if (LED8_state==8) LED8_state=8; else LED8_state=1;
digitalWrite(LED8,LED8_state);
}
}
\ \ \

Hi, welcome to the forum.

Could you edit your post and put the sketch between lines with three backslash-single-quotes ?

```
Your sketch
```

@marchkid, your topic was moved to a more suitable location.

You nearly got your code tags right, it's 3 backticks, not 3 backslashes.

Hello
well, I gues the usage of struct{} and arrays would be a good way to find proper solution.

consider


#undef MyHW
#ifdef MyHW
byte ledPins [] = { 10, 11, 12, 13 };
#else
byte ledPins [] = { 8, 7, 6, 5, 4, 3, 2, 1 };
#endif

#define N_LEDS  (int)sizeof(ledPins)

enum { Off = HIGH, On = LOW };

#define Interval    1000
unsigned long msecLst = 0;

int idx = 0;
int dir = 1;

void
loop (void)
{
    unsigned long msec = millis ();

    if ( (msec - msecLst) > Interval)  {
        msecLst = msec;

        digitalWrite (ledPins [idx], dir > 0 ? On : Off);

        idx += dir;

        if (0 > idx)  {
            dir = 1;
            idx = 0;
        }
        else if (N_LEDS <= idx)  {
            dir = -1;
            idx = N_LEDS -1;
        }
    }
}

void
setup (void)
{
    Serial.begin (9600);
    for (unsigned n = 0; n < N_LEDS; n++)  {
        digitalWrite (ledPins [n], Off);
        pinMode    (ledPins [n], OUTPUT);
    }
}

1 Like