program gets lost after end of switch case.

I’m using a 8Mhz Adafruit Trinket with NeoPixel Rings. I’m setting up an interrupt with two buttons to control the current demo the rings display and another to change the color and a switch to change brightness. I setup up a switch case that changes the modes the rings are in. After i get this switch case working I’m going to make another one for ring color and brightness.

The problem is that when I try to use STROBE() demo or when the switch case hits the ends the program crashes and the interrupt no longer responds (LED3) . I don’t think I’m running out of memory, the sketch compiles to 4008 bytes of 5310 bytes max I cant run serial memory since the trinket doesn’t have serial communication. Any help would appreciated and Thank you in advance.

PS I know there is only one interrupt pin. I have a setup that uses one interrupt pin and multiple digitalRead buttons that activate the interrupt pin and separate digitalRead pins

Here is the code.

//taken and modified from adafruit neopixel demo program 
#include <Adafruit_NeoPixel.h>
#define PIN 0
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(32, PIN);
uint8_t  mode   = 0, // Current animation effect
         offset = 0; // Position of spinny eyes
uint32_t color  = 0x0000FF; // Start red
uint32_t prevTime;
 // blue 0x0000FF
 // red  0xff0000
 // GREEN 0x00FF00 

//000000000000 debounce 0000000000000000000000000
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 500;    // the debounce time; increase if the output flickers
long last_micros;

//0000000000000000000000000000000000000000000000 
int MyMode=0;
int LED1=1;
int LED2=2;
int LED3=3;
int LED4=4;

void setup() 
{
  pinMode(LED1, OUTPUT);//   
  pinMode(LED3, OUTPUT);// PIN 3 led 
  pinMode(LED4, INPUT);// PIN 4 2nd BUTTON
  attachInterrupt(0, debounce, RISING);
  pixels.begin();
  pixels.setBrightness(70); //70 1/3 brightness 200 MAX 
  prevTime = millis();
}
 
void loop() 
{
  uint8_t  i;
  uint32_t t;
  modeselect();
}

void buttonread()
{
if(digitalRead(2)==true)
  {
  digitalWrite(3, HIGH);
  ++MyMode;
  if(MyMode>5){MyMode==1;}
  }
else if(digitalRead(2)==false)
  {  
  digitalWrite(3, LOW);
  }
}    
 
 void modeselect()//chooses which demo to run 
 {
   switch(MyMode)
   {
   case 1: sparks();
   break;
   case 2: SPINWHEEL();
   break;
   case 3: //STROBE();
   break;
   case 4: //FLASH();
   break;
   case 5: ALLON();
   break;
   default: MyMode==1;
   }
 }

void debounce()
{
 if(digitalRead(2)==true){
  if(((long)(micros() - last_micros) >= debounceDelay * 2000) || last_micros==0)
    {
      buttonState = !buttonState;
      digitalWrite(1, buttonState);
      //digitalWrite(3, true);
      last_micros = micros(); 
    }
 }
 buttonread();
}
 
void sparks() // Random sparks - just one LED on at a time!
{
    int i = random(32);
    pixels.setPixelColor(i, color);
    pixels.show();
    delay(10);
    pixels.setPixelColor(i, 0);
}
 
void SPINWHEEL()// Spinny wheels (8 LEDs on at a time)
 {
    for(int i=0; i<16; i++) 
    {
      uint32_t c = 0;
      if(((offset + i) & 7) < 2) c = color; // 4 pixels on...
      pixels.setPixelColor(   i, c); // First eye
      pixels.setPixelColor(31-i, c); // Second eye (flipped)
    }
    pixels.show();
    offset++;
    delay(50);
  }
  
void STROBE() 
{
   for(int a=0; a<255; a=a+25) //cycles all colors low range
   {
     for(int b=0; b<255; b=b+25) //cycles all colors mid range
     {
       for(int c=0; c<255; c=c+25) //cycles all colors end range
       {
          for(int i=0; i<32; i++) //cycles all the pixel to light whole ring
          {
            pixels.setPixelColor(i, pixels.Color(a,b,c));
            pixels.show();
          }
       }
     }
   }  
}
  
void FLASH()
{
   for(int b=0; b<255; b=b+25) //cycles all colors low range
   {
          for(int i=0; i<32; i++) //cycles all the pixel to light whole ring
          {
            pixels.setPixelColor(i, pixels.Color(0,b,0));
            pixels.show();
          }
   }
}
 
void ALLON()
{
  for(int i=0; i<32; i++) //cycles all the pixel to light whole ring
      {
        pixels.setPixelColor(i, pixels.Color(0,255,0));
        pixels.show();
      }      
      // 255,255,255 white ish. 0,255,0 green. 255,0,0 red. 0,0,255 blue
}
   default: MyMode==1;

Ditto for here:

   if(MyMode>5){
      MyMode==1;

Thank you. That fixed it from getting lost. That was a newbie mistake for me. I guess i got rusty after not coding for a while.

Another problem though. When I uncommented the subroutine STROBE() the switch case gets stuck. i believe the sketch is still running but skips over buttonread() since the LED1 on debounce still lights but LED3 within buttonread doesn't respond. Does it have to due to the fact that STROBE() has multiple nested for loops? I thought the interrupt is able to escape from that. Thanks in advance.

Interrupts don’t “escape” you from anything. If you want to do something different you have to choose to. For example:

void STROBE() 
{
  for(int a=0; a<255; a=a+25) //cycles all colors low range
  {
    for(int b=0; b<255; b=b+25) //cycles all colors mid range
    {
      for(int c=0; c<255; c=c+25) //cycles all colors end range
      {
        for(int i=0; i<32; i++) //cycles all the pixel to light whole ring
        {
          pixels.setPixelColor(i, pixels.Color(a,b,c));
          pixels.show();
          if (MyMode != 4)  // Switch has been pressed
             return;   // so leave this function
        }
      }
    }
  }  
}

I added those lines to my sketch and it negates the subroutine and does nothing. The sketch still works as a whole. It still exits the subroutine like you said it would, but while in there it doesn't do anything.

I meant:

          if (MyMode != 3)  // Switch has been pressed

After all, that is the mode that enters STROBE, right?

It would have been easier if you used constants, instead of magic numbers.

eg.

          if (MyMode != STROBE_MODE)  // Switch has been pressed

That fixed it up. My code is good to go. Now looking at it was simple mistakes that were easy to miss. Thank you so much.