stuck with my program

Hello everyone, im at a point in my programming where im stuck, the basis for the program is im building a traffic adviser for my truck, (the amber type that tells you left or right type of deal), it has 4 push buttons, button 1 activates the “warning mode” which is a fast and randomish pattern, button 2 activates a move left pattern, button 3 activates a go around pattern, and button 4 activates a move right pattern.

i have gotten each pattern to work with the assigned button, but the for loop only completes 1 cycle then i have to push the button again to get it to play again.

how do i get it to continuously play till either the button is pushed again to stop the pattern or another button is pushed to change the pattern?

int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 1000;

int button1value = 0;
int button2value = 0;
int button3value = 0;
int button4value = 0;

int button1 = A5;
int button2 = A4;
int button3 = A3;
int button4 = A2;

boolean last1 = LOW;
boolean last2 = LOW;
boolean last3 = LOW;
boolean last4 = LOW;

boolean current1 = LOW;
boolean current2 = LOW;
boolean current3 = LOW;
boolean current4 = LOW;

//----------------------------------------------------------------------------

void setup() {
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);

  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);

  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);

  pinMode(5, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(2, OUTPUT);

  int ledPins[] = {11, 10, 9, 8, 7, 6, 5, 3, 2};

  for (int i = 0; i <= 8; i++) {
    digitalWrite(ledPins[i], LOW); // turns all lights off
  }
}
boolean debounce(boolean last1) {
  boolean current1 = analogRead(button1);
  if (last1 != current1) {
    delay(5);
    current1 = analogRead(button1);
  }
  return current1;
}
boolean debounce2(boolean last2) {
  boolean current2 = analogRead(button2);
  if (last2 != current2) {
    delay(5);
    current2 = analogRead(button2);
  }
  return current2;
}
boolean debounce3(boolean last3) {
  boolean current3 = analogRead(button3);
  if (last3 != current3) {
    delay(5);
    current3 = analogRead(button3);
  }
  return current3;
}
boolean debounce4(boolean last4) {
  boolean current4 = analogRead(button4);
  if (last4 != current4) {
    delay(5);
    current4 = analogRead(button4);
  }
  return current4;
}

//------------------------------------------------------------------------

void loop() {
  int ledPins[] = {11, 10, 9, 8, 7, 6, 5, 3, 2}; //table with all leds
  int ledPinscustom[] = {11, 10, 8, 7, 5, 3, 2}; //table with limited leds
  int ledPinscustom2[] = {10, 9, 8, 7, 6, 5, 3}; //table with limited leds

  current1 = debounce(last1);
  if (last1 == LOW && current1 == HIGH)
  {
    //center 2 on outer 3 flash with center of 3 on.
    for (int i = 0; i <= 8; i++) {
      // digitalWrite(ledPins[i], LOW);
      for (int j = 0; j <= 6; j++) {
        digitalWrite(ledPins[2], LOW);
        digitalWrite(ledPins[6], LOW);
        digitalWrite(ledPinscustom[j], HIGH);
      }
      delay(150);
      for (int k = 0; k <= 6; k++) {
        digitalWrite(ledPins[0], LOW);
        digitalWrite(ledPins[8], LOW);
        digitalWrite(ledPinscustom2[k], HIGH);
      }
      delay(150);
    } //end for loop

    //Turns off all leds
    for (int i = 0; i <= 8; i++) {
      digitalWrite(ledPins[i], LOW);
    }
    delay(150);// end for loop

    //strobe, out to in, with 1 each side lit.
    for (int i = 0; i <= 5; i++) {
      for (int i = 0; i <= 4; i++) {
        digitalWrite(ledPins[i], HIGH);
        digitalWrite(ledPins[8 - i], HIGH);
        delay(50);
        digitalWrite(ledPins[i], LOW);
        digitalWrite(ledPins[8 - i], LOW);
        delay(50);
      }
    }//end for loop

    //Turns off all leds
    for (int i = 0; i <= 8; i++) {
      digitalWrite(ledPins[i], LOW);
    }
    delay(150);// end for loop

    //half and half fast
    for (int i = 0; i <= 8; i++) {
      for (int i = 0; i <= 5; i++) {
        digitalWrite(ledPins[i], HIGH);
      }
      for (int i = 5; i <= 8; i++) {
        digitalWrite(ledPins[i], LOW);
      }
      delay(100);
      for (int i = 0; i <= 3; i++) {
        digitalWrite(ledPins[i], LOW);
      }
      for (int i = 5; i <= 8; i++) {
        digitalWrite(ledPins[i], HIGH);
      }
      delay(100);
    }//end for loop

    //Turns off all leds
    for (int i = 0; i <= 8; i++) {
      digitalWrite(ledPins[i], LOW); //Turns on LED #i each time this runs
    }
    delay(150);// end for loop

    //LEDs strobe, center out, 1 each side lit.
    for (int i = 0; i <= 5; i++) {
      for (int i = 5; i >= 0; i--) {
        digitalWrite(ledPins[i ], HIGH); //Turns on LED #i each time this runs
        digitalWrite(ledPins[8 - i], HIGH); //Turns on LED #i each time this runs
        delay(50);
        digitalWrite(ledPins[i ], LOW); //Turns on LED #i each time this runs
        digitalWrite(ledPins[8 - i], LOW); //Turns on LED #i each time this runs
        delay(50);
      }
    } //end for loop
  }
  last1 = current1;


  current2 = debounce2(last2);
  if (last2 == LOW && current2 == HIGH)
  {
    //slow arrow left to right
    for (int i = 0; i <= 8; i++) {
      digitalWrite(ledPins[i], HIGH);
      delay(500);
    }
    for (int i = 0; i <= 8; i++) {
      digitalWrite(ledPins[i], LOW); // turns all lights off
    }
  }
  last2 = current2;


  current3 = debounce3(last3);
  if (last3 == LOW && current3 == HIGH)
  {
    for (int i = 4; i >= 0; i--) {
      digitalWrite(ledPins[i], HIGH); //Turns on LED #i each time this runs
      digitalWrite(ledPins[8 - i], HIGH); //Turns on LED #i each time this runs
      delay(500);
      for (int i = 0; i <= 8; i++) {
        digitalWrite(ledPins[i], LOW); // turns all lights off
      }
    }
  }
  last3 = current3;

  current4 = debounce4(last4);
  if (last4 == LOW && current4 == HIGH)
  {
    //slow arrow right to left
    for (int i = 8; i >= 0; i--) {
      digitalWrite(ledPins[i], HIGH);
      delay(500);
    }
    for (int i = 0; i <= 8; i++) {
      digitalWrite(ledPins[i], LOW); // turns all lights off
    }
  }
  last4 = current4;
}
boolean debounce4(boolean last4) {

Any time you have numbers in your variable names, you're probably doing it wrong. If you have numbers in function names, then you're definitely doing it wrong.

I don't think analogRead() does what you think it does. Start by reading up on the definition of true and false.

the only reason the variables have numbers associated to them is because i have 4 variables that do the same thing for each different buttons, as far as the analogRead() i have the buttons assigned to the analog pins with a pulldown resistor, if the value is anything other than 0 then the button has been pushed, it then changes the state machine for that button to "on" vs "off", the problem im having is its not staying on.

You should digitalRead(); the button pins.

the i dont have enough pins for digital buttons.

d.raymond:
the i dont have enough pins for digital buttons.

That does not matter. Usually analog pins can be used as digital pins; on a 328 based board, it is the case for A0 to A5.

ok, so ive changed all variable names with numbers in them(no more numbers), ive got rid of all analogRead(), and i still have the same issue.

im attaching a picture of what im trying to build (just a simpler version)

here is the new code

unsigned long previousMillis = 0;
const long interval = 1000;

int warning_value = 0;
int left_value = 0;
int center_value = 0;
int right_value = 0;

int warning = 1;
int left = 2;
int center = 3;
int right = 4;

boolean warning_last = LOW;
boolean left_last = LOW;
boolean center_last = LOW;
boolean right_last = LOW;

boolean warning_current = LOW;
boolean left_current = LOW;
boolean center_current = LOW;
boolean right_current = LOW;

//----------------------------------------------------------------------------

void setup() {
  pinMode(warning, INPUT);
  pinMode(left, INPUT);
  pinMode(center, INPUT);
  pinMode(right, INPUT);

  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);

  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);

  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);

  int ledPins[] = {13, 11, 10, 9, 8, 7, 6, 5, 4};

  for (int i = 0; i <= 8; i++) {
    digitalWrite(ledPins[i], LOW); // turns all lights off
  }
}
boolean debounce_warning(boolean warning_last) {
  boolean warning_current = digitalRead(warning);
  if (warning_last != warning_current) {
    delay(5);
    warning_current = digitalRead(warning);
  }
  return warning_current;
}
boolean debounce_left(boolean left_last) {
  boolean left_current = digitalRead(left);
  if (left_last != left_current) {
    delay(5);
    left_current = digitalRead(left);
  }
  return left_current;
}
boolean debounce_center(boolean center_last) {
  boolean center_current = digitalRead(center);
  if (center_last != center_current) {
    delay(5);
    center_current = digitalRead(center);
  }
  return center_current;
}
boolean debounce_right(boolean right_last) {
  boolean right_current = digitalRead(right);
  if (right_last != right_current) {
    delay(5);
    right_current = digitalRead(right);
  }
  return right_current;
}

//------------------------------------------------------------------------

void loop() {
  int ledPins[] = {13, 12, 11, 10, 9, 8, 7, 6, 5}; //table with all leds
  int ledPinscustom[] = {13, 12, 10, 9, 8, 6, 5}; //table with limited leds
  int ledPinscustom2[] = {12, 11, 10, 9, 8, 7, 6}; //table with limited leds

  warning_current = debounce_warning(warning_last);
  if (warning_last == LOW && warning_current == HIGH)
  {
    //center 2 on outer 3 flash with center of 3 on.
    for (int i = 0; i <= 8; i++) {
      // digitalWrite(ledPins[i], LOW);
      for (int j = 0; j <= 6; j++) {
        digitalWrite(ledPins[2], LOW);
        digitalWrite(ledPins[6], LOW);
        digitalWrite(ledPinscustom[j], HIGH);
      }
      delay(150);
      for (int k = 0; k <= 6; k++) {
        digitalWrite(ledPins[0], LOW);
        digitalWrite(ledPins[8], LOW);
        digitalWrite(ledPinscustom2[k], HIGH);
      }
      delay(150);
    } //end for loop

    //Turns off all leds
    for (int i = 0; i <= 8; i++) {
      digitalWrite(ledPins[i], LOW);
    }
    delay(150);// end for loop

    //strobe, out to in, with 1 each side lit.
    for (int i = 0; i <= 5; i++) {
      for (int i = 0; i <= 4; i++) {
        digitalWrite(ledPins[i], HIGH);
        digitalWrite(ledPins[8 - i], HIGH);
        delay(50);
        digitalWrite(ledPins[i], LOW);
        digitalWrite(ledPins[8 - i], LOW);
        delay(50);
      }
    }//end for loop

    //Turns off all leds
    for (int i = 0; i <= 8; i++) {
      digitalWrite(ledPins[i], LOW);
    }
    delay(150);// end for loop

    //half and half fast
    for (int i = 0; i <= 8; i++) {
      for (int i = 0; i <= 5; i++) {
        digitalWrite(ledPins[i], HIGH);
      }
      for (int i = 5; i <= 8; i++) {
        digitalWrite(ledPins[i], LOW);
      }
      delay(100);
      for (int i = 0; i <= 3; i++) {
        digitalWrite(ledPins[i], LOW);
      }
      for (int i = 5; i <= 8; i++) {
        digitalWrite(ledPins[i], HIGH);
      }
      delay(100);
    }//end for loop

    //Turns off all leds
    for (int i = 0; i <= 8; i++) {
      digitalWrite(ledPins[i], LOW); //Turns on LED #i each time this runs
    }
    delay(150);// end for loop

    //LEDs strobe, center out, 1 each side lit.
    for (int i = 0; i <= 5; i++) {
      for (int i = 5; i >= 0; i--) {
        digitalWrite(ledPins[i ], HIGH); //Turns on LED #i each time this runs
        digitalWrite(ledPins[8 - i], HIGH); //Turns on LED #i each time this runs
        delay(50);
        digitalWrite(ledPins[i ], LOW); //Turns on LED #i each time this runs
        digitalWrite(ledPins[8 - i], LOW); //Turns on LED #i each time this runs
        delay(50);
      }
    } //end for loop
  }
  warning_last = warning_current;


  left_current = debounce_left(left_last);
  if (left_last == LOW && left_current == HIGH)
  {
    //slow arrow right to left
    for (int i = 0; i <= 8; i++) {
      digitalWrite(ledPins[i], HIGH);
      delay(500);
    }
    for (int i = 0; i <= 8; i++) {
      digitalWrite(ledPins[i], LOW); // turns all lights off
    }
  }
  left_last = left_current;


  center_current = debounce_center(center_last);
  if (center_last == LOW && center_current == HIGH)
  {
    //center out
    for (int i = 4; i >= 0; i--) {
      digitalWrite(ledPins[i], HIGH); //Turns on LED #i each time this runs
      digitalWrite(ledPins[8 - i], HIGH); //Turns on LED #i each time this runs
      delay(500);
      for (int i = 0; i <= 8; i++) {
        digitalWrite(ledPins[i], LOW); // turns all lights off
      }
    }
  }
  center_last = center_current;

  right_current = debounce_right(right_last);
  if (right_last == LOW && right_current == HIGH)
  {
    //slow arrow left to right
    for (int i = 8; i >= 0; i--) {
      digitalWrite(ledPins[i], HIGH);
      delay(500);
    }
    for (int i = 0; i <= 8; i++) {
      digitalWrite(ledPins[i], LOW); // turns all lights off
    }
  }
  right_last = right_current;
}

:slight_smile: The point wasn't to just get the numbers out of the names. That was just the symptom. What you should immediately notice once you see variables with numbers is that you need an array. Make the number matter. Then instead of putting numbers on multiple functions, write one function that takes a number as an argument. Then, use #define or an enum to give those numbers names to end up with what you have now in much much much less code.

Furthermore, once you realize that you have all those similar objects like buttons that all need the same list of variables, then what you really want to learn about is classes.

im not sure how to do that, can you post an example?

A class is one solution to the numbered-functions problem. You could define a class called Button which contains information about the button like the pin it’s connected to and the state of the button. Then you can make methods (functions) in the class which do useful things like isPressed() and wasPressedSinceILastAsked().

Another solution is to just put it all into one function. Look at each one and work out what’s different. The only difference is the pin number, so make that a parameter. When you call the debounce function, you should give it the pin number.

boolean debounce(boolean last, int pin) {
  boolean current = digitalRead(pin);
  if (last != current) {
    delay(5);
    current = digitalRead(pin);
  }
  return current;
}

Then you would call this function like: warning_current = debounce(warning_last, warning)

OK, so now to the real problem. Think about what your current code does if you were able to make it “just loop”. It turns on an LED, waits in a delay(), then it turns on another LED, waits again. It does that for all the lights and then goes back to the top where it does a digitalRead() to find out if another button is currently pressed. So if you want to change pattern, pressing and releasing the button during a pattern will do nothing. It never reads the button during that time so it can’t know if the button was pressed.

So you need a way of putting a wait-time between each light while you are still checking the button to see if it’s pressed. You will have to totally overhaul your code to do this. You will need to use the millis() function to measure the time the LED is on and you need some way of easily encoding which LED is next and how long it’s supposed to stay on.

You are already using an array for your LED pins. Think about having two arrays - one with the list of which LEDs to turn on/off and another with the delay between each one. To store the on/off state of your 9 lights, you can use 16-bit integers, to store all the bits efficiently. (The 8-bit Arduinos will always use a full byte to store one boolean variable. That’s fine for one or a hundred, but you could have more with complex patterns.)

//note that bits are numbered right-to-left, so position 0 is actually the right end
uint_16 warning_pattern[] = { 
  0b0000000111111111,
  0b0000000011111110,
  0b0000000001111100,
  0b0000000000111000,
  0b0000000000010000};
unsigned long warning_delay[] = {150, 150, 150, 150, 150};


void loop() {
  //this only does the warning pattern - need to add buttons to select patterns later
  static unsigned_long last_change_time;
  static int warning_index = 0;
  if(millis() - last_change_time > warning_delay[warning_index]) {
    //time to change to the next line in the pattern
    warning_index++;
    if(warning_index > sizeof(warning_pattern)/sizeof(warning_pattern[0])) {
      warning_index = 0; //go back to the start of the pattern
    }
    for(int i=0; i<sizeof(ledPins)/sizeof(ledPins[0]); i++) {
      digitalWrite(ledPins[i], warning_pattern[warning_index] & bit(i));
    }
  }
   //other processing here, like reading buttons
}

Note the sizeOf() trick gets the length of each array in bytes then divides by the length of the first item of the array to find out how many elements are in the array.

bit(i) is a macro defined by the Arduino framework which returns a value where the i’th bit is set to 1. Doing a logical-and with the pattern will then return zero if that bit in the pattern was not set. If it was set, the result is 2^i. zero is the same as false, which is the same as LOW. Any non-zero value is true or HIGH.

thank you everyone for the help so far.

I have been working the code the last few days and i have been able to fix part of it, the for loops now continuously loop until another button is pushed(as desired), but the problem is still the delays(when i switch between the modes i have to hit the buttons a ton of times and hope it gets a read during a non delay time) (not desired).

I have not been able to get the bit shifting to work.

i did remove 1 light, so now im at 8 lights, and the code reflects that. (mostly done because my relay module only has 8 channels, but also cause 8 bit shifting is cleaner than 16 bit shifting)

here is my new code, if you could continue to help me get this figured out without delay it would be greatly appreciated.

int warning_state = LOW;
int left_state = LOW;
int center_state = LOW;
int right_state = LOW;

int warning_value = 0;
int left_value = 0;
int center_value = 0;
int right_value = 0;

int warning = 1;
int left = 2;
int center = 3;
int right = 4;

boolean warning_last = LOW;
boolean left_last = LOW;
boolean center_last = LOW;
boolean right_last = LOW;

boolean warning_current = LOW;
boolean left_current = LOW;
boolean center_current = LOW;
boolean right_current = LOW;

//----------------------------------------------------------------------------

void setup() {
  pinMode(warning, INPUT);
  pinMode(left, INPUT);
  pinMode(center, INPUT);
  pinMode(right, INPUT);

  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);

  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);

  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);

  int ledPins[] = {12, 11, 10, 9, 8, 7, 6, 5};

  for (int i = 0; i <= 7; i++) {
    digitalWrite(ledPins[i], LOW); // turns all lights off
  }
}
boolean debounce_warning(boolean warning_last) {
  boolean warning_current = digitalRead(warning);
  if (warning_last != warning_current) {
    delay(5);
    warning_current = digitalRead(warning);
  }
  return warning_current;
}
boolean debounce_left(boolean left_last) {
  boolean left_current = digitalRead(left);
  if (left_last != left_current) {
    delay(5);
    left_current = digitalRead(left);
  }
  return left_current;
}
boolean debounce_center(boolean center_last) {
  boolean center_current = digitalRead(center);
  if (center_last != center_current) {
    delay(5);
    center_current = digitalRead(center);
  }
  return center_current;
}
boolean debounce_right(boolean right_last) {
  boolean right_current = digitalRead(right);
  if (right_last != right_current) {
    delay(5);
    right_current = digitalRead(right);
  }
  return right_current;
}

//------------------------------------------------------------------------

void loop() {

  int ledPins[] = {12, 11, 10, 9, 8, 7, 6, 5}; //table with all leds
  int ledPinscustom[] = {12, 11, 9, 8, 6, 5}; //table with limited leds
  int ledPinscustom2[] = {11, 10, 9, 8, 7, 6}; //table with limited leds

  warning_current = debounce_warning(warning_last);
  if (warning_last == LOW && warning_current == HIGH)
  {
    warning_state = HIGH;
    left_state = LOW;
    center_state = LOW;
    right_state = LOW;
  }
  left_current = debounce_left(left_last);
  if (left_last == LOW && left_current == HIGH)
  {
    warning_state = LOW;
    left_state = HIGH;
    center_state = LOW;
    right_state = LOW;
  }
  center_current = debounce_center(center_last);
  if (center_last == LOW && center_current == HIGH)
  {
    warning_state = LOW;
    left_state = LOW;
    center_state = HIGH;
    right_state = LOW;
  }
  right_current = debounce_right(right_last);
  if (right_last == LOW && right_current == HIGH)
  {
    warning_state = LOW;
    left_state = LOW;
    center_state = LOW;
    right_state = HIGH;
  }
  if (warning_state == HIGH)
  {
    //center 2 on outer 3 flash with center of 3 on.
    for (int i = 0; i <= 7; i++) {
      // digitalWrite(ledPins[i], LOW);
      for (int j = 0; j <= 5; j++) {
        digitalWrite(ledPins[1], LOW);
        digitalWrite(ledPins[6], LOW);
        digitalWrite(ledPinscustom[j], HIGH);
      }
      delay(150);
      for (int k = 0; k <= 5; k++) {
        digitalWrite(ledPins[0], LOW);
        digitalWrite(ledPins[7], LOW);
        digitalWrite(ledPinscustom2[k], HIGH);
      }
      delay(150);
    } //end for loop

    //Turns off all leds
    for (int i = 0; i <= 7; i++) {
      digitalWrite(ledPins[i], LOW);
    }
    delay(150);// end for loop

    //strobe, out to in, with 1 each side lit.
    for (int i = 0; i <= 4; i++) {
      for (int i = 0; i <= 3; i++) {
        digitalWrite(ledPins[i], HIGH);
        digitalWrite(ledPins[7 - i], HIGH);
        delay(50);
        digitalWrite(ledPins[i], LOW);
        digitalWrite(ledPins[7 - i], LOW);
        delay(50);
      }
    }//end for loop

    //Turns off all leds
    for (int i = 0; i <= 7; i++) {
      digitalWrite(ledPins[i], LOW);
    }
    delay(150);// end for loop

    //half and half fast
    for (int i = 0; i <= 7; i++) {
      for (int i = 0; i <= 2; i++) {
        digitalWrite(ledPins[i], HIGH);
      }
      for (int i = 2; i <= 7; i++) {
        digitalWrite(ledPins[i], LOW);
      }
      delay(100);
      for (int i = 0; i <= 2; i++) {
        digitalWrite(ledPins[i], LOW);
      }
      for (int i = 2; i <= 7; i++) {
        digitalWrite(ledPins[i], HIGH);
      }
      delay(100);
    }//end for loop

    //Turns off all leds
    for (int i = 0; i <= 7; i++) {
      digitalWrite(ledPins[i], LOW); //Turns on LED #i each time this runs
    }
    delay(150);// end for loop

    //LEDs strobe, center out, 1 each side lit.
    for (int i = 0; i <= 4; i++) {
      for (int i = 4; i >= 0; i--) {
        digitalWrite(ledPins[i ], HIGH); //Turns on LED #i each time this runs
        digitalWrite(ledPins[7 - i], HIGH); //Turns on LED #i each time this runs
        delay(50);
        digitalWrite(ledPins[i ], LOW); //Turns on LED #i each time this runs
        digitalWrite(ledPins[7 - i], LOW); //Turns on LED #i each time this runs
        delay(50);
      }
    } //end for loop
  }
  warning_last = warning_current;

  if (left_state == HIGH)
  {
    //slow arrow right to left
    for (int i = 0; i <= 7; i++) {
      digitalWrite(ledPins[i], HIGH);
      delay(500);
    }
    for (int i = 0; i <= 7; i++) {
      digitalWrite(ledPins[i], LOW); // turns all lights off
    }
  }
  left_last = left_current;

  if (center_state == HIGH)
  {
    //center out
    for (int i = 3; i >= 0; i--) {
      digitalWrite(ledPins[i], HIGH); //Turns on LED #i each time this runs
      digitalWrite(ledPins[7 - i], HIGH); //Turns on LED #i each time this runs
      delay(500);
      for (int i = 0; i <= 7; i++) {
        digitalWrite(ledPins[i], LOW); // turns all lights off
      }
    }
  }
  center_last = center_current;

  if (right_state == HIGH)
  {
    //slow arrow left to right
    for (int i = 7; i >= 0; i--) {
      digitalWrite(ledPins[i], HIGH);
      delay(500);
    }
    for (int i = 0; i <= 7; i++) {
      digitalWrite(ledPins[i], LOW); // turns all lights off
    }
  }
  right_last = right_current;
}
//note that bits are numbered right-to-left, so position 0 is actually the right end
uint8_t warning_pattern[] = { 
  0b11111111,
  0b11111110,
  0b01111100,
  0b00111000,
  0b00010000};
unsigned long warning_delay[] = {150, 150, 150, 150, 150};


void loop() {
  //this only does the warning pattern - need to add buttons to select patterns later
  static unsigned_long last_change_time;
  static int warning_index = 0;
  if(millis() - last_change_time > warning_delay[warning_index]) {
    //time to change to the next line in the pattern
    warning_index++;
    if(warning_index > sizeof(warning_pattern)/sizeof(warning_pattern[0])) {
      warning_index = 0; //go back to the start of the pattern
    }
    for(int i=0; i<sizeof(ledPins)/sizeof(ledPins[0]); i++) {
      digitalWrite(ledPins[i], warning_pattern[warning_index] & bit(i));
    }
  }
   //other processing here, like reading buttons
}

that code doesnt work, it just lights up 4 random leds that dont do anything.

so to clarify, what i need now is when i push a button while one pattern is running it immediately switches to the other pattern.

what i need now is when i push a button while one pattern is running it immediately switches to the other pattern.

That CAN happen, but only after you get rid of EVERY delay() in your program. Google state machines, and read, understand, AND EMBRACE the blink without delay philosophy.

Of course it doesn't work. It doesn't have the rest of the stuff that you need to write yourself.

You should see some change on the LEDs if that code is running.

I know this is not in keeping with the “Teach a Man to Fish” goal of the forum, but I was intrigued by the problem. I started thinking about how I would solve it. Before I knew it, I had the code below. It’s totally untested, but it does compile. Make sure you have the Bounce2 Library:

As written the different patterns all have the same LED sequence. Those need to be updated with the proper Warning, Left, Go Around, and Right sequences (and custom periods if desired).

I’m certainly not saying this is the best way to do this. But, it is A way. And, it EMBRACES the Arduino philosophy of keeping the loop going with non-blocking code. If nothing else, the OP needs to do the same (per PaulS).

#include <Bounce2.h>

void allLedsOff(void);
void displayPattern(void);

const uint8_t warningButton = A5;
const uint8_t leftButton = A4;
const uint8_t goAroundButton = A3;
const uint8_t rightButton = A2;
const uint8_t numPatterns = 4;
const uint32_t debounceDelay = 5;

typedef struct {
  const uint16_t ledPattern;
  const uint16_t delayTime;
} patternElement;

typedef struct {
  Bounce activationButton;
  patternElement *patternStart;
  uint16_t patternLength;
} patternControl;

patternElement warningPattern[] = {
  {0b0000000111111111, 150},
  {0b0000000011111110, 150},
  {0b0000000001111100, 150},
  {0b0000000000111000, 150},
  {0b0000000000010000, 150}
};

patternElement leftPattern[] = {
  {0b0000000111111111, 150},
  {0b0000000011111110, 150},
  {0b0000000001111100, 150},
  {0b0000000000111000, 150},
  {0b0000000000010000, 150}
};

patternElement goAroundPattern[] = {
  {0b0000000111111111, 150},
  {0b0000000011111110, 150},
  {0b0000000001111100, 150},
  {0b0000000000111000, 150},
  {0b0000000000010000, 150}
};

patternElement rightPattern[] = {
  {0b0000000111111111, 150},
  {0b0000000011111110, 150},
  {0b0000000001111100, 150},
  {0b0000000000111000, 150},
  {0b0000000000010000, 150}
};

patternControl patterns[numPatterns];
uint8_t ledPins[] = {13, 11, 10, 9, 8, 7, 6, 5, 4};
uint8_t numLeds = sizeof(ledPins) / sizeof(uint8_t);
uint8_t activePattern, patternPosition;

void setup() {
  uint8_t index;

  pinMode(warningButton, INPUT_PULLUP);
  patterns[0].activationButton.attach(warningButton);
  patterns[0].activationButton.interval(debounceDelay);
  patterns[0].patternStart = warningPattern;
  patterns[0].patternLength = sizeof(warningPattern) / sizeof(patternElement);

  pinMode(leftButton, INPUT_PULLUP);
  patterns[1].activationButton.attach(leftButton);
  patterns[1].activationButton.interval(debounceDelay);
  patterns[1].patternStart = leftPattern;
  patterns[1].patternLength = sizeof(leftPattern) / sizeof(patternElement);

  pinMode(goAroundButton, INPUT_PULLUP);
  patterns[2].activationButton.attach(goAroundButton);
  patterns[2].activationButton.interval(debounceDelay);
  patterns[2].patternStart = goAroundPattern;
  patterns[2].patternLength = sizeof(goAroundPattern) / sizeof(patternElement);

  pinMode(rightButton, INPUT_PULLUP);
  patterns[3].activationButton.attach(rightButton);
  patterns[3].activationButton.interval(debounceDelay);
  patterns[3].patternStart = rightPattern;
  patterns[3].patternLength = sizeof(rightPattern) / sizeof(patternElement);

  for (index = 0; index < numLeds; index++) {
    pinMode(ledPins[index], OUTPUT);
  }

  allLedsOff();
}

void loop() {
  enum State {standBy, active};

  static State state = standBy;
  static uint32_t patternTimer;
  uint8_t index;

  for (index = 0; index < numPatterns; index++) {
    patterns[index].activationButton.update();
  }

  if ((state == active) && patterns[activePattern].activationButton.rose()) {
    state = standBy;
    allLedsOff();
  } else {
    for (index = 0; index < numPatterns; index++) {
      if (patterns[index].activationButton.rose()) {
        state = active;
        activePattern = index;
        patternTimer = millis();
        patternPosition = 0;
        displayPattern();
        break;
      }
    }
  }

  if (state == active) {
    if (millis() - patternTimer >= (patterns[activePattern].patternStart + patternPosition)->delayTime) {
      patternPosition++;
      if (patternPosition >= patterns[activePattern].patternLength) {
        patternPosition = 0;
      }
      patternTimer += (patterns[activePattern].patternStart + patternPosition)->delayTime;
      displayPattern();
    }
  }
}

void allLedsOff() {
  uint8_t index;
  for (index = 0; index < numLeds; index++) {
    digitalWrite(ledPins[index], LOW);
  }
}

void displayPattern() {
  uint8_t index;
  for (index = 0; index < numLeds; index++) {
    digitalWrite(ledPins[index], (patterns[activePattern].patternStart + patternPosition)->ledPattern & bit(index));
  }
}