Problem with the programm for a "puzzle lamp"

Hi there;-)

I’m new in this forum because i need your help… I want to built a lamp with 4 RGB which can be controlled by an IR-Controller. When I want to change the colors it works but when I push the button and switch to a class with a if- or while-loop then I can’t switch the color…I think I need to leave the loop but I don’t know how…with the command "if(…) break; it doesn’t work.
I hope someone can help me with this problem…Thanks;-)

  #include <IRremote.h>
  
    switch(Taste)
    {
    // Taste 1 Abschalten
    case 1: 
  
    analogWrite(r_led1,OFF);
    analogWrite(g_led1,OFF);
    analogWrite(b_led1,OFF);
    
    analogWrite(r_led2,OFF);
    analogWrite(g_led2,OFF);
    analogWrite(b_led2,OFF);
    
    break;
    
    // Taste 2 Disco langsam
    case 2: 
    
    tas2 = true;
    tas3 = false;
    tas4 = false;
    tas5 = false;
    tas6 = false;
    tas7 = false;
    tas8 = false;
    tas10 = false;
    tas11 = false;
    tas12 = false;
    
    if(Taste == 2)
    {
    
    // Weiss
    analogWrite(r_led1,ON);
    analogWrite(g_led1,ON);
    analogWrite(b_led1,ON);
    
    analogWrite(r_led2,ON);
    analogWrite(g_led2,ON);
    analogWrite(b_led2,ON);
    
    delay(disco_slow);
    
    // Rot
    analogWrite(r_led1,ON);
    analogWrite(g_led1,OFF);
    analogWrite(b_led1,OFF);
    
    analogWrite(r_led2,ON);
    analogWrite(g_led2,OFF);
    analogWrite(b_led2,OFF);
    
    delay(disco_slow);
  
    // Mangenta
    analogWrite(r_led1,OFF);
    analogWrite(g_led1,ON);
    analogWrite(b_led1,ON);
    
    analogWrite(r_led2,OFF);
    analogWrite(g_led2,ON);
    analogWrite(b_led2,ON);
    
    delay(disco_slow);
      
    // Blau
    analogWrite(r_led1,OFF);
    analogWrite(g_led1,OFF);
    analogWrite(b_led1,ON); 
    
    analogWrite(r_led2,OFF);
    analogWrite(g_led2,OFF);
    analogWrite(b_led2,ON);
    
    delay(disco_slow);
    
    // Cyan
    analogWrite(r_led1,ON);
    analogWrite(g_led1,OFF);
    analogWrite(b_led1,ON);
    
    analogWrite(r_led2,ON);
    analogWrite(g_led2,OFF);
    analogWrite(b_led2,ON);
    
    delay(disco_slow);
    
    // Grün
    analogWrite(r_led1,OFF);
    analogWrite(g_led1,ON);
    analogWrite(b_led1,OFF);
    
    analogWrite(r_led2,OFF);
    analogWrite(g_led2,ON);
    analogWrite(b_led2,OFF);
    
    delay(disco_slow);
    
    // Gelb
    analogWrite(r_led1,ON);
    analogWrite(g_led1,ON);
    analogWrite(b_led1,OFF);
    
    analogWrite(r_led2,ON);
    analogWrite(g_led2,ON);
    analogWrite(b_led2,OFF);
    
    delay(disco_slow);
    }
    
    break;
    
  
    // Taste 3 Dicso schnell
    case 3:
    
    tas2 = false;
    tas3 = true;
    tas4 = false;
    tas5 = false;
    tas6 = false;
    tas7 = false;
    tas8 = false;
    tas10 = false;
    tas11 = false;
    tas12 = false;
    
    if(Taste == 3)
    {
  
    // Weiss
    analogWrite(r_led1,ON);
    analogWrite(g_led1,ON);
    analogWrite(b_led1,ON);
    
    analogWrite(r_led2,ON);
    analogWrite(g_led2,ON);
    analogWrite(b_led2,ON);
    
    delay(disco_fast);
    
    // Rot
    analogWrite(r_led1,ON);
    analogWrite(g_led1,OFF);
    analogWrite(b_led1,OFF);
    
    analogWrite(r_led2,ON);
    analogWrite(g_led2,OFF);
    analogWrite(b_led2,OFF);
    
    delay(disco_fast);
  
    // Mangenta
    analogWrite(r_led1,OFF);
    analogWrite(g_led1,ON);
    analogWrite(b_led1,ON);
    
    analogWrite(r_led2,OFF);
    analogWrite(g_led2,ON);
    analogWrite(b_led2,ON);
    
    delay(disco_fast);
      
    // Blau
    analogWrite(r_led1,OFF);
    analogWrite(g_led1,OFF);
    analogWrite(b_led1,ON); 
    
    analogWrite(r_led2,OFF);
    analogWrite(g_led2,OFF);
    analogWrite(b_led2,ON);
    
    delay(disco_fast);
    
    // Cyan
    analogWrite(r_led1,ON);
    analogWrite(g_led1,OFF);
    analogWrite(b_led1,ON);
    
    analogWrite(r_led2,ON);
    analogWrite(g_led2,OFF);
    analogWrite(b_led2,ON);
    
    delay(disco_fast);
    
    // Grün
    analogWrite(r_led1,OFF);
    analogWrite(g_led1,ON);
    analogWrite(b_led1,OFF);
    
    analogWrite(r_led2,OFF);
    analogWrite(g_led2,ON);
    analogWrite(b_led2,OFF);
    
    delay(disco_fast);
    
    // Gelb
    analogWrite(r_led1,ON);
    analogWrite(g_led1,ON);
    analogWrite(b_led1,OFF);
    
    analogWrite(r_led2,ON);
    analogWrite(g_led2,ON);
    analogWrite(b_led2,OFF);
    
    delay(disco_fast);
    }
    
    break;
  
    // Taste 4 Fade langsame Wechselgeschwindikeit
    case 4:
  
  // while(r_val != 255)
  //{
  //  analogWrite(r_led1,r_val);
  //  analogWrite(g_led1,g_val);
  //  
  //  analogWrite(r_led2,r_val);
  //  analogWrite(g_led2,g_val);
  //  
  //  r_val++;
  //  g_val--;
  //  
  //  delay(20);
  //}
  //
  //while(g_val != 255)
  //{
  //  analogWrite(g_led1,g_val);
  //  analogWrite(b_led1,b_val);
  //  
  //  analogWrite(g_led2,g_val);
  //  analogWrite(b_led2,b_val);
  //  
  //  g_val++;
  //  b_val--;
  //  
  //  delay(20);
  //}
  //
  //while(b_val != 255)
  //{
  //  analogWrite(b_led1,b_val);
  //  analogWrite(r_led1,r_val);
  //  
  //  analogWrite(b_led2,b_val);
  //  analogWrite(r_led2,r_val);
  //  
  //  b_val++;
  //  r_val--;
  //  
  //  delay(20);
  //}
  
    // Taste 5 Fade mittlere Wechselgeschwindigkeit
    case 5:
    
    break;
    
    // Taste 6 Fade schnelle Wechselgeschwindigkeit
    case 6:
    
    break;
  
  
    // Taste 7 Helligkeit verringern
    case 7:
    
    break;
  
    // Taste 8 Helligkeit vergrössern
    case 8:
    
    break;
  
    // Taste 9 Weiss
    case 9:
    
    analogWrite(r_led1,ON);
    analogWrite(g_led1,ON);
    analogWrite(b_led1,ON);
    
    analogWrite(r_led2,ON);
    analogWrite(g_led2,ON);
    analogWrite(b_led2,ON);
    
    break;
  
    // Taste 10 Weiss 2h
    case 10:
    
    if(hell_sen < 500 )
    {
    analogWrite(r_led1,OFF);
    analogWrite(g_led1,OFF);
    analogWrite(b_led1,OFF);
    
    analogWrite(r_led2,OFF);
    analogWrite(g_led2,OFF);
    analogWrite(b_led2,OFF);
    }
    
    if(hell_sen > 500)
    {
    analogWrite(r_led1,ON);
    analogWrite(g_led1,OFF);
    analogWrite(b_led1,ON);
    
    analogWrite(r_led2,ON);
    analogWrite(g_led2,OFF);
    analogWrite(b_led2,ON);
    
    delay(2000);
    }
    
    break;
  
  
    // Taste 11 Weiss 4h
    case 11:
  
    if(hell_sen < 500 )
    {
    analogWrite(r_led1,OFF);
    analogWrite(g_led1,OFF);
    analogWrite(b_led1,OFF);
    
    analogWrite(r_led2,OFF);
    analogWrite(g_led2,OFF);
    analogWrite(b_led2,OFF);
    }
    
    if(hell_sen > 500)
    {
    analogWrite(r_led1,ON);
    analogWrite(g_led1,OFF);
    analogWrite(b_led1,ON);
    
    analogWrite(r_led2,ON);
    analogWrite(g_led2,OFF);
    analogWrite(b_led2,ON);
    
    delay(4000);
    }
    
    break;
    
    // Taste 12 Weiss 6h
    case 12:
    
    if(hell_sen < 500 )
    {
    analogWrite(r_led1,OFF);
    analogWrite(g_led1,OFF);
    analogWrite(b_led1,OFF);
    
    analogWrite(r_led2,OFF);
    analogWrite(g_led2,OFF);
    analogWrite(b_led2,OFF);
    }
    
    if(hell_sen > 500)
    {
    analogWrite(r_led1,ON);
    analogWrite(g_led1,OFF);
    analogWrite(b_led1,ON);
    
    analogWrite(r_led2,ON);
    analogWrite(g_led2,OFF);
    analogWrite(b_led2,ON);
    
    delay(6000);
    }
    
    break;
  
    // Taste 13 Rot
    case 13:
    
    analogWrite(r_led1,ON);
    analogWrite(g_led1,OFF);
    analogWrite(b_led1,OFF);
    
    analogWrite(r_led2,ON);
    analogWrite(g_led2,OFF);
    analogWrite(b_led2,OFF);
    
    break;
  
    // Taste 14 Grün
    case 14:
    
    analogWrite(r_led1,OFF);
    analogWrite(g_led1,ON);
    analogWrite(b_led1,OFF);
    
    analogWrite(r_led2,OFF);
    analogWrite(g_led2,ON);
    analogWrite(b_led2,OFF);
    
    break;
  
    // Taste 15 Blau
    case 15:
    
    analogWrite(r_led1,OFF);
    analogWrite(g_led1,OFF);
    analogWrite(b_led1,ON);
    
    analogWrite(r_led2,OFF);
    analogWrite(g_led2,OFF);
    analogWrite(b_led2,ON);
    
    break;
  
    }
  }

Best regards Groley10

Do you want to run a sequence for the colors continuesly after pressing a button ? I don't understand that. I think the 'disco' should run forever until you press a new button.

You have to split the functionality. One section is reading the remote buttons. This should run perhaps 10 times a second or more. Another section is the disco sequence.

To run both independent of each other, you can no longer use delays for the disco sequence. You have to make timing based. The 'blink without delay' is the example for that. http://arduino.cc/en/Tutorial/BlinkWithoutDelay Do you also want to fade colors, or only flashing them on and off ?

Thank you for the very fast reply;-)

Yes I want to run a sequence after pressing a button. Exactly the disco-mode should run forever until I press a new button. Thanks for the tip for splitting the functionalitiy. I totally forgott about the function delay.....of course I need to use the function "millis()"!;-)

So now i try to use your improvments for programming the new code.

Best regards

Groley10

I can think of three ways to do this.

1 ) Use a function with a certain sequence. The sequence is in code. But instead of a normal delay, use a delay that checks a button. If the button is pressed, return from the function. Checking the button can be with polling but the button can also generate an interrupt.

2 ) Use a sequence in data. Like in some kind of string. You can use timing to 'walk' through the string, and do something new if the next time event occurs. I would prefer this, since you have multiple sequences running, and adding fading colors is possible.

3 ) Use a library. Someone must have make a library for this. But those libraries are more ment to control relays at a certain time. Since this is done before, there must be a library somewhere. I searched the adafruit site, but did not find it. This looks awesome, but it uses the neopixel library. I don't know if you can use that. http://learn.adafruit.com/neopixel-cyber-falls