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.
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
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 See About the Installation & Troubleshooting category.
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...