/*
  Multi state button example with debounce code.
  Use as a baseline to build a single button menu.
  Switch case allows user to toggle through various options.
*/



uint8_t buttonRead;
uint8_t oldButtonRead = 0;
const uint8_t debounceTime = 10;  // milliseconds
uint8_t pattern = 0;
uint8_t i;

const uint8_t buttonPin = 8; // button input pin

uint8_t IO_Pins[] = {3, 4, 5, 6, 7}; // Led pins

void setup() {
  Serial.begin(9600);
  for (i = 0; i < sizeof(IO_Pins) / sizeof(IO_Pins[0]); i++) {
    pinMode(IO_Pins[i], OUTPUT);      
  }
  pinMode(buttonPin, INPUT_PULLUP); // will read as 1 with pullup resistor
}

void loop() {
  buttonRead = digitalRead(buttonPin); // buttonRead will start as 1
  // - - - - Debounce Section - - - -
  // Serial.print("buttonRead: "); Serial.println(buttonRead);
  // Serial.print("oldButtonRead: "); Serial.println(oldButtonRead);
  // Serial.print("pattern: "); Serial.println(pattern);
  if (buttonRead != oldButtonRead) {
    oldButtonRead = buttonRead;
    // Serial.print("if buttonRead: "); Serial.println(buttonRead);
    // Serial.print("if oldButtonRead: "); Serial.println(oldButtonRead);
    delay (debounceTime);

    if (buttonRead == 0) {
      if (pattern >= 7) pattern = 0;
      else pattern++;
      Serial.print("pattern: "); Serial.println(pattern);
    }

    switch (pattern) {
      case 0:
        for (i = 0; i < 5; i++) {
          digitalWrite(IO_Pins[i], LOW);
        }
        break;
      case 1:
        digitalWrite(IO_Pins[0], HIGH);
        digitalWrite(IO_Pins[1], LOW);
        digitalWrite(IO_Pins[2], LOW);
        digitalWrite(IO_Pins[3], LOW);
        digitalWrite(IO_Pins[4], LOW);
        break;
      case 2:
        digitalWrite(IO_Pins[0], LOW);
        digitalWrite(IO_Pins[1], HIGH);
        digitalWrite(IO_Pins[2], LOW);
        digitalWrite(IO_Pins[3], LOW);
        digitalWrite(IO_Pins[4], LOW);
        break;
      case 3: // using a nested for loop also works!
        for (int i = 0; i < 5; i++) {
          digitalWrite(IO_Pins[i], LOW);
          for (int j = 2; j < 3; j++){
            digitalWrite(IO_Pins[j], HIGH);
          }
        }
        break;
      case 4:
        digitalWrite(IO_Pins[0], LOW);
        digitalWrite(IO_Pins[1], LOW);
        digitalWrite(IO_Pins[2], LOW);
        digitalWrite(IO_Pins[3], HIGH);
        digitalWrite(IO_Pins[4], LOW);
        break;
      case 5:
        digitalWrite(IO_Pins[0], LOW);
        digitalWrite(IO_Pins[1], LOW);
        digitalWrite(IO_Pins[2], LOW);
        digitalWrite(IO_Pins[3], LOW);
        digitalWrite(IO_Pins[4], HIGH);
        break;
      case 6:
        for (i = 0; i < 5; i++) {
          digitalWrite(IO_Pins[i], HIGH);
        }
        break;
      case 7:
        for (i = 0; i < 5; i++) {
          digitalWrite(IO_Pins[i], HIGH);
          delay(100);
          digitalWrite(IO_Pins[i], LOW);
          delay(100);
        }
       
        for (i = 5; i > 0; i--) {
          digitalWrite(IO_Pins[i-1], HIGH);
          delay(100);
          digitalWrite(IO_Pins[i-1], LOW);
          delay(100);
        }
        break;
    }
  }
  delay(50);
}