Cannot move from one state to another (FastLED + Pixel Matrix)

I am trying to write a test code on Arduino with a LED Matrix I have built. I wrote a couple of functions for animating graphics on the LED Matrix and wanted to change the animation at different integers 'parseInt' through the Serial monitor. However, I am able to get into either one of the 'states,' but once I am in one of the states, I cannot transition to another when I send a different integer that supposedly activates its corresponding state.

Does anyone know what I am doing wrong here? The code is as shown below:


int state = 0;
void loop() {

  while (Serial.available() > 0) {
    int mm = Serial.parseInt();
    if (mm == 1) {
      state = 1;
      while (state == 1) {
        animation1();

      }

    } else if (mm == 2) {
      state = 2;
      while (state == 2) {
        animation2();
        Serial.println(state);
      }} else if (mm == 3) {
        state=3;
        while (state ==3) {
        animation3();
        }
      }
    }
  }

I tried creating boolean variables and tried also printing out the state the pixels is animating. Still, it seems like once it enters one of the states it won't even read the new character sent via the serial monitor.

What do you think is happening here ?


      while (state == 1) {
        animation1();

      }

yea I realized while loop continuously gets stuck in the loop but solely putting the animation function inside if statement didn't work as it only displayed the 1st frame of the animation and not continuously... that's why I used the while loop

Once in the loop, you will never get out.

but how do I write a condition so I can play one animation function to another without facing the issue of only playing 1 frame of the animation??

if I put animation function in if statement it only runs once it seems cus the animation does not continuously progress.

You need to update state inside the while( ) loop.

what do you mean?? can you write a line to show what you mean?

When state is equal to 1, you enter the while( ) loop.

You need to decide how to make state not equal 1, this needs to be done inside the while( ) loop.

so I need to write an if statement *within the while loop to 'breaks;' out of the while loop?

but i also realized once it enters one of the while loop, it wont even collect any data I type in from the serial monitor..

Either use break or make state not equal to 0

I want to break out of the state when a new integer is parsed int through the serial monitor, but if I put the state inside the while loop, it wont gets reading or update the 'mm' value however I type in the serial monitor.. Do you know why the reason for this is?

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

Please post your full code.

because your while loop is blocking all other activity of the controller.

You should change your program logic completely. Play only 1 frame of animation at once, then return to the main program, check the Serial for received characters, do another stuff and after that go the animation again. Play next frame and again return to main program.... and so on

how do i make it so that my animation play continuous framr? currently i wrote a bunch of functions then used 'EVERY_N_MILLISECONDS' from the FastLED function...

Are you rewrote the program? Please show the new code

I just updated my code to include animation codes

Oh... I just realized what the problem is. I don't have to modify my animation code I put double while loop and it works now! Thanks for the guidance!