SOLVED: Button Logic Problem

Ok so I've run into an issue and I can't seem to get the logic of the buttons to work. The first section of code works, however the issue is INPUT 0 is for the first button and I'd like to add more than just two states to the button, similar to what INPUTS 1 & 2 do in the code.

Old working code:

void updateLEDState(int input) {
  // input 0 (Mode) = State 0 and 1
  if(input == 0) {
    if(LEDState == 0) {
      LEDState = 1;
    }else{
      LEDState = 0;
    }
  // input 1 (Color) = State 2 to 7
  }else if(input == 1) { // 2,3,4,5,6,7,2,3,4,5,6,7...
    if(LEDState == 0 || LEDState == 1 || LEDState > 6) {
      LEDState = 2;
    }else{
      LEDState++;
    }
  // input 2 (Brightness) = state 8 to 12
  }else if(input == 2) { //test for states 8 through 12 to change brightness
    if(LEDState == 0 || LEDState == 1 || LEDState < 7 || LEDState > 12) {
      LEDState = 8;
    }else{
      LEDState++;
    }
  }
}

The code below is where i'm currently stuck, basically i'm trying to add 3 more states to the first button, however I can't get the logic to work out now. I'm unsure of how to structure the IF statements for their respective inputs to make it so that when the button is pressed and 'LEDState' is outside of the inputs respective range that it resets and starts from the beginning. Can anyone help me sort this out?

void updateLEDState(int input) {
  // input 0 (Mode) = State 0 to 4
  if(input == 0) { //test for states 0 through 4 to change modes
    if(LEDState > 3) { //0,1,2,3,4,0,1,2,3,4...
      LEDState = 0;
    }else{
      LEDState++;
    }
  // input 1 (Color) = State 5 to 10
  }else if(input == 1) { //test for states 5 through 10 to change color
    if(LEDState < 4 || LEDState > 10) { //5,6,7,8,9,10,5,6,7,8,9,10...
      LEDState = 5;
    }else{
      LEDState++;
    }
  // input 2 (Brightness) = state 11 to 15
  }else if(input == 2) { //test for states 11 through 15 to change brightness
    if(LEDState < 10 || LEDState > 14) { //11,12,13,14,15,11,12,13,14,15...
      LEDState = 11;
    }else{
      LEDState++;
    }
  }
}

Whenever I watch the code using Serial.print for each input it seems to do odd things, like input 0 going from LEDState 0 to LEDState 11 in one press, or input 1 will skip state 5 and go 6,7,8,9,10,10,6 instead of 5 through 10 and then reset back to 5.

Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]

BTW, Follow this example:

if (condition1)
{
// do Thing A
}
else if (condition2)
{
// do Thing B
}
else
{
// do Thing C
}

larryd:
Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]

I'm using the code tags in my post...also your example doesn't help as I'm already using that structure. I'm concerned with the IF statements below:

if (LEDState > 3)
if (LEDState < 4 || LEDState > 10)
if(LEDState < 10 || LEDState > 14)

I'm trying to get the logic in those lines to have the desired effect, which I described in the original post.

Here is my current code again, formatted with CTRL-T:

void updateLEDState(int input) {
  // input 0 (Mode) = State 0 to 4
  if (input == 0) { //test for states 0 through 4 to change modes
    if (LEDState > 3) { //0,1,2,3,4,0,1,2,3,4...
      LEDState = 0;
    } else {
      LEDState++;
    }
    // input 1 (Color) = State 5 to 10
  } else if (input == 1) { //test for states 5 through 10 to change color
    if (LEDState < 4 || LEDState > 10) { //5,6,7,8,9,10,5,6,7,8,9,10...
      LEDState = 5;
    } else {
      LEDState++;
    }
    // input 2 (Brightness) = state 11 to 15
  } else if (input == 2) { //test for states 11 through 15 to change brightness
    if (LEDState < 10 || LEDState > 14) { //11,12,13,14,15,11,12,13,14,15...
      LEDState = 11;
    } else {
      LEDState++;
    }
  }
}

Here is the old code that works but without the changes I need:

void updateLEDState(int input) {
  // input 0 (Mode) = State 0 and 1
  if (input == 0) {
    if (LEDState == 0) {
      LEDState = 1;
    } else {
      LEDState = 0;
    }
    // input 1 (Color) = State 2 to 7
  } else if (input == 1) { // 2,3,4,5,6,7,2,3,4,5,6,7...
    if (LEDState == 0 || LEDState == 1 || LEDState > 6) {
      LEDState = 2;
    } else {
      LEDState++;
    }
    // input 2 (Brightness) = state 8 to 12
  } else if (input == 2) { //test for states 8 through 12 to change brightness
    if (LEDState == 0 || LEDState == 1 || LEDState < 7 || LEDState > 12) {
      LEDState = 8;
    } else {
      LEDState++;
    }
  }
}

If anyone that can help me with my specific question I would greatly appreciate it. General answers about code structure are not what i'm asking about...

I wasn't able to post my complete code in the previous response, so here it is. Although I'm only asking for help with the specific function 'updateLEDState'

#include "FastLED.h"
#include "EnableInterrupt.h"

#define NUM_LEDS 20
#define DATA_PIN 7
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define DEBOUNCE_DELAY 100 //ISR delay

// Define the array of leds
CRGB leds[NUM_LEDS];
CRGB row1[NUM_LEDS];
uint32_t LEDColor[] = {CRGB::Green, CRGB::Blue, CRGB::Purple, CRGB::Red, CRGB::Yellow, CRGB::White};
int NUM_COLOR = 1; //determine starting color from LEDColor array

//ISR variables
uint32_t last_interrupt_time = 0;
volatile byte eventModeFlag;
volatile byte eventColorFlag;
volatile byte eventBrightFlag;

//button variables
const int numOfInputs = 3;
const int inputPins[numOfInputs] = {3, 4, 5}; // pin3(mode), pin4(color), pin5(brightness)
const int outputPin = 6;
int LEDState = 0;
int inputState[numOfInputs];
int lastInputState[numOfInputs] = {LOW, LOW};
bool inputFlags[numOfInputs] = {LOW, LOW};
int inputCounters[numOfInputs];
long lastDebounceTime[numOfInputs] = {0, 0};
long debounceDelay = 50;

void setup() {
  for (int i = 0; i < numOfInputs; i++) {
    pinMode(inputPins[i], INPUT_PULLUP);
    //digitalWrite(inputPins[i], HIGH); // pull-up 20k
    FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
    LEDS.setBrightness(10);
    FastLED.show();
    enableInterrupt(3, isr_ModeHandler, RISING);
    enableInterrupt(4, isr_ColorHandler, RISING);
    enableInterrupt(5, isr_BrightnessHandler, RISING);
  }
  Serial.begin(9600);
  pinMode(outputPin, OUTPUT);
}

void loop() {
  setInputFlags();
  resolveInputFlags();
  resolveOutputs();
  eventModeFlag = 0; //reset eventFlags for next event.
  eventColorFlag = 0;
  eventBrightFlag = 0;
}

void setInputFlags() {
  for (int i = 0; i < numOfInputs; i++) {
    int reading = digitalRead(inputPins[i]);
    if (reading != lastInputState[i]) {
      lastDebounceTime[i] = millis();
    }
    if ((millis() - lastDebounceTime[i]) > debounceDelay) {
      if (reading != inputState[i]) {
        inputState[i] = reading;
        if (inputState[i] == HIGH) {
          inputFlags[i] = HIGH;
        }
      }
    }
    lastInputState[i] = reading;
  }
}

void resolveInputFlags() {
  for (int i = 0; i < numOfInputs; i++) {
    if (inputFlags[i] == HIGH) {
      // Input Toggle Logic
      inputCounters[i]++;
      updateLEDState(i);
      printString(i);
      inputFlags[i] = LOW;
    }
  }
}

void printString(int output) {
  //Serial.print("Input ");
  // Serial.print(output);
  // Serial.print(" was pressed ");
  // Serial.print(inputCounters[output]);
  // Serial.println(" times. ");
  // Serial.println("");
  //Serial.print(LEDState);
  //Serial.print("eventModeFlag =");
  //Serial.print(eventModeFlag);
  //Serial.println("");
}

void updateLEDState(int input) {
  // input 0 (Mode) = State 0 to 4
  if (input == 0) { //test for states 0 through 4 to change modes
    if (LEDState > 3) { //0,1,2,3,4,0,1,2,3,4...
      LEDState = 0;
    } else {
      LEDState++;
    }
    // input 1 (Color) = State 5 to 10
  } else if (input == 1) { //test for states 5 through 10 to change color
    if (LEDState < 4 || LEDState > 10) { //5,6,7,8,9,10,5,6,7,8,9,10...
      LEDState = 5;
    } else {
      LEDState++;
    }
    // input 2 (Brightness) = state 11 to 15
  } else if (input == 2) { //test for states 11 through 15 to change brightness
    if (LEDState < 10 || LEDState > 14) { //11,12,13,14,15,11,12,13,14,15...
      LEDState = 11;
    } else {
      LEDState++;
    }
  }
}

void resolveOutputs() {
  switch (LEDState) {

    // Mode change cases
    case 0:
      digitalWrite(outputPin, LOW); //turn LED off
      fill_solid(leds, NUM_LEDS, CRGB::Green);
      FastLED.show();
      break;
    case 1:
      digitalWrite(outputPin, HIGH); //turn LED on
      setSpinColors();
      spinMode();
      break;
    case 2:
      digitalWrite(outputPin, LOW); //turn LED off
      fill_solid(leds, NUM_LEDS, CRGB::Blue);
      FastLED.show();
      break;
    case 3:
      digitalWrite(outputPin, LOW); //turn LED off
      fill_solid(leds, NUM_LEDS, CRGB::Red);
      FastLED.show();
      break;
    case 4:
      digitalWrite(outputPin, LOW); //turn LED off
      fill_solid(leds, NUM_LEDS, CRGB::Pink);
      FastLED.show();
      break;

    //Color Change Cases
    case 5:
      fill_solid(leds, NUM_LEDS, CRGB::Pink); //change to green once testing done
      NUM_COLOR = 0;
      FastLED.show();
      break;
    case 6:
      fill_solid(leds, NUM_LEDS, CRGB::Blue);
      NUM_COLOR = 1;
      FastLED.show();
      break;
    case 7:
      fill_solid(leds, NUM_LEDS, CRGB::Purple);
      NUM_COLOR = 2;
      FastLED.show();
      break;
    case 8:
      fill_solid(leds, NUM_LEDS, CRGB::Red);
      NUM_COLOR = 3;
      FastLED.show();
      break;
    case 9:
      fill_solid(leds, NUM_LEDS, CRGB::Yellow);
      NUM_COLOR = 4;
      FastLED.show();
      break;
    case 10:
      fill_solid(leds, NUM_LEDS, CRGB::White);
      NUM_COLOR = 5;
      FastLED.show();
      break;

    // Brightness change cases
    case 11:
      fill_solid(leds, NUM_LEDS, LEDColor[NUM_COLOR]);
      LEDS.setBrightness(10);
      FastLED.show();
      break;
    case 12:
      fill_solid(leds, NUM_LEDS, LEDColor[NUM_COLOR]);
      LEDS.setBrightness(25);
      FastLED.show();
      break;
    case 13:
      fill_solid(leds, NUM_LEDS, LEDColor[NUM_COLOR]);
      LEDS.setBrightness(50);
      FastLED.show();
      break;
    case 14:
      fill_solid(leds, NUM_LEDS, LEDColor[NUM_COLOR]);
      LEDS.setBrightness(75);
      FastLED.show();
      break;
    case 15:
      fill_solid(leds, NUM_LEDS, LEDColor[NUM_COLOR]);
      LEDS.setBrightness(100);
      FastLED.show();
      break;
    default:
      break;
  }
}

void setSpinColors() {
  row1[0] = LEDColor[NUM_COLOR];
  row1[1] = LEDColor[NUM_COLOR];
  row1[2] = LEDColor[NUM_COLOR];
  row1[3] = CRGB::Black;
  row1[4] = CRGB::Black;
  row1[5] = CRGB::Black;
  row1[6] = CRGB::Black;
  row1[7] = CRGB::Black;
  row1[8] = CRGB::Black;
  row1[9] = CRGB::Black;
  row1[10] = CRGB::Black;
  row1[11] = CRGB::Black;
  row1[12] = LEDColor[NUM_COLOR];
  row1[13] = LEDColor[NUM_COLOR];
  row1[14] = LEDColor[NUM_COLOR];
  row1[15] = CRGB::Black;
  row1[16] = CRGB::Black;
  row1[17] = CRGB::Black;
  row1[18] = CRGB::Black;
  row1[19] = CRGB::Black;
  row1[20] = CRGB::Black;
}

void spinMode() {
  for (int j = 0; j < NUM_LEDS; j++)
    for (int i = 0; i < numOfInputs; i++)
      if (eventModeFlag == 0) {
        assignTo(j);
        FastLED.show();
        delay(10);
      }
      else if (eventModeFlag != 0) {
        int reading = digitalRead(inputPins[i]);
        if (reading != lastInputState[i]) {
          lastDebounceTime[i] = millis();
        }
        if ((millis() - lastDebounceTime[i]) > debounceDelay) {
          LEDState = 0; //must set the LEDState to the next 'mode' that would be in the sequence, in this case there are only two states; 0 and 1. So switch the state back to 0.
          loop(); //return back to main loop
        }
      }

  for (int i = 0; i < numOfInputs; i++) //color mode
    if (eventColorFlag == 1) {
      int reading = digitalRead(inputPins[i]);
      if (reading != lastInputState[i]) {
        lastDebounceTime[i] = millis();
      }
      if ((millis() - lastDebounceTime[i]) > debounceDelay) {
        LEDState = 2; //must set the LEDState to the start of the color cases
        loop();
      }
    }

  for (int i = 0; i < numOfInputs; i++) //brightness mode
    if (eventBrightFlag == 1) {
      int reading = digitalRead(inputPins[i]);
      if (reading != lastInputState[i]) {
        lastDebounceTime[i] = millis();
      }
      if ((millis() - lastDebounceTime[i]) > debounceDelay) {
        LEDState = 8; //must set the LEDState to the start of the brightness cases
        loop();
      }
    }
}

void assignTo(int j) {
  int i;
  for (i = 0; i < NUM_LEDS; i++)
  {
    leds[i] = row1[(i + j) % NUM_LEDS];
  }
}

void isr_ModeHandler() {
  uint32_t interrupt_time = millis();
  if (interrupt_time - last_interrupt_time > DEBOUNCE_DELAY) {
    eventModeFlag = 1;
  }
  last_interrupt_time = interrupt_time;
}

void isr_ColorHandler() {
  uint32_t interrupt_time = millis();
  if (interrupt_time - last_interrupt_time > DEBOUNCE_DELAY) {
    eventColorFlag = 1;
  }
  last_interrupt_time = interrupt_time;
}

void isr_BrightnessHandler() {
  uint32_t interrupt_time = millis();
  if (interrupt_time - last_interrupt_time > DEBOUNCE_DELAY) {
    eventBrightFlag = 1;
  }
  last_interrupt_time = interrupt_time;
}

Finally was able to fix the issue. I'm an idiot and forgot that I was setting the LEDState from the spinMode function further down in the code. So once I was getting to LEDState = 1 and calling that function I was setting it back to 0, hence why it wouldn't cycle through the range.

I'd post the full code but it exceeds the max allowed length of 9000 characters. Below is the working function though.

void updateLEDState(int input) {
  // input 0 (Mode) = State 0 to 4
  if (input == 0) { //test for states 0 through 4 to change modes
    if (LEDState >= 4) { //0,1,2,3,4,0,1,2,3,4...
      LEDState = 0;
    } else {
      LEDState++;
    }
    // input 1 (Color) = State 5 to 10
  } else if (input == 1) { //test for states 5 through 10 to change color
    if (LEDState <= 4 || LEDState >= 10) { //5,6,7,8,9,10,5,6,7,8,9,10...
      LEDState = 5;
    } else {
      LEDState++;;
    }
    // input 2 (Brightness) = state 11 to 15
  } else if (input == 2) { //test for states 11 through 15 to change brightness
    if (LEDState <= 10 || LEDState >= 15) { //11,12,13,14,15,11,12,13,14,15...
      LEDState = 11;
    } else {
      LEDState++;
    }
  }
}