title: Is it possible to create a loop for some functions but stop them when external information comes in?
body: <p>I am new to this platform and to Arduino. I tried to create a bluetooth controller for RGB Leds using an arduino nano. My code works well but I'd like to make it &quot;perfect&quot; (if you understand what i mean). <br><br>
  So, I designed an Android app on MIT-App-Inventor, and it allows me to click on a button and select an rgb effect that I want (static colors, rainbow effect and so on).<br>
  The problem is that when I have the &quot;<em>looping</em>&quot; effects, rainbow and mixed colors, they stop at one point and the colors freeze on the Led Strip.<br>
  I tried to create another variable that changes according to the type of effect selected, <code>int var = 1;</code> line 48. But by doing that, I cannot escape the loop anymore.<br><br>
  Any Ideas ?<br><br>
  PS : I am using WS2812b 3 meters Led Strip, an Arduino Nano and a HC-05 Bluetooth Module <br><br>
  Here is the entire code : <br></p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;SoftwareSerial.h&gt;
  #include &lt;Adafruit_NeoPixel.h&gt;
  #ifdef __AVR__
    #include &lt;avr/power.h&gt;
  #endif
  
  SoftwareSerial BT(10, 11);  // TX, RX of the Bluetooth Module 
  
  #define PIXEL_PIN      5    // Data Pin of Led strip 
  #define PIXEL_COUNT    160   // Number of LEDs in the strip
  #define BRIGHTNESS   125   // use 96 for medium brightness
  #define SPEED          5   // Speed of each Color Transition (in ms)
  #define RAINBOW_SPEED  27    // Rainbow Transition speed
  
  
  
  Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
  
  bool offOld = LOW;
  bool WhiteOld = LOW;
  bool RedOld = LOW;
  bool GreenOld = LOW;
  bool BlueOld = LOW;
  bool YellowOld = LOW;
  bool OrangeOld = LOW;
  bool pinkOld = LOW;
  bool purpleOld = LOW;
  bool cyanOld = LOW;
  bool RainbowOld = LOW;
  bool rgbOld = LOW;
  bool RandomOld = LOW;
  int  showType = 0;
  
  
  void setup() {
  
     BT.begin(9600);
     
     BT.println(&quot;Connected to Arduino&quot;);
     
  
    strip.setBrightness(BRIGHTNESS);  
    strip.begin();
    strip.show();
  
  }
  
  int var = 1;
  char a;
  
  void loop() {
    bool off = LOW;
    bool White = LOW;
    bool Blue = LOW;
    bool Red = LOW;
    bool Green = LOW;
    bool Yellow = LOW;
    bool Orange = LOW;
    bool pink = LOW;
    bool purple = LOW;
    bool cyan = LOW;
    bool Rainbow = LOW;
    bool rgb = LOW;
    bool Random = LOW;
    bool ende;
    
     if (BT.available())
     {
      a= (char)BT.read();
  
  
  
      if(a=='f')
      {
        off = HIGH;
        var = 1;
        BT.println(&quot;LEDs OFF&quot;);
      }else{
            off = LOW;
      }
      
  // ===========================================================================================
  
      if(a=='w')
      {
        White = HIGH;
        var = 1;
        BT.println(&quot;LEDs turning White&quot;);
      }else{
            White = LOW;
      }
      
  // ===========================================================================================
  
      if(a=='b')
      {
        Blue = HIGH;
        var = 1;
        BT.println(&quot;LEDs turning Blue&quot;);          
      }else{
            Blue = LOW;  
      }
  
  // ===========================================================================================
  
  if(a=='r')
      {
        Red = HIGH;
        var = 1;
        BT.println(&quot;LEDs turning Red&quot;);      
      }else{
            Red = LOW;  
      }
  
  // ===========================================================================================
  
  if(a=='g')
      {
        Green = HIGH;
        var = 1;      
        BT.println(&quot;LEDs turning Green&quot;);    
      }else{
            Green = LOW;
      }
  
  // ===========================================================================================
  
  if(a=='y')
      {
        Yellow = HIGH;
        var = 1;      
        BT.println(&quot;LEDs turning Yellow&quot;);    
      }else{
            Yellow = LOW;
      }
      
  // ===========================================================================================
  
  if(a=='o')
      {
        Orange = HIGH;
        var = 1;      
        BT.println(&quot;LEDs turning Orange&quot;);    
      }else{
            Orange = LOW;
      }
  
  // ===========================================================================================
  
  if(a=='p')
      {
        pink = HIGH;
        var = 1;      
        BT.println(&quot;LEDs turning Pink&quot;);    
      }else{
            pink= LOW;  
      }
  
  
  // ===========================================================================================
  
  if(a=='v')
      {
        purple = HIGH;
        var = 1;      
        BT.println(&quot;LEDs turning Purple&quot;);    
      }else{
            purple = LOW;  
      }
  
  
  // ===========================================================================================
  
  if(a=='c')
      {
        cyan = HIGH;
        var = 1;      
        BT.println(&quot;LEDs turning Cyan&quot;);    
      }else{
            cyan = LOW;  
      }
  
  
  // ===========================================================================================
  
      if(a=='a')
      {
        Rainbow = HIGH;
        var = 0;
        BT.println(&quot;LEDs making Rainbow&quot;);    
      }else{
            Rainbow = LOW;  
      }
      
  // ===========================================================================================
  
       if(a=='m')
      {
        rgb = HIGH;
        var = 0;      
        BT.println(&quot;LEDs making Mixed Colors&quot;);    
      }else{
            rgb = LOW;  
      }
  
  
  // ===========================================================================================
  
       if(a=='e')
      {
        Random = HIGH;
        var = 0;      
        BT.println(&quot;LEDs turning random colors&quot;);    
      }else{
            Random = LOW;  
      }
      
  }
  
  if (off == LOW &amp;&amp; offOld == HIGH) {
      delay(20);
     
      
      if (off == LOW) {
         do {
         showType = 0  ;                            // Off animation Type 0
       
        startShow(showType);
        } while (a=0);
      }
    }
  
  // ===========================================================================================
  
  if (White == LOW &amp;&amp; WhiteOld == HIGH) {
      delay(20);
     
      
      if (White == LOW) {
         do {
         showType = 1  ;                            // White animation Type 1
       
        startShow(showType);
        } while (a=0);
      }
    }
  
    
  // ===========================================================================================  
    
    if (Red == LOW &amp;&amp; RedOld == HIGH) {
      delay(20);
     
      
      if (Red == LOW) {
         do {
         showType = 2  ;                            // Red animation Type 2
       
        startShow(showType);
        } while (a=0);
      }
    }
  
  // ===========================================================================================
  
  if (Green == LOW &amp;&amp; GreenOld == HIGH) {
      delay(20);
     
      
      if (Green == LOW) {
         do {
         showType = 3  ;                            // Green animation Type 3
       
        startShow(showType);
        } while (a=0);
      }
    }
    
  // ===========================================================================================
  
  if (Blue == LOW &amp;&amp; BlueOld == HIGH) {
      delay(20);
     
      
      if (Blue == LOW) {
         do {
       
         showType = 4  ;                            // Blue animation Type 4
       
        startShow(showType);
        } while (a=0);
      }
    }
    
    // ===========================================================================================
  
  if (Yellow == LOW &amp;&amp; YellowOld == HIGH) {
      delay(20);
     
      
      if (Yellow == LOW) {
         do {
         showType = 5  ;                            // Yellow animation Type 5
       
        startShow(showType);
        } while (a=0);
      }
    }
    
  // ===========================================================================================
  
  if (Orange == LOW &amp;&amp; OrangeOld == HIGH) {
      delay(20);
     
      
      if (Orange == LOW) {
         do {
         showType = 8  ;                            // Orange animation Type 8
       
        startShow(showType);
        } while (a=0);
      }
    }
  
  // ===========================================================================================
  
  if (pink == LOW &amp;&amp; pinkOld == HIGH) {
      delay(20);
     
      
      if (pink == LOW) {
         do {
         showType = 9  ;                            // Pink animation Type 9
       
        startShow(showType);
        } while (a=0);
      }
    }
    
  // ===========================================================================================
  
  if (purple == LOW &amp;&amp; purpleOld == HIGH) {
      delay(20);
     
      
      if (purple == LOW) {
         do {
         showType = 10  ;                            // Purple animation Type 10
         startShow(showType);
        } while (a=0);
      }
    }
  
  // ===========================================================================================
  
  if (cyan == LOW &amp;&amp; cyanOld == HIGH) {
      delay(20);
     
      
      if (cyan == LOW) {
       do {
         showType = 11  ;                            // Cyan animation Type 11
         startShow(showType);
       } while (a=0);
      }
    }
    
  // ===========================================================================================
  
        if (rgb == LOW &amp;&amp; rgbOld == HIGH) {
      delay(20);
  
      if (rgb == LOW) {
        
         showType = 6;                            // Mix animation Type 6
         startShow(showType);
        
         
      }
    }
    
  // ===========================================================================================
  
  
      if (Rainbow == LOW &amp;&amp; RainbowOld == HIGH) {
      delay(20);
  
      if (Rainbow == LOW) {
         
         showType = 7;                            // Rainbow animation Type 7
         startShow(showType);
      
        
      }
    }
  
  // ===========================================================================================
  
  
      if (Random == LOW &amp;&amp; RandomOld == HIGH) {
      delay(20);
  
      if (Random == LOW) {
         
         showType = 12;                            // Random colors Type 12
         startShow(showType);
      }
    }
  
    
  WhiteOld = White;
  RedOld = Red;
  BlueOld = Blue;
  GreenOld = Green;
  YellowOld = Yellow;
  OrangeOld = Orange;
  pinkOld = pink;
  purpleOld = purple;
  cyanOld = cyan;
  offOld = off;
  RainbowOld = Rainbow;
  rgbOld = rgb;
  RandomOld = Random;
  }
  
  
  void startShow(int i) {
    switch(i){
  
      case 0: colorWipe(strip.Color(0, 0, 0), SPEED);    // Black/off
              break;
  
      case 1: strip.setBrightness(255);                            // Changes the Brightness to MAX
              colorWipe(strip.Color(255, 255, 255), SPEED);  // White
              strip.setBrightness(BRIGHTNESS);                     // Reset the Brightness to Default value
              break;  
  
      case 2: colorWipe(strip.Color(255, 0, 0), SPEED);  // Red
              break;
  
      case 3: colorWipe(strip.Color(0, 255, 0), SPEED);  // Green
              break;
  
      case 4: colorWipe(strip.Color(0, 0, 255), SPEED);  // Blue
              break;
  
      case 5: colorWipe(strip.Color(255, 220, 0), SPEED);  // Yellow
              break;
      
      case 8: colorWipe(strip.Color(255, 100, 0), SPEED);  // Orange
              break;
  
      case 9: colorWipe(strip.Color(255, 0, 127), SPEED);  // pink
              break;
  
      case 10: colorWipe(strip.Color(127, 0, 255), SPEED);  // purple
               break;
  
      case 11: colorWipe(strip.Color(0, 255, 255), SPEED);  // cyan
               break;
              
      case 6: do {
                colorWipe(strip.Color(255, 220, 0), SPEED);  // Yellow
                colorWipe(strip.Color(255, 100, 0), SPEED);  // Orange
                colorWipe(strip.Color(255, 0, 0), SPEED);  // Red
                colorWipe(strip.Color(255, 0, 127), SPEED);  // pink
                colorWipe(strip.Color(127, 0, 255), SPEED);  // purple
                colorWipe(strip.Color(0, 0, 255), SPEED);  // Blue
                colorWipe(strip.Color(0, 255, 255), SPEED);  // cyan
                colorWipe(strip.Color(0, 255, 0), SPEED);  // Green
                theaterChase(strip.Color(255, 220, 0), SPEED);  // Yellow
                theaterChase(strip.Color(255, 100, 0), SPEED);  // Orange
                theaterChase(strip.Color(255, 0, 0), SPEED);  // Red
                theaterChase(strip.Color(255, 0, 127), SPEED);  // pink
                theaterChase(strip.Color(127, 0, 255), SPEED);  // purple
                theaterChase(strip.Color(0, 0, 255), SPEED);  // Blue
                theaterChase(strip.Color(0, 255, 255), SPEED);  // cyan
                theaterChase(strip.Color(0, 255, 0), SPEED);  // Green
              }while (var == 0);
  
      case 7: do {
                rainbowCycle(25);
              }while (var == 0);
  
      case 12: do {
                colorWipe(strip.Color(random(0, 255),random(0, 255),random(0, 255)), SPEED);
               }while (var == 0);
  }
  }
  
  void colorWipe(uint32_t c, uint8_t wait) {
    for(uint16_t i=0; i&lt;strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
    }
  }
  
  void rainbowCycle(uint8_t wait) {
    uint16_t i, j;
  
    for(j=0; j&lt;256*2; j++) { // 5 cycles of all colors on wheel
      for(i=0; i&lt; strip.numPixels(); i++) {
        strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) &amp; 255));
      }
      strip.show();
      delay(wait);
    }
  }
  
  void theaterChase(uint32_t c, uint8_t wait) {
    for (int j=0; j&lt;10; j++) {  //do 10 cycles of chasing
      for (int q=0; q &lt; 3; q++) {
        for (int i=0; i &lt; strip.numPixels(); i=i+3) {
          strip.setPixelColor(i+q, c);    //turn every third pixel on
        }
        strip.show();
  
        delay(wait);
  
        for (int i=0; i &lt; strip.numPixels(); i=i+3) {
          strip.setPixelColor(i+q, 0);        //turn every third pixel off
        }
      }
    }
  }
  
  uint32_t Wheel(byte WheelPos) {
    WheelPos = 255 - WheelPos;
    if(WheelPos &lt; 85) {
      return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
    }
    if(WheelPos &lt; 170) {
      WheelPos -= 85;
      return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
    }
    WheelPos -= 170;
    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } 
  </code></pre>