Issue with matrix keypad vs pull up buttons (in the code)

Good Day all,

My first version of the project used pull up resistors with the buttons and worked great, the code for that here:

void  loop() {
  digitalWrite(13, HIGH);
  delayMicroseconds(10); // Approximately  10% duty cycle @ 1KHz
  digitalWrite(13, LOW);
  delayMicroseconds(1000 -  10);
  if (digitalRead(pattern_button) == HIGH)
  {
    delay(500);
    warning_count++;
    if (warning_count > 10) {
      warning_count = 1;
    }
  }
  if (digitalRead(warning_button) == HIGH)
  {
    delay(50);
    pattern_count = 1; 
  }
  if (digitalRead(left_button) == HIGH)
  {
    delay(50);
    pattern_count = 2;
  }
  if (digitalRead(center_button)  == HIGH)
  {
    delay(50);
    pattern_count = 3;
  }
  if (digitalRead(right_button)  == HIGH)
  {
    delay(50);
    pattern_count = 4;
  }
  if (digitalRead(off_button)  == HIGH)
  {
    delay(50);
    pattern_count = 0;
    EEPROM.update(0,warning_count);
  }
 switch (pattern_count) {
    case 0:
      pattern_off();
      break;
    case 1:
      traffic_left();
      break;
    case 2:
      traffic_center();
      break;
    case 3:
      traffic_right();
      break;
    case 4:
      traffic_warning();
      break;
  }
}

but i reorganized my hardware layout to reduce bulk and place all of the larger components in an enclosure, when i did this, i switched the inputs to a matrix keypad, I adjusted the code accordingly and it kind of works.

Matrix keypad code:

void loop() {
  
digitalWrite(statuslight, HIGH);

byte key = (byte)keypad.getKey();
if (key){
  gotKey = true;
  pressedKey = key - 1;
  key = 0;
}
if (gotKey == true){
  if (ButtonPress[pressedKey] == "Change"){
    delay(500);
    warning_count++;
    if (warning_count > 18) {
    warning_count = 1;
    }
      gotKey = false;
  }
    if (ButtonPress[pressedKey] == "Right"){
      delay(50);
      pattern_count = 1;
    }
    if (ButtonPress[pressedKey] == "Center"){
      delay(50);
      pattern_count = 2;
    }
    if (ButtonPress[pressedKey] == "Left"){
      delay(50);
      pattern_count = 3;
    }
    if (ButtonPress[pressedKey] == "Warn"){
      delay(50);
      pattern_count = 4;
    }
    if (ButtonPress[pressedKey] == "Stop"){
      delay(50);
      pattern_count = 0;
      EEPROM.update(0, warning_count);
    }
}

2 main issues:

First: is that if the arduino is in a "stand by" state, meaning i have not pressed any input buttons for more than a few minutes, it locks up and does not select the correct IF statement or case statement.

Second: while the warning pattern is active, i press the pattern button to advance to a new pattern, it de activates the warning pattern now. and i have to repress the warning button to reactivate it.

I believe the issue lies with this line:

gotKey = false;

I have played around with placing it in different positions throughout the loop and even tried removing it, but i cannot get the desired results.

Any help with this would be greatly appreciated.

Part of the code.

Post the complete sketch, or a smaller sketch that illustrates whatever it is the problem you are having.

a7

the rest of the sketch works fine, but here you are:

Entire sketch:

#include <EEPROM.h>
#include <Keypad.h>

const byte ROWS = 2;
const byte COLS = 3;
const byte key [ROWS][COLS] = 
{
  {1,2,3},
  {4,5,6}
};

const String ButtonPress[6] = {"Stop", "Warn", "Change", "Left", "Center", "Right"};

byte rowPins[ROWS] = {3,2};
byte colPins[COLS] = {A4,A3,A2};

bool gotKey = false;

byte pressedKey;

Keypad keypad = Keypad(makeKeymap(key),rowPins, colPins, ROWS, COLS);

int statuslight = 13;
int LEDleft[] = {5, 6, 7, 8, 9, 10, 11, 12};
int LEDright[] = {12, 11, 10, 9, 8, 7, 6, 5};
int centerleft[] = {9, 10, 11, 12};
int centerright[] = {8, 7, 6, 5};
int light = 0;
int counter = 1;
int pattern_count = 0;
int warning_count = EEPROM.read(0);
long lasttimer = 0;
static long timer = 100;
static long timer2 = 200;
#define LEFTside 0x0
#define RIGHTside 0x1
byte whichLED = LEFTside;
byte LEFT_state = LOW;
byte RIGHT_state = LOW;
byte arrow_state = HIGH;
unsigned long strobeDelay = 75;
unsigned long switchDelay = 500;
unsigned long switchDelay2 = 1000;
unsigned long switchDelay3 = 450;
unsigned long strobeWait = strobeDelay;
unsigned long waitUntilSwitch = switchDelay;
unsigned long sequenceStartTime;
unsigned long sequenceStartTime2;

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

  pinMode(statuslight, 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);

}
void loop() {
  
digitalWrite(statuslight, HIGH);

byte key = (byte)keypad.getKey();
if (key){
  gotKey = true;
  pressedKey = key - 1;
  key = 0;
}
if (gotKey == true){
  if (ButtonPress[pressedKey] == "Change"){
    delay(500);
    warning_count++;
    if (warning_count > 18) {
    warning_count = 1;
    }
      gotKey = false;
  }
    if (ButtonPress[pressedKey] == "Right"){
      delay(50);
      pattern_count = 1;
    }
    if (ButtonPress[pressedKey] == "Center"){
      delay(50);
      pattern_count = 2;
    }
    if (ButtonPress[pressedKey] == "Left"){
      delay(50);
      pattern_count = 3;
    }
    if (ButtonPress[pressedKey] == "Warn"){
      delay(50);
      pattern_count = 4;
    }
    if (ButtonPress[pressedKey] == "Stop"){
      delay(50);
      pattern_count = 0;
      EEPROM.update(0, warning_count);
    }

}

  switch (pattern_count) {
    case 0:
      pattern_off();
      break;
    case 1:
      traffic_left();
      break;
    case 2:
      traffic_center();
      break;
    case 3:
      traffic_right();
      break;
    case 4:
      traffic_warning();
      break;
  }
}

void pattern_off() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
}
void traffic_left() {
  long time = millis() - sequenceStartTime;
  if (time < 16000)
  {
    left_arrow();
  }
  else if (time < 18000)
  {
    half_half_flash();
  }
  else sequenceStartTime = millis();
}
void traffic_center() {
  long time = millis() - sequenceStartTime;
  if (time < 16000)
  {
    center_arrow();
  }
  else if (time < 18000)
  {
    half_half_flash();
  }
  else sequenceStartTime = millis();
}
void traffic_right() {
  long time = millis() - sequenceStartTime;
  if (time < 16000)
  {
    right_arrow();
  }
  else if (time < 18000)
  {
    half_half_flash();
  }
  else sequenceStartTime = millis();
}
void traffic_warning() {
  switch (warning_count) {
    case 1:
      even_odd();
      break;
    case 2:
      even_odd_flash();
      break;
    case 3:
      half_half();
      break;
    case 4:
      half_half_flash();
      break;
    case 5:
      two_by_two();
      break;
    case 6:
      tow_by_two_flash();
      break;
    case 7:
      two_by_two_parallel();
      break;
    case 8:
      two_by_two_parallel_flash();
      break;
    case 9:
      three_by_one();
      break;
    case 10:
      three_by_one_flash();
      break;
    case 11:
      outboard();
      break;
    case 12:
      outboard_flash();
      break;
    case 13:
      inboard_6();
      break;
    case 14:
      inboard_6_flash();
      break;
    case 15:
      solid();
      break;
    case 16:
      solid_flash();
      break;
    case 17:
      solid_flash_pause();
      break;
    case 18:
      multi_pattern();
      break;
  }
}
void even_odd() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  long time = millis() - sequenceStartTime;
  if (time < 300)
  {
    digitalWrite(12, HIGH);
    digitalWrite(11, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    digitalWrite(5, LOW);
  }
  else if (time < 600)
  {
    digitalWrite(12, LOW);
    digitalWrite(11, HIGH);
    digitalWrite(10, LOW);
    digitalWrite(9, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(7, HIGH);
    digitalWrite(6, LOW);
    digitalWrite(5, HIGH);
  }
  else sequenceStartTime = millis();
}
void even_odd_flash() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  digitalWrite(12, LEFT_state);
  digitalWrite(11, RIGHT_state);
  digitalWrite(10, LEFT_state);
  digitalWrite(9, RIGHT_state);
  digitalWrite(8, LEFT_state);
  digitalWrite(7, RIGHT_state);
  digitalWrite(6, LEFT_state);
  digitalWrite(5, RIGHT_state);
  if ((long)(millis() - waitUntilSwitch) >= 0) {
    LEFT_state = LOW;
    RIGHT_state = LOW;
    whichLED = !whichLED;
    waitUntilSwitch += switchDelay;
  }
  if ((long)(millis() - strobeWait) >= 0) {
    if (whichLED == LEFTside)
      LEFT_state = !LEFT_state;
    if (whichLED == RIGHTside)
      RIGHT_state = !RIGHT_state;
    strobeWait += strobeDelay;
  }
}
void half_half() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  long time = millis() - sequenceStartTime;
  if (time < 250)
  {
    digitalWrite(12, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(7, LOW);
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
  }
  else if (time < 500)
  {
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(5, HIGH);
  }
  else sequenceStartTime = millis();
}
void half_half_flash() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  digitalWrite(12, LEFT_state);
  digitalWrite(11, LEFT_state);
  digitalWrite(10, LEFT_state);
  digitalWrite(9, LEFT_state);
  digitalWrite(8, RIGHT_state);
  digitalWrite(7, RIGHT_state);
  digitalWrite(6, RIGHT_state);
  digitalWrite(5, RIGHT_state);
  if ((long)(millis() - waitUntilSwitch) >= 0) {
    LEFT_state = LOW;
    RIGHT_state = LOW;
    whichLED = !whichLED;
    waitUntilSwitch += switchDelay;
  }
  if ((long)(millis() - strobeWait) >= 0) {
    if (whichLED == LEFTside)
      LEFT_state = !LEFT_state;
    if (whichLED == RIGHTside)
      RIGHT_state = !RIGHT_state;
    strobeWait += strobeDelay;
  }
}
void two_by_two() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  long time = millis() - sequenceStartTime;
  if (time < 150)
  {
    digitalWrite(12, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    digitalWrite(5, HIGH);
  }
  else if (time < 300)
  {
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
  }
  else sequenceStartTime = millis();
}
void tow_by_two_flash() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  digitalWrite(12, LEFT_state);
  digitalWrite(11, LEFT_state);
  digitalWrite(10, RIGHT_state);
  digitalWrite(9, RIGHT_state);
  digitalWrite(8, RIGHT_state);
  digitalWrite(7, RIGHT_state);
  digitalWrite(6, LEFT_state);
  digitalWrite(5, LEFT_state);
  if ((long)(millis() - waitUntilSwitch) >= 0) {
    LEFT_state = LOW;
    RIGHT_state = LOW;
    whichLED = !whichLED;
    waitUntilSwitch += switchDelay;
  }
  if ((long)(millis() - strobeWait) >= 0) {
    if (whichLED == LEFTside)
      LEFT_state = !LEFT_state;
    if (whichLED == RIGHTside)
      RIGHT_state = !RIGHT_state;
    strobeWait += strobeDelay;
  }
}
void three_by_one() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  long time = millis() - sequenceStartTime;
  if (time < 400)
  {
    digitalWrite(12, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(7, LOW);
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
  }
  else if (time < 800)
  {
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
    digitalWrite(9, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(7, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(5, HIGH);
  }
  else sequenceStartTime = millis();
}
void three_by_one_flash() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  digitalWrite(12, RIGHT_state);
  digitalWrite(11, RIGHT_state);
  digitalWrite(10, RIGHT_state);
  digitalWrite(8, RIGHT_state);
  digitalWrite(9, LEFT_state);
  digitalWrite(7, LEFT_state);
  digitalWrite(6, LEFT_state);
  digitalWrite(5, LEFT_state);
  if ((long)(millis() - waitUntilSwitch) >= 0) {
    LEFT_state = LOW;
    RIGHT_state = LOW;
    whichLED = !whichLED;
    waitUntilSwitch += switchDelay;
  }
  if ((long)(millis() - strobeWait) >= 0) {
    if (whichLED == LEFTside)
      LEFT_state = !LEFT_state;
    if (whichLED == RIGHTside)
      RIGHT_state = !RIGHT_state;
    strobeWait += strobeDelay;
  }
}
void two_by_two_parallel() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  long time = millis() - sequenceStartTime;
  if (time < 150)
  {
    digitalWrite(12, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
  }
  else if (time < 300)
  {
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    digitalWrite(5, HIGH);
  }
  else sequenceStartTime = millis();
}
void two_by_two_parallel_flash() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  digitalWrite(12, LEFT_state);
  digitalWrite(11, LEFT_state);
  digitalWrite(10, RIGHT_state);
  digitalWrite(9, RIGHT_state);
  digitalWrite(8, LEFT_state);
  digitalWrite(7, LEFT_state);
  digitalWrite(6, RIGHT_state);
  digitalWrite(5, RIGHT_state);
  if ((long)(millis() - waitUntilSwitch) >= 0) {
    LEFT_state = LOW;
    RIGHT_state = LOW;
    whichLED = !whichLED;
    waitUntilSwitch += switchDelay;
  }
  if ((long)(millis() - strobeWait) >= 0) {
    if (whichLED == LEFTside)
      LEFT_state = !LEFT_state;
    if (whichLED == RIGHTside)
      RIGHT_state = !RIGHT_state;
    strobeWait += strobeDelay;
  }
}
void outboard() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  long time = millis() - sequenceStartTime;
  if (time < 300)
  {
    digitalWrite(12, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    digitalWrite(7, LOW);
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
  }
  else if (time < 600)
  {
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    digitalWrite(5, HIGH);
  }
  else sequenceStartTime = millis();
}
void outboard_flash() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  digitalWrite(12, RIGHT_state);
  digitalWrite(11, RIGHT_state);
  digitalWrite(6, LEFT_state);
  digitalWrite(5, LEFT_state);
  if ((long)(millis() - waitUntilSwitch) >= 0) {
    LEFT_state = LOW;
    RIGHT_state = LOW;
    whichLED = !whichLED;
    waitUntilSwitch += switchDelay;
  }
  if ((long)(millis() - strobeWait) >= 0) {
    if (whichLED == LEFTside)
      LEFT_state = !LEFT_state;
    if (whichLED == RIGHTside)
      RIGHT_state = !RIGHT_state;
    strobeWait += strobeDelay;
  }
}
void inboard_6() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  long time = millis() - sequenceStartTime;
  if (time < 200)
  {
    digitalWrite(11, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    digitalWrite(7, HIGH);
    digitalWrite(6, HIGH);
  }
  else if (time < 400)
  {
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
    digitalWrite(9, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(7, LOW);
    digitalWrite(6, LOW);
  }
  else sequenceStartTime = millis();
}
void inboard_6_flash() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  digitalWrite(11, LEFT_state);
  digitalWrite(10, LEFT_state);
  digitalWrite(9, RIGHT_state);
  digitalWrite(8, RIGHT_state);
  digitalWrite(7, LEFT_state);
  digitalWrite(6, LEFT_state);
  if ((long)(millis() - waitUntilSwitch) >= 0) {
    LEFT_state = LOW;
    RIGHT_state = LOW;
    whichLED = !whichLED;
    waitUntilSwitch += switchDelay;
  }
  if ((long)(millis() - strobeWait) >= 0) {
    if (whichLED == LEFTside)
      LEFT_state = !LEFT_state;
    if (whichLED == RIGHTside)
      RIGHT_state = !RIGHT_state;
    strobeWait += strobeDelay;
  }
}
void solid() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  long time = millis() - sequenceStartTime;
  if (time < 800)
  {
    digitalWrite(12, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(5, HIGH);
  }
  else if (time < 1600)
  {
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    digitalWrite(7, LOW);
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
  }
  else sequenceStartTime = millis();
}
void solid_flash() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  long time = millis() - sequenceStartTime;
  if (time < 200)
  {
    digitalWrite(12, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(5, HIGH);
  }
  else if (time < 400)
  {
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    digitalWrite(7, LOW);
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
  }
  else sequenceStartTime = millis();
}
void solid_flash_pause() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  digitalWrite(12, LEFT_state);
  digitalWrite(11, LEFT_state);
  digitalWrite(10, LEFT_state);
  digitalWrite(9, LEFT_state);
  digitalWrite(8, LEFT_state);
  digitalWrite(7, LEFT_state);
  digitalWrite(6, LEFT_state);
  digitalWrite(5, LEFT_state);
  if ((long)(millis() - waitUntilSwitch) >= 0) {
    LEFT_state = LOW;
    RIGHT_state = LOW;
    whichLED = !whichLED;
    waitUntilSwitch += switchDelay3;
  }
  if ((long)(millis() - strobeWait) >= 0) {
    if (whichLED == LEFTside)
      LEFT_state = !LEFT_state;
    if (whichLED == RIGHTside)
      RIGHT_state = !RIGHT_state;
    strobeWait += strobeDelay;
  }
}
void left_arrow() {
  unsigned long currenttimer = millis();
  if (currenttimer - lasttimer >= timer) {
    lasttimer = currenttimer;
    for (int i = 0; i <= 7; i++) {
    }
    light = light + counter;
    if (light > 7) {
      arrow_state = !arrow_state;
      light = 0;
      counter = 1;
    }
    if (arrow_state == HIGH) {
      digitalWrite(LEDleft[light], HIGH);
    }
    else if (arrow_state == LOW) {
      digitalWrite(LEDleft[light], LOW);
    }
  }
}
void right_arrow() {
  unsigned long currenttimer = millis();
  if (currenttimer - lasttimer >= timer) {
    lasttimer = currenttimer;
    for (int i = 0; i <= 7; i++) {
    }
    light = light + counter;
    if (light > 7) {
      arrow_state = !arrow_state;
      light = 0;
      counter = 1;
    }
    if (arrow_state == HIGH) {
      digitalWrite(LEDright[light], HIGH);
    }
    else if (arrow_state == LOW) {
      digitalWrite(LEDright[light], LOW);
    }
  }
}
void center_arrow() {
  unsigned long currenttimer = millis();
  if (currenttimer - lasttimer >= timer2) {
    lasttimer = currenttimer;
    for (int i = 0; i <= 7; i++) {
    }
    light = light + counter;
    if (light > 3) {
      arrow_state = !arrow_state;
      light = 0;
      counter = 1;
    }
    if (arrow_state == HIGH) {
      digitalWrite(centerright[light], HIGH);
      digitalWrite(centerleft[light], HIGH);
    }
    if (arrow_state == LOW) {
      digitalWrite(centerright[light], LOW);
      digitalWrite(centerleft[light], LOW);
    }
  }
}
void multi_pattern(){
  long time = millis() - sequenceStartTime2;
  if (time < 4000)
  {
    half_half_flash();
  }
  else if (time < 8000)
  {
    outboard();
  }
  else if (time < 12000)
  {
    two_by_two_parallel_flash();
  }
  else if (time < 16000)
  {
    solid_flash_pause();
  }
  else if (time < 20000)
  {
    outboard_flash();
  }
  else if (time < 24000)
  {
    tow_by_two_flash();
  }
  else if (time < 28000)
  {
    half_half();
  }
  else if (time < 32000)
  {
    two_by_two_parallel();
  }
  else sequenceStartTime2 = millis();
}

Yes maybe. You go to quite a bit of trouble to solicit and act upon a key press, that code is very involved.

I cannot test any theories at this time, but I think that the gotKey flag should be reset immediately you enter the code that handles the fact it flags

  if (gotKey == true){
    gotKey = false;

as the rest of that block handles the key press, so you don't want to execute it again with a stale key value.

Alternately, it is sometime done before, like

  gotKey = false;  // until proven otherwise

  if (key){

TBH most keypad examples just use *key*, no need to compute a flag, *key* is the flag and the value all at once.

I also note the painful

    if (ButtonPress[pressedKey] == "Right"){

lines which seem like they are just seeing if pressedKey is a certain number. If human readability was any part of your decision to code this, you might have used an enum, a way of assigned identifiers to numbers that is somewhat lighter weight

enum mySymbols {Stop = 1, Warn, Change, Left, Center, Right};

and anywhere in your code Warn will be the same as writing 2, Change the same as 3 and so forth.

Here's a decent article on it, your google may turn up something more to your liking:

That's it from me for now, HTH.

a7

thanks for the ideas, ill give them a try and let you know.

Perhaps set gotKey = false before if (key) {makes it true}
unless you do that elsewhere?

Otherwise once true always true.

Holy cow the delays you use!

BTW, you can run a button matrix with 1 diode per button on INPUT_PULLUP pin power alone.

All six delay() calls are within a few lines of each other during keyboard handling. So no large problem even in the original.

In the keypad version they are are useless, as in they serve no purpose in this context - the keypad library has eliminated any previous need to.

a7

I placed the code in the wokwi simulator and hacked it to use a neopixel strip which is more fun than wiring LEDs, and affords a possibility for testing and verification.

The keypad only scans 6 buttons.

Try it here


Wokwi_badge UA matrix keypad v. pull up buttons


The code
// https://forum.arduino.cc/t/issue-with-matrix-keypad-vs-pull-up-buttons-in-the-code/1239164
// https://wokwi.com/projects/393248438299541505

# include <EEPROM.h>
# include <Keypad.h>

// hack it to use a strip of neopixels
//
# define NINE   8   // # of LEDs to handle (lose status LED at 13)
# define BASE   5   // base. pins must++

# include <Adafruit_NeoPixel.h>

# define LED_PIN    A0
# define LED_COUNT  NINE

Adafruit_NeoPixel disaply(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

void myDigitalWrite(int theLED, int theValue)
{

  unsigned long was = disaply.getPixelColor(theLED - BASE);

  disaply.setPixelColor(theLED - BASE, theValue ? 0xff0000 : 0x202020);
  disaply.show();

  return;   // rest is for 

  if (was == disaply.getPixelColor(theLED - BASE)) Serial.println("                         work to do");

  Serial.print(theLED);
  Serial.print(" + ");

  bool any = false;
  for (int ii = 0; ii < 9; ii++)
    any |= disaply.getPixelColor(ii) == 0xff0000;

//  if (any) {
  {
    for (int ii = 0; ii < 9; ii++)
      Serial.print(disaply.getPixelColor(ii) == 0xff0000 ? "X" : ".");
  
    Serial.println("");
  }
}

# define digitalWrite myDigitalWrite
//
// end of hack. sue me, I said it was a hack

// the rest is mostly untouched original

const byte ROWS = 2;
const byte COLS = 3;
const byte key [ROWS][COLS] = 
{
  {1,2,3},
  {4,5,6}
};

const String ButtonPress[6] = {"Stop", "Warn", "Change", "Left", "Center", "Right"};

byte rowPins[ROWS] = {3, 2};
byte colPins[COLS] = {A2, A3, A4};

bool gotKey = false;

byte pressedKey;

Keypad keypad = Keypad(makeKeymap(key),rowPins, colPins, ROWS, COLS);

int statuslight = 13;
int LEDleft[] = {5, 6, 7, 8, 9, 10, 11, 12};
int LEDright[] = {12, 11, 10, 9, 8, 7, 6, 5};
int centerleft[] = {9, 10, 11, 12};
int centerright[] = {8, 7, 6, 5};
int light = 0;
int counter = 1;
int pattern_count = 0;
int warning_count = EEPROM.read(0);
long lasttimer = 0;
static long timer = 100;
static long timer2 = 200;
#define LEFTside 0x0
#define RIGHTside 0x1
byte whichLED = LEFTside;
byte LEFT_state = LOW;
byte RIGHT_state = LOW;
byte arrow_state = HIGH;
unsigned long strobeDelay = 75;
unsigned long switchDelay = 500;
unsigned long switchDelay2 = 1000;
unsigned long switchDelay3 = 450;
unsigned long strobeWait = strobeDelay;
unsigned long waitUntilSwitch = switchDelay;
unsigned long sequenceStartTime;
unsigned long sequenceStartTime2;

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

  disaply.begin();
  disaply.setPixelColor(1, 0xff40c0);
  disaply.show();
  delay(777);
  disaply.clear();  
  disaply.show();

  pinMode(statuslight, 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);

}
void loop() {

//  digitalWrite(statuslight, HIGH);

  byte key = (byte)keypad.getKey();
  if (key) {
    Serial.print("\n$raw key "); Serial.print(key);
    gotKey = true;
    pressedKey = key - 1;
    key = 0;
  }
  if (gotKey == true) {
    Serial.print("  "); Serial.print("now key "); Serial.println(pressedKey); Serial.print("  $ ");
    Serial.println(ButtonPress[pressedKey]);
    gotKey = false;

    /*}
    if (0) {*/

    if (0) // hand control this outlier
      if (ButtonPress[pressedKey] == "Change") {
        delay(500);
        warning_count++;
        if (warning_count > 18) {
          warning_count = 1;
        }
        // already is boss      gotKey = false;
      }

    if (ButtonPress[pressedKey] == "Right") {
      delay(50);
      pattern_count = 1;
    }

    if (ButtonPress[pressedKey] == "Center") {
      delay(50);
      pattern_count = 2;
    }

    if (ButtonPress[pressedKey] == "Left") {
      delay(50);
      pattern_count = 3;
    }

    if (ButtonPress[pressedKey] == "Warn") {
      delay(50);
      pattern_count = 4;
    }

    if (ButtonPress[pressedKey] == "Stop") {
      delay(50);
      pattern_count = 0;
      EEPROM.update(0, warning_count);
    }
  }

  static int lastPattern = -1;
  if (pattern_count != lastPattern) {
    Serial.print("pattern_count case "); Serial.println(pattern_count);
    lastPattern = pattern_count;
  }


if (1)
  switch (pattern_count) {
    case 0:
      pattern_off();
      break;
    case 1:
      traffic_left();
Serial.println("            traffic_left");
      break;
    case 2:
      traffic_center();
      break;
    case 3:
      traffic_right();
      break;
    case 4:
      traffic_warning();
      break;
  }

}

void pattern_off() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
}
void traffic_left() {
  long time = millis() - sequenceStartTime;
  if (time < 16000)
  {
    left_arrow();
  }
  else if (time < 18000)
  {
    half_half_flash();
  }
  else sequenceStartTime = millis();
}
void traffic_center() {
  long time = millis() - sequenceStartTime;
  if (time < 16000)
  {
    center_arrow();
  }
  else if (time < 18000)
  {
    half_half_flash();
  }
  else sequenceStartTime = millis();
}
void traffic_right() {
  long time = millis() - sequenceStartTime;
  if (time < 16000)
  {
    right_arrow();
  }
  else if (time < 18000)
  {
    half_half_flash();
  }
  else sequenceStartTime = millis();
}
void traffic_warning() {
  switch (warning_count) {
    case 1:
      even_odd();
      break;
    case 2:
      even_odd_flash();
      break;
    case 3:
      half_half();
      break;
    case 4:
      half_half_flash();
      break;
    case 5:
      two_by_two();
      break;
    case 6:
      tow_by_two_flash();
      break;
    case 7:
      two_by_two_parallel();
      break;
    case 8:
      two_by_two_parallel_flash();
      break;
    case 9:
      three_by_one();
      break;
    case 10:
      three_by_one_flash();
      break;
    case 11:
      outboard();
      break;
    case 12:
      outboard_flash();
      break;
    case 13:
      inboard_6();
      break;
    case 14:
      inboard_6_flash();
      break;
    case 15:
      solid();
      break;
    case 16:
      solid_flash();
      break;
    case 17:
      solid_flash_pause();
      break;
    case 18:
      multi_pattern();
      break;
  }
}
void even_odd() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  long time = millis() - sequenceStartTime;
  if (time < 300)
  {
    digitalWrite(12, HIGH);
    digitalWrite(11, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    digitalWrite(5, LOW);
  }
  else if (time < 600)
  {
    digitalWrite(12, LOW);
    digitalWrite(11, HIGH);
    digitalWrite(10, LOW);
    digitalWrite(9, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(7, HIGH);
    digitalWrite(6, LOW);
    digitalWrite(5, HIGH);
  }
  else sequenceStartTime = millis();
}
void even_odd_flash() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  digitalWrite(12, LEFT_state);
  digitalWrite(11, RIGHT_state);
  digitalWrite(10, LEFT_state);
  digitalWrite(9, RIGHT_state);
  digitalWrite(8, LEFT_state);
  digitalWrite(7, RIGHT_state);
  digitalWrite(6, LEFT_state);
  digitalWrite(5, RIGHT_state);
  if ((long)(millis() - waitUntilSwitch) >= 0) {
    LEFT_state = LOW;
    RIGHT_state = LOW;
    whichLED = !whichLED;
    waitUntilSwitch += switchDelay;
  }
  if ((long)(millis() - strobeWait) >= 0) {
    if (whichLED == LEFTside)
      LEFT_state = !LEFT_state;
    if (whichLED == RIGHTside)
      RIGHT_state = !RIGHT_state;
    strobeWait += strobeDelay;
  }
}
void half_half() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  long time = millis() - sequenceStartTime;
  if (time < 250)
  {
    digitalWrite(12, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(7, LOW);
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
  }
  else if (time < 500)
  {
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(5, HIGH);
  }
  else sequenceStartTime = millis();
}
void half_half_flash() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  digitalWrite(12, LEFT_state);
  digitalWrite(11, LEFT_state);
  digitalWrite(10, LEFT_state);
  digitalWrite(9, LEFT_state);
  digitalWrite(8, RIGHT_state);
  digitalWrite(7, RIGHT_state);
  digitalWrite(6, RIGHT_state);
  digitalWrite(5, RIGHT_state);
  if ((long)(millis() - waitUntilSwitch) >= 0) {
    LEFT_state = LOW;
    RIGHT_state = LOW;
    whichLED = !whichLED;
    waitUntilSwitch += switchDelay;
  }
  if ((long)(millis() - strobeWait) >= 0) {
    if (whichLED == LEFTside)
      LEFT_state = !LEFT_state;
    if (whichLED == RIGHTside)
      RIGHT_state = !RIGHT_state;
    strobeWait += strobeDelay;
  }
}
void two_by_two() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  long time = millis() - sequenceStartTime;
  if (time < 150)
  {
    digitalWrite(12, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    digitalWrite(5, HIGH);
  }
  else if (time < 300)
  {
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
  }
  else sequenceStartTime = millis();
}
void tow_by_two_flash() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  digitalWrite(12, LEFT_state);
  digitalWrite(11, LEFT_state);
  digitalWrite(10, RIGHT_state);
  digitalWrite(9, RIGHT_state);
  digitalWrite(8, RIGHT_state);
  digitalWrite(7, RIGHT_state);
  digitalWrite(6, LEFT_state);
  digitalWrite(5, LEFT_state);
  if ((long)(millis() - waitUntilSwitch) >= 0) {
    LEFT_state = LOW;
    RIGHT_state = LOW;
    whichLED = !whichLED;
    waitUntilSwitch += switchDelay;
  }
  if ((long)(millis() - strobeWait) >= 0) {
    if (whichLED == LEFTside)
      LEFT_state = !LEFT_state;
    if (whichLED == RIGHTside)
      RIGHT_state = !RIGHT_state;
    strobeWait += strobeDelay;
  }
}
void three_by_one() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  long time = millis() - sequenceStartTime;
  if (time < 400)
  {
    digitalWrite(12, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(7, LOW);
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
  }
  else if (time < 800)
  {
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
    digitalWrite(9, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(7, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(5, HIGH);
  }
  else sequenceStartTime = millis();
}
void three_by_one_flash() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  digitalWrite(12, RIGHT_state);
  digitalWrite(11, RIGHT_state);
  digitalWrite(10, RIGHT_state);
  digitalWrite(8, RIGHT_state);
  digitalWrite(9, LEFT_state);
  digitalWrite(7, LEFT_state);
  digitalWrite(6, LEFT_state);
  digitalWrite(5, LEFT_state);
  if ((long)(millis() - waitUntilSwitch) >= 0) {
    LEFT_state = LOW;
    RIGHT_state = LOW;
    whichLED = !whichLED;
    waitUntilSwitch += switchDelay;
  }
  if ((long)(millis() - strobeWait) >= 0) {
    if (whichLED == LEFTside)
      LEFT_state = !LEFT_state;
    if (whichLED == RIGHTside)
      RIGHT_state = !RIGHT_state;
    strobeWait += strobeDelay;
  }
}
void two_by_two_parallel() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  long time = millis() - sequenceStartTime;
  if (time < 150)
  {
    digitalWrite(12, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
  }
  else if (time < 300)
  {
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    digitalWrite(5, HIGH);
  }
  else sequenceStartTime = millis();
}
void two_by_two_parallel_flash() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  digitalWrite(12, LEFT_state);
  digitalWrite(11, LEFT_state);
  digitalWrite(10, RIGHT_state);
  digitalWrite(9, RIGHT_state);
  digitalWrite(8, LEFT_state);
  digitalWrite(7, LEFT_state);
  digitalWrite(6, RIGHT_state);
  digitalWrite(5, RIGHT_state);
  if ((long)(millis() - waitUntilSwitch) >= 0) {
    LEFT_state = LOW;
    RIGHT_state = LOW;
    whichLED = !whichLED;
    waitUntilSwitch += switchDelay;
  }
  if ((long)(millis() - strobeWait) >= 0) {
    if (whichLED == LEFTside)
      LEFT_state = !LEFT_state;
    if (whichLED == RIGHTside)
      RIGHT_state = !RIGHT_state;
    strobeWait += strobeDelay;
  }
}
void outboard() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  long time = millis() - sequenceStartTime;
  if (time < 300)
  {
    digitalWrite(12, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    digitalWrite(7, LOW);
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
  }
  else if (time < 600)
  {
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    digitalWrite(5, HIGH);
  }
  else sequenceStartTime = millis();
}
void outboard_flash() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  digitalWrite(12, RIGHT_state);
  digitalWrite(11, RIGHT_state);
  digitalWrite(6, LEFT_state);
  digitalWrite(5, LEFT_state);
  if ((long)(millis() - waitUntilSwitch) >= 0) {
    LEFT_state = LOW;
    RIGHT_state = LOW;
    whichLED = !whichLED;
    waitUntilSwitch += switchDelay;
  }
  if ((long)(millis() - strobeWait) >= 0) {
    if (whichLED == LEFTside)
      LEFT_state = !LEFT_state;
    if (whichLED == RIGHTside)
      RIGHT_state = !RIGHT_state;
    strobeWait += strobeDelay;
  }
}
void inboard_6() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  long time = millis() - sequenceStartTime;
  if (time < 200)
  {
    digitalWrite(11, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    digitalWrite(7, HIGH);
    digitalWrite(6, HIGH);
  }
  else if (time < 400)
  {
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
    digitalWrite(9, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(7, LOW);
    digitalWrite(6, LOW);
  }
  else sequenceStartTime = millis();
}
void inboard_6_flash() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  digitalWrite(11, LEFT_state);
  digitalWrite(10, LEFT_state);
  digitalWrite(9, RIGHT_state);
  digitalWrite(8, RIGHT_state);
  digitalWrite(7, LEFT_state);
  digitalWrite(6, LEFT_state);
  if ((long)(millis() - waitUntilSwitch) >= 0) {
    LEFT_state = LOW;
    RIGHT_state = LOW;
    whichLED = !whichLED;
    waitUntilSwitch += switchDelay;
  }
  if ((long)(millis() - strobeWait) >= 0) {
    if (whichLED == LEFTside)
      LEFT_state = !LEFT_state;
    if (whichLED == RIGHTside)
      RIGHT_state = !RIGHT_state;
    strobeWait += strobeDelay;
  }
}
void solid() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  long time = millis() - sequenceStartTime;
  if (time < 800)
  {
    digitalWrite(12, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(5, HIGH);
  }
  else if (time < 1600)
  {
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    digitalWrite(7, LOW);
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
  }
  else sequenceStartTime = millis();
}
void solid_flash() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  long time = millis() - sequenceStartTime;
  if (time < 200)
  {
    digitalWrite(12, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(5, HIGH);
  }
  else if (time < 400)
  {
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    digitalWrite(7, LOW);
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
  }
  else sequenceStartTime = millis();
}
void solid_flash_pause() {
  for (int i = 0; i <= 7; i++) {
    digitalWrite(LEDright[i], LOW);
  }
  digitalWrite(12, LEFT_state);
  digitalWrite(11, LEFT_state);
  digitalWrite(10, LEFT_state);
  digitalWrite(9, LEFT_state);
  digitalWrite(8, LEFT_state);
  digitalWrite(7, LEFT_state);
  digitalWrite(6, LEFT_state);
  digitalWrite(5, LEFT_state);
  if ((long)(millis() - waitUntilSwitch) >= 0) {
    LEFT_state = LOW;
    RIGHT_state = LOW;
    whichLED = !whichLED;
    waitUntilSwitch += switchDelay3;
  }
  if ((long)(millis() - strobeWait) >= 0) {
    if (whichLED == LEFTside)
      LEFT_state = !LEFT_state;
    if (whichLED == RIGHTside)
      RIGHT_state = !RIGHT_state;
    strobeWait += strobeDelay;
  }
}
void left_arrow() {
  unsigned long currenttimer = millis();
  if (currenttimer - lasttimer >= timer) {
    lasttimer = currenttimer;
    for (int i = 0; i <= 7; i++) {
    }
    light = light + counter;
    if (light > 7) {
      arrow_state = !arrow_state;
      light = 0;
      counter = 1;
    }
    if (arrow_state == HIGH) {
      digitalWrite(LEDleft[light], HIGH);
    }
    else if (arrow_state == LOW) {
      digitalWrite(LEDleft[light], LOW);
    }
  }
}
void right_arrow() {
  unsigned long currenttimer = millis();
  if (currenttimer - lasttimer >= timer) {
    lasttimer = currenttimer;
    for (int i = 0; i <= 7; i++) {
    }
    light = light + counter;
    if (light > 7) {
      arrow_state = !arrow_state;
      light = 0;
      counter = 1;
    }
    if (arrow_state == HIGH) {
      digitalWrite(LEDright[light], HIGH);
    }
    else if (arrow_state == LOW) {
      digitalWrite(LEDright[light], LOW);
    }
  }
}
void center_arrow() {
  unsigned long currenttimer = millis();
  if (currenttimer - lasttimer >= timer2) {
    lasttimer = currenttimer;
    for (int i = 0; i <= 7; i++) {
    }
    light = light + counter;
    if (light > 3) {
      arrow_state = !arrow_state;
      light = 0;
      counter = 1;
    }
    if (arrow_state == HIGH) {
      digitalWrite(centerright[light], HIGH);
      digitalWrite(centerleft[light], HIGH);
    }
    if (arrow_state == LOW) {
      digitalWrite(centerright[light], LOW);
      digitalWrite(centerleft[light], LOW);
    }
  }
}
void multi_pattern(){
  long time = millis() - sequenceStartTime2;
  if (time < 4000)
  {
    half_half_flash();
  }
  else if (time < 8000)
  {
    outboard();
  }
  else if (time < 12000)
  {
    two_by_two_parallel_flash();
  }
  else if (time < 16000)
  {
    solid_flash_pause();
  }
  else if (time < 20000)
  {
    outboard_flash();
  }
  else if (time < 24000)
  {
    tow_by_two_flash();
  }
  else if (time < 28000)
  {
    half_half();
  }
  else if (time < 32000)
  {
    two_by_two_parallel();
  }
  else sequenceStartTime2 = millis();
}

I did not understand the roll of one of the keypad handlers

    if (0) // hand control this outlier
      if (ButtonPress[pressedKey] == "Change") {
        delay(500);
        warning_count++;
        if (warning_count > 18) {
          warning_count = 1;
        }
        // already is boss      gotKey = false;
      }

which breaks an exploitable pattern, and doesn't seem to do anything I can see, so I just if (0)'d the statment that does the work, effectivley ignoring that key.

Obvsly I am out the door before I can test this, but it seems to function. When I am next in the lab, I will not be able to resist reworking the top level code, and I think there is tempting temptation for looking at 100s of lines of code that may do what @d.raymond wants them to, but might be fun to do differently.

Fun in my empty life.

But it is a spectacular day already too much slipped away while we indoors, so.

a7

Thank you all for the suggestions! it ended up pointing me in the right direction and all i had to do was play around.

the key seemed to be treating the matrix keypad as pull up buttons and ignoring the keypad library, after i did that i realized that after i press a button, it constantly returns that value over and over, so i modified it to only capture one press then pause for another press.

once there, i had to create a robust state machine to keep track of everything i wanted to do and use multiple select case and if statements to choose the right action.

I was even able to add a few more options after i got the organization right.

full NEW code with solution below:

#include <EEPROM.h>

#define LEFTside 0x0
#define RIGHTside 0x1

byte whichLED = LEFTside;
byte LEFT_state = LOW;
byte RIGHT_state = LOW;
byte arrow_state = HIGH;

int statuslight = 13;
int light = 0;
int counter = 1;
int warning_count = EEPROM.read(0);
int left_count = 1;
int center_count = 1;
int right_count = 1;

const int LEDleft[] = {5, 6, 7, 8, 9, 10, 11, 12};
const int LEDright[] = {12, 11, 10, 9, 8, 7, 6, 5};
const int centerleft[] = {9, 10, 11, 12};
const int centerright[] = {8, 7, 6, 5};
const int rowPins[2] = {3, 2};
const int colPins[3] = {A4, A3, A2};

char keys[2][3] = {
    {1, 2, 3},
    {4, 5, 6}};

long lasttimer = 0;

static long timer = 100;
static long timer2 = 200;

unsigned long strobeDelay = 75;
unsigned long switchDelay = 500;
unsigned long switchDelay2 = 1000;
unsigned long switchDelay3 = 450;
unsigned long strobeWait = strobeDelay;
unsigned long waitUntilSwitch = switchDelay;
unsigned long sequenceStartTime;
unsigned long sequenceStartTime2;

bool warn = false;
bool left = false;
bool center = false;
bool right = false;

void setup(){
  Serial.begin(9600);
  pinMode(statuslight, 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);

  for (int i = 0; i < 2; i++)
  {
    pinMode(rowPins[i], INPUT_PULLUP);
  }

  for (int i = 0; i < 3; i++)
  {
    pinMode(colPins[i], OUTPUT);
    digitalWrite(colPins[i], HIGH);
  }
}
char getKey(){
  for (int i = 0; i < 3; i++)
  {
    digitalWrite(colPins[i], LOW);
    for (int j = 0; j < 2; j++)
    {
      if (digitalRead(rowPins[j]) == LOW)
      {
        char key = keys[j][i];
        digitalWrite(colPins[i], HIGH);
        return key;
      }
    }
    digitalWrite(colPins[i], HIGH);
  }

  return NULL;
}
void loop(){
  static bool keyPressed = false;
  char key = getKey();

  if (key != NULL)
  {
    switch (key)
    {
    case 1:
      delay(500);
      pattern_off();
      warn = false;
      left = false;
      center = false;
      right = false;
      keyPressed = true;
      break;

    case 2:
      delay(500);
      warn = true;
      left = false;
      center = false;
      right = false;
      keyPressed = true;
      break;

    case 3:
      delay(500);
      advance_select();
      keyPressed = true;
      break;

    case 4:
      delay(500);
      left = true;
      warn = false;
      center = false;
      right = false;
      keyPressed = true;
      break;

    case 5:
      delay(500);
      center = true;
      warn = false;
      left = false;
      right = false;
      keyPressed = true;
      break;

    case 6:
      delay(500);
      right = true;
      warn = false;
      left = false;
      center = false;
      keyPressed = true;
      break;
    }
  }
  if (keyPressed && getKey() == NULL)
  {
    keyPressed = false;
  }
  if (warn == true)
  {
    pattern_select();
  }
  if (left == true)
  {
    left_arrow_select();
  }
  if (center == true)
  {
    center_arrow_select();
  }
  if (right == true)
  {
    right_arrow_select();
  }
}
void pattern_off(){
  for (int i = 0; i <= 7; i++)
  {
    digitalWrite(LEDright[i], LOW);
  }
}
void advance_select(){
  if (warn == true)
  {
    warning_count++;
    if (warning_count > 10)
    {
      warning_count = 1;
    }
  }
  if (left == true)
  {
    left_count++;
    if (left_count>3)
    {
      left_count = 1;
    }
  }
  if (center == true)
  {
    center_count++;
    if (center_count>3)
    {
      center_count = 1;
    }
  }
  if (right == true)
  {
    right_count++;
    if (right_count>3)
    {
      right_count = 1;
    }
  }
}
void pattern_select(){
  switch (warning_count)
  {
  case 1:
    multi_pattern();
    break;
  case 2:
    tow_by_two_flash();
    break;
  case 3:
    two_by_two_parallel();
    break;
  case 4:
    two_by_two_parallel_flash();
    break;
  case 5:
    three_by_one_flash();
    break;
  case 6:
    half_half();
    break;
  case 7:
    half_half_flash();
    break;
  case 8:
    outboard();
    break;
  case 9:
    outboard_flash();
    break;
  case 10:
    solid_flash_pause();
    break;
  }
}
void left_arrow_select(){
  switch (left_count)
  {
  case 1:
    left_arrow();
    break;
  case 2:
    left_arrow_single();
    break;
  case 3:
    left_arrow_triple();
    break;
  }
}
void right_arrow_select(){
  switch (right_count)
  {
  case 1:
    right_arrow();
    break;
  case 2:
    right_arrow_single();
    break;
  case 3:
    right_arrow_triple();
    break;
  }
}
void center_arrow_select(){
  switch (center_count)
  {
  case 1:
    center_arrow();
    break;
  case 2:
    center_arrow();
    break;
  case 3:
    center_arrow();
    break;
  }
}
void multi_pattern(){
  long time = millis() - sequenceStartTime2;
  if (time < 4000)
  {
    half_half_flash();
  }
  else if (time < 8000)
  {
    outboard();
  }
  else if (time < 12000)
  {
    two_by_two_parallel_flash();
  }
  else if (time < 16000)
  {
    solid_flash_pause();
  }
  else if (time < 20000)
  {
    outboard_flash();
  }
  else if (time < 24000)
  {
    tow_by_two_flash();
  }
  else if (time < 28000)
  {
    half_half();
  }
  else if (time < 32000)
  {
    two_by_two_parallel();
  }
  else if (time < 36000)
  {
    three_by_one_flash();
  }
  else
    sequenceStartTime2 = millis();}
void half_half(){
  long time = millis() - sequenceStartTime;
  if (time < 250)
  {
    digitalWrite(12, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(7, LOW);
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
  }
  else if (time < 500)
  {
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(5, HIGH);
  }
  else
    sequenceStartTime = millis();}

void half_half_flash(){
  digitalWrite(12, LEFT_state);
  digitalWrite(11, LEFT_state);
  digitalWrite(10, LEFT_state);
  digitalWrite(9, LEFT_state);
  digitalWrite(8, RIGHT_state);
  digitalWrite(7, RIGHT_state);
  digitalWrite(6, RIGHT_state);
  digitalWrite(5, RIGHT_state);
  if ((long)(millis() - waitUntilSwitch) >= 0)
  {
    LEFT_state = LOW;
    RIGHT_state = LOW;
    whichLED = !whichLED;
    waitUntilSwitch += switchDelay;
  }
  if ((long)(millis() - strobeWait) >= 0)
  {
    if (whichLED == LEFTside)
      LEFT_state = !LEFT_state;
    if (whichLED == RIGHTside)
      RIGHT_state = !RIGHT_state;
    strobeWait += strobeDelay;
  }}

void tow_by_two_flash(){
  digitalWrite(12, LEFT_state);
  digitalWrite(11, LEFT_state);
  digitalWrite(10, RIGHT_state);
  digitalWrite(9, RIGHT_state);
  digitalWrite(8, RIGHT_state);
  digitalWrite(7, RIGHT_state);
  digitalWrite(6, LEFT_state);
  digitalWrite(5, LEFT_state);
  if ((long)(millis() - waitUntilSwitch) >= 0)
  {
    LEFT_state = LOW;
    RIGHT_state = LOW;
    whichLED = !whichLED;
    waitUntilSwitch += switchDelay;
  }
  if ((long)(millis() - strobeWait) >= 0)
  {
    if (whichLED == LEFTside)
      LEFT_state = !LEFT_state;
    if (whichLED == RIGHTside)
      RIGHT_state = !RIGHT_state;
    strobeWait += strobeDelay;
  }}

void three_by_one_flash(){
  digitalWrite(12, RIGHT_state);
  digitalWrite(11, RIGHT_state);
  digitalWrite(10, RIGHT_state);
  digitalWrite(8, RIGHT_state);
  digitalWrite(9, LEFT_state);
  digitalWrite(7, LEFT_state);
  digitalWrite(6, LEFT_state);
  digitalWrite(5, LEFT_state);
  if ((long)(millis() - waitUntilSwitch) >= 0)
  {
    LEFT_state = LOW;
    RIGHT_state = LOW;
    whichLED = !whichLED;
    waitUntilSwitch += switchDelay;
  }
  if ((long)(millis() - strobeWait) >= 0)
  {
    if (whichLED == LEFTside)
      LEFT_state = !LEFT_state;
    if (whichLED == RIGHTside)
      RIGHT_state = !RIGHT_state;
    strobeWait += strobeDelay;
  }}

void two_by_two_parallel(){
  long time = millis() - sequenceStartTime;
  if (time < 150)
  {
    digitalWrite(12, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
  }
  else if (time < 300)
  {
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    digitalWrite(5, HIGH);
  }
  else
    sequenceStartTime = millis();}

void two_by_two_parallel_flash(){
  digitalWrite(12, LEFT_state);
  digitalWrite(11, LEFT_state);
  digitalWrite(10, RIGHT_state);
  digitalWrite(9, RIGHT_state);
  digitalWrite(8, LEFT_state);
  digitalWrite(7, LEFT_state);
  digitalWrite(6, RIGHT_state);
  digitalWrite(5, RIGHT_state);
  if ((long)(millis() - waitUntilSwitch) >= 0)
  {
    LEFT_state = LOW;
    RIGHT_state = LOW;
    whichLED = !whichLED;
    waitUntilSwitch += switchDelay;
  }
  if ((long)(millis() - strobeWait) >= 0)
  {
    if (whichLED == LEFTside)
      LEFT_state = !LEFT_state;
    if (whichLED == RIGHTside)
      RIGHT_state = !RIGHT_state;
    strobeWait += strobeDelay;
  }}

void outboard(){
  long time = millis() - sequenceStartTime;
  if (time < 300)
  {
    digitalWrite(12, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    digitalWrite(7, LOW);
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
  }
  else if (time < 600)
  {
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    digitalWrite(5, HIGH);
  }
  else
    sequenceStartTime = millis();}

void outboard_flash(){
  digitalWrite(12, RIGHT_state);
  digitalWrite(11, RIGHT_state);
  digitalWrite(6, LEFT_state);
  digitalWrite(5, LEFT_state);
  if ((long)(millis() - waitUntilSwitch) >= 0)
  {
    LEFT_state = LOW;
    RIGHT_state = LOW;
    whichLED = !whichLED;
    waitUntilSwitch += switchDelay;
  }
  if ((long)(millis() - strobeWait) >= 0)
  {
    if (whichLED == LEFTside)
      LEFT_state = !LEFT_state;
    if (whichLED == RIGHTside)
      RIGHT_state = !RIGHT_state;
    strobeWait += strobeDelay;
  }}

void solid_flash_pause(){
  digitalWrite(12, LEFT_state);
  digitalWrite(11, LEFT_state);
  digitalWrite(10, LEFT_state);
  digitalWrite(9, LEFT_state);
  digitalWrite(8, LEFT_state);
  digitalWrite(7, LEFT_state);
  digitalWrite(6, LEFT_state);
  digitalWrite(5, LEFT_state);
  if ((long)(millis() - waitUntilSwitch) >= 0)
  {
    LEFT_state = LOW;
    RIGHT_state = LOW;
    whichLED = !whichLED;
    waitUntilSwitch += switchDelay3;
  }
  if ((long)(millis() - strobeWait) >= 0)
  {
    if (whichLED == LEFTside)
      LEFT_state = !LEFT_state;
    if (whichLED == RIGHTside)
      RIGHT_state = !RIGHT_state;
    strobeWait += strobeDelay;
  }
}
void left_arrow(){
  unsigned long currenttimer = millis();
  if (currenttimer - lasttimer >= timer)
  {
    lasttimer = currenttimer;
    for (int i = 0; i <= 7; i++)
    {
    }
    light = light + counter;
    if (light > 7)
    {
      arrow_state = !arrow_state;
      light = 0;
      counter = 1;
    }
    if (arrow_state == HIGH)
    {
      digitalWrite(LEDleft[light], HIGH);
    }
    else if (arrow_state == LOW)
    {
      digitalWrite(LEDleft[light], LOW);
    }
  }}
void left_arrow_single(){
  unsigned long currenttimer = millis();
  if (currenttimer - lasttimer >= timer)
  {
    lasttimer = currenttimer;
    for (int i = 0; i <= 7; i++)
    {
      digitalWrite(LEDleft[i], LOW);
    }
    light = light + counter;
    if (light > 7)
    {
      arrow_state = !arrow_state;
      light = 0;
      counter = 1;
    }
    if (arrow_state == HIGH)
    {
      digitalWrite(LEDleft[light], HIGH);
    }
    else if (arrow_state == LOW)
    {
      digitalWrite(LEDleft[light], LOW);
    }
  }}

void left_arrow_triple(){
  unsigned long currenttimer = millis();
  if (currenttimer - lasttimer >= timer)
  {
    lasttimer = currenttimer;
    for (int i = 0; i <= 7; i++)
    {
      digitalWrite(LEDleft[i], LOW);
    }

    for (int j = 0; j < 3; j++)
    {
      digitalWrite(LEDleft[light], HIGH);
      delay(100);
      digitalWrite(LEDleft[light], LOW);
      delay(100);
    }

    light = light + counter;
    if (light > 7)
    {
      arrow_state = !arrow_state;
      light = 0;
      counter = 1;
    }

    if (arrow_state == HIGH)
    {
      digitalWrite(LEDleft[light], HIGH);
    }
    else if (arrow_state == LOW)
    {
      digitalWrite(LEDleft[light], LOW);
    }
  }
}
void right_arrow(){
  unsigned long currenttimer = millis();
  if (currenttimer - lasttimer >= timer)
  {
    lasttimer = currenttimer;
    for (int i = 0; i <= 7; i++)
    {
    }
    light = light + counter;
    if (light > 7)
    {
      arrow_state = !arrow_state;
      light = 0;
      counter = 1;
    }
    if (arrow_state == HIGH)
    {
      digitalWrite(LEDright[light], HIGH);
    }
    else if (arrow_state == LOW)
    {
      digitalWrite(LEDright[light], LOW);
    }
  }}

void right_arrow_single(){
  unsigned long currenttimer = millis();
  if (currenttimer - lasttimer >= timer)
  {
    lasttimer = currenttimer;
    for (int i = 7; i >= 0; i--)
    {
      digitalWrite(LEDright[i], LOW);
    }
    light = light + counter;
    if (light > 7)
    {
      arrow_state = !arrow_state;
      light = 0;
      counter = 1;
    }
    if (arrow_state == HIGH)
    {
      digitalWrite(LEDright[light], HIGH);
    }
    else if (arrow_state == LOW)
    {
      digitalWrite(LEDright[light], LOW);
    }
  }}

void right_arrow_triple(){
  unsigned long currenttimer = millis();
  if (currenttimer - lasttimer >= timer)
  {
    lasttimer = currenttimer;
    for (int i = 7; i >= 0; i--)
    {
      digitalWrite(LEDright[i], LOW);
    }

    for (int j = 0; j < 3; j++)
    {
      digitalWrite(LEDright[light], HIGH);
      delay(100);
      digitalWrite(LEDright[light], LOW);
      delay(100);
    }

    light = light + counter;
    if (light > 7)
    {
      arrow_state = !arrow_state;
      light = 0;
      counter = 1;
    }

    if (arrow_state == HIGH)
    {
      digitalWrite(LEDright[light], HIGH);
    }
    else if (arrow_state == LOW)
    {
      digitalWrite(LEDright[light], LOW);
    }
  }
}
void center_arrow(){
  unsigned long currenttimer = millis();
  if (currenttimer - lasttimer >= timer2)
  {
    lasttimer = currenttimer;
    for (int i = 0; i <= 7; i++)
    {
    }
    light = light + counter;
    if (light > 3)
    {
      arrow_state = !arrow_state;
      light = 0;
      counter = 1;
    }
    if (arrow_state == HIGH)
    {
      digitalWrite(centerright[light], HIGH);
      digitalWrite(centerleft[light], HIGH);
    }
    if (arrow_state == LOW)
    {
      digitalWrite(centerright[light], LOW);
      digitalWrite(centerleft[light], LOW);
    }
  }}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.