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;
}