Two sequence using a switch and 8LED

In this picture we practice delays but how to convert it in Millis?. Based on a switch if switch is 0 the sequence is 1=On, 1=Off, 2=On, 2=Off, 3=On, 3=Off, 4=On, 4=Off, 5=On, 5=Off, 6=On, 6=Off, 7=On, 7=Off,8=On, 8=Off; else switch is 1 the sequence is 1&&8=On, 1&&8=Off, 2&&7=On, 2&&7=Off, 3&&6=On, 3&&6=Off, 4&&5=On, 4&&5=Off.... Thank You for imparting your knowledge.

\
// C++ code
//
int buttonState = 0;

void setup()
{
pinMode(10, INPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);
pinMode(2, OUTPUT);
}

void loop()
{
//read the state of the pushbutton
buttonState = digitalRead(10);
//check if pushbutton is pressed if it is in the
//HIGH
if (buttonState == LOW) {
digitalWrite(2,HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(2,LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(4,HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(4,LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(6,HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(6,LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(8,HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(8,LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(3,HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(3,LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(5,HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(5,LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(7,HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(7,LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(9,HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(9,LOW);
} else {
digitalWrite(6,HIGH);
digitalWrite(5,HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(6,LOW);
digitalWrite(5,LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(7,HIGH);
digitalWrite(4,HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(7,LOW);
digitalWrite(4,LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(8,HIGH);
digitalWrite(3,HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(8,LOW);
digitalWrite(3,LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(9,HIGH);
digitalWrite(2,HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(9,LOW);
digitalWrite(2,LOW);
}
}
///

@jvalls1 please do not post photos of code

Read How to get the best out of this forum and follow the instructions to post code using code tags

I'd put the LED pin numbers in an array.

consider

// check multiple buttons and toggle LEDs

enum { Off = HIGH, On = LOW };

byte butPin = A1;

#define Led1    10
#define Led2    11
#define Led3    12
#define Led4    13

byte ledPins [] = { Led1, Led2, Led3, Led4 };

struct Seq_s {
    byte            pin;
    byte            state;
    unsigned long   interval;
};

#define OneSec  1000

Seq_s seq1 [] = {
    { 10, On,  OneSec },
    { 10, Off, OneSec },
    { 11, On,  OneSec },
    { 11, Off, OneSec },
    { 12, On,  OneSec },
    { 12, Off, OneSec },
    { 13, On,  OneSec },
    { 13, Off, OneSec },
    {}
};

Seq_s seq2 [] = {
    { 10, On,  0      },
    { 13, On,  OneSec },
    { 10, Off, 0      },
    { 13, Off, OneSec },

    { 11, On,  0      },
    { 12, On,  OneSec },
    { 11, Off, 0      },
    { 12, Off, OneSec },

    { 11, Off, 0      },
    { 12, On,  OneSec },
    { 11, On,  0      },
    { 12, Off, OneSec },

    { 12, Off, 0      },
    {}
};

int idx = 0;


// -----------------------------------------------------------------------------
void
ledsOff (void)
{
    for (unsigned n = 0; n < sizeof(ledPins); n++)
        digitalWrite (ledPins [n], Off);
    idx = 0;
}

// -----------------------------------------------------------------------------
unsigned long msecLst;
byte          butLst = Off;
Seq_s       *seq     = seq1;

void
loop ()
{

    byte but = digitalRead (butPin);

    if (butLst != but)  {
        butLst = but;

        if (LOW == but)  {
            ledsOff ();

            if (seq == seq1)
                seq = seq2;
            else
                seq = seq1;

            digitalWrite (seq [idx].pin,seq [idx].state);
        }
        delay (10);     // debounce
    }

    unsigned long msec = millis ();

    if ((msec - msecLst) > seq [idx].interval)  {
        msecLst = msec;

        if (0 == seq [++idx].pin)
            idx = 0;

        char s [80];
        sprintf (s, " %s: pin %d, %d", __func__, seq [idx].pin,seq [idx].state);
        Serial.println (s);

        digitalWrite (seq [idx].pin,seq [idx].state);
    }
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

    pinMode (butPin, INPUT_PULLUP);
    butLst = digitalRead (butPin);

    for (unsigned n = 0; n < sizeof(ledPins); n++)  {
        digitalWrite (ledPins [n], Off);
        pinMode      (ledPins [n], OUTPUT);
    }
}

Hello jvalls1,
try this sketch. I´ve made a simple sketch using a timer for the steps and inside the timer a counter is running to address the leds.

// https://forum.arduino.cc/t/two-sequence-using-a-switch-and-8led/876878/4
// https://www.arduino.cc/reference/en/
//---------- Declarations
const byte leds[] {2, 3, 4, 5, 6, 7, 8, 9, 10};
const byte pushButton {A0};
struct TIMER {
  unsigned long stamp;
  unsigned long duration;
} timer {0, 1000};

void setup() { // put your setnumber code here, to run once:
// test wiring
  for (auto led : leds) pinMode(led, OUTPUT), digitalWrite(led, HIGH), delay(100), digitalWrite(led, LOW);
  pinMode (pushButton, INPUT_PULLUP);
}
void loop() {  // put your main code here, to run repeatedly:
  if (millis() - timer.stamp >= timer.duration) {
    timer.stamp = millis();
    static  byte number;
    for (auto led : leds) digitalWrite(led, LOW);
    digitalWrite(leds[number], HIGH);
    if (!digitalRead(pushButton)) digitalWrite(leds[sizeof(leds) - number], HIGH);
    number++;
    number = number % sizeof(leds);
  }
}

What du you think?

i don't believe it generates the sequences correctly, nor under control of the button

not sure generating the sequence in logic is the best approach.

code is dense. ever hear of white space? ever have your code reviewed?

Thank you so very informative but the LED were still and it does not turn off.. But thank you very much

\
enum { Off = HIGH, On = LOW };

byte butPin = A1;

#define Led1 9
#define Led2 8
#define Led3 7
#define Led4 6
#define Led5 5
#define Led6 4
#define Led7 3
#define Led8 2

byte ledPins [] = { Led1, Led2, Led3, Led4, Led5, Led6, Led7, Led8 };

struct Seq_s {
byte pin;
byte state;
unsigned long interval;
};

#define OneSec 1000

Seq_s seq1 [] = {
{ 9, On, OneSec },
{ 9, Off, OneSec },
{ 8, On, OneSec },
{ 8, Off, OneSec },
{ 7, On, OneSec },
{ 7, Off, OneSec },
{ 6, On, OneSec },
{ 6, Off, OneSec },
{ 5, On, OneSec },
{ 5, Off, OneSec },
{ 4, On, OneSec },
{ 4, Off, OneSec },
{ 3, On, OneSec },
{ 3, Off, OneSec },
{ 2, On, OneSec },
{ 2, Off, OneSec },
{}
};

Seq_s seq2 [] = {
{ 9, On, 0 },
{ 2, On, OneSec },
{ 9, Off, 0 },
{ 2, Off, OneSec },

{ 8, On,  0      },
{ 3, On,  OneSec },
{ 8, Off, 0      },
{ 3, Off, OneSec },

{ 7, Off, 0      },
{ 4, On,  OneSec },
{ 7, On,  0      },
{ 4, Off, OneSec },
  
{ 6, Off, 0      },
{ 5, On,  OneSec },
{ 6, On,  0      },
{ 5, Off, OneSec },
  
{ 5, Off, 0      },
{}

};

int idx = 0;

// -----------------------------------------------------------------------------
void
ledsOff (void)
{
for (unsigned n = 0; n < sizeof(ledPins); n++)
digitalWrite (ledPins [n], Off);
idx = 0;
}

// -----------------------------------------------------------------------------
unsigned long msecLst;
byte butLst = Off;
Seq_s *seq = seq1;

void
loop ()
{

byte but = digitalRead (butPin);

if (butLst != but)  {
    butLst = but;

    if (LOW == but)  {
        ledsOff ();

        if (seq == seq1)
            seq = seq2;
        else
            seq = seq1;

        digitalWrite (seq [idx].pin,seq [idx].state);
    }
    delay (10);     // debounce
}

unsigned long msec = millis ();

if ((msec - msecLst) > seq [idx].interval)  {
    msecLst = msec;

    if (0 == seq [++idx].pin)
        idx = 0;

    char s [80];
    sprintf (s, " %s: pin %d, %d", __func__, seq [idx].pin,seq [idx].state);
    Serial.println (s);

    digitalWrite (seq [idx].pin,seq [idx].state);
}

}

// -----------------------------------------------------------------------------
void
setup ()
{
Serial.begin (9600);

pinMode (butPin, INPUT_PULLUP);
butLst = digitalRead (butPin);

for (unsigned n = 0; n < sizeof(ledPins); n++)  {
    digitalWrite (ledPins [n], Off);
    pinMode      (ledPins [n], OUTPUT);
}

}
///

@jvalls1 I see that you decided to ignore the advice to use code tags when posting code

Why ?

The easier you make it to read and copy your code the more likely it is that you will get help

hi guys i want to buy one like this can anyone help me do it plz?

I think i copy the code just below the picture Sir

The Code was under the picture if you notice Sir. :slight_smile:

Yes, but it was not posted in code tags, which was my point. As it happens, due to the layout of your code the forum software has interpreted part of it as code, but not all of it

use </>