Go Down

Topic: exiting one loop and starting another loop with pressing a button  (Read 57 times) previous topic - next topic

TB04

Hello all,

I am very new to arduino and coding in general. I am attempting to switch between animations on my neopixel strip when pressing different buttons in a bluetooth app. For example, when button 'a' is pressed I would like it to run a certain animation. Then when button 'b' is pressed I would like it to end the previous animation from button 'a' and begin a new animation when button 'b' is pressed. How can I go about accomplishing this task?

As of now I can get the first animation to run by pressing button 'a' but it does not break the loop and start a new loop when I press button 'b'. Therefore, it just continuously runs the animation from button 'a'.


Here is what I have for the code.

void setup(){
  strip.begin();
  strip.show();
  Serial.begin(9600);
 
}

void loop(){
  if(Serial.available()>0)
  {
    int data= Serial.read();
    switch(data)
    {
      case'a':STRIPON();break;
      case'b':STRIPOFF();break;
      defult: break;
    }
  }
  delay(50);
}



void STRIPON(){
 
  int x=0;
  while(x=0){
    for (int i=0; i<=NUM_LEDS; i=i+2){
      strip.setPixelColor(i, 25, 25, 25);  // even pixels
      strip.setPixelColor(i+1 , 0, 0, 25); // odd pixels
      strip.show();
  }
    for (int i=0; i<= NUM_LEDS; i=i+2){
      strip.setPixelColor(i, 0, 0, 0);  // even pixels
      strip.setPixelColor(i+1 , 0, 0, 0); // odd pixels
      strip.show();
    }
  } 
}


void STRIPOFF(){
 
  for (int i=0; i<NUM_LEDS; i++){
   strip.setPixelColor(i, 0, 0, 0);
   strip.show();
  }
}

groundFungus

We can't help you with your code unless we can see it.  Please read the "How to use the forum" stickies to see how to format and post code.

Delta_G

Look at the beginning of STRIPON function.  You set x to zero and have a while x==0.  Since nothing in that loop changes the value of x, that is an infinite loop and the code is stuck there forever. 

What's worse is that instead of using the comparison operator == you used assignment = in the condition for the while.
If at first you don't succeed, up - home - sudo - enter.

Go Up