Help with small Neopixel project

Hello! I apologize for being the umpteenth new person to post something like this but I would greatly appreciate some help. I have a Neopixel project for my fishing kayak. I have part of the code working great but getting into some deeper functionality while learning this has me completely stumped.

I am using an arduino Uno to drive one (12 LED) neopixel ring. I am using a “Fade In - Fade to black” style animation to use as a night fishing beacon for my kayak. I have one iteration of the code working but I want to add a function where I have two buttons to press that will basically choose what animation style i want. I want a normal, white color that fades to black effect for just normal fishing and a Red, slightly faster transitioning, Fade in - fade to black effect for if I had an emergency situation. The problem I am having is trying to think of a way to implement the second half into this project where I can choose between modes.

Does anyone have some suggestions? I am horrible at coding, just a basic all around DIY, person so please be patient if I don’t understand something. If someone happens to easily plot a code I can usually understand things much easier than trying to wrap my head around coding logic from the very beginning (my ADHD is horrible for this reason). I am not trying to be a burden on anyone, just trying to understand, much appreciated!

#include <Adafruit_NeoPixel.h>

#define PIN 3


Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, PIN, NEO_GRB + NEO_KHZ800);



void setup() {
  strip.begin();
  strip.show();
  
strip.setBrightness(100); //Just for testing purposes so I am not blind
}

void loop() {
uint16_t i, j;

  for (j = 0; j < 150; j++) {
    for (i = 0; i < strip.numPixels(); i++) { 
      strip.setPixelColor(i, j, j, j);
    }
    strip.show();
    delay(25);
  }
  

  for (j = 150; j > 0; j--) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, j, j, j);
    }
    strip.show();
    delay(10);
  }
  
}

What you do is divide the program into two parts, button reading, and LED display (including mode selection). To support multiple modes, you use a variable to set and store the mode with button switches, and read the mode in ‘if’ statements to choose the blocks of code that are associated with a particular mode.

This project is do-able, but it’s painful unless you break it down into the above logical steps. Also, this method is really the only hope for easily running both button code and LED code at the same time. If you don’t, the buttons won’t respond during LED sequencing.

For now, I suggest you write and debug a button switch sketch that works. Do not use any blocking code (code that stops and waits for some event). That will sink you when you integrate it with the LED code.

I hate to break this to you, but you will have to unravel the for loops in order to make them non-blocking (currently there is no way to interrupt them to check buttons). There are instructions in parts of the sticky threads at the top of the forum for writing, “doing two things at the same time” code. To make this work, you will have to master that.

So something you have like:

  for (j = 0; j < 150; j++) {

Has to be implemented as a state machine. Here is how you implement a for loop using a state machine:

for(i=3; i<6; i++) {<somecode>}

decomposes as the sequence

i=3;
while (i<6) {
<somecode>
i++;
}

For the state machine you would do something like

if (state == INITIALIZE) {
 i=3;
 state = RUN;
}
else if (state == RUN and i<6) {
 <somecode>
 i++;
}
else {
 state = IDLE;
}

To activate it, you just assert:

state = INITIALIZE;

Or, you can use a switch case statement:

switch (state) {

case:INITIALIZE
 i=3;
 state = RUN;
 break;

case:RUN
 if (i<6) {
 <somecode>
 i++;
 break;

 state = IDLE;
}