Button to switch stops reading (loop inside of case)

Ok, basically im trying to make a simple led that cicles between modes and all static colors work but I haven't been able to make a color cicle that doesn't get stuck. Basically I can switch between every case but once I try to do a cicle it stops reading the input and I have to reset it to go back to the other colors, but like this I also cant add any other cicles.

The code is the following, it's not the most optimal but everything apart from this problem is working :

void setup() {

 #define button 3


 //pushButton
 pinMode(button, INPUT);

 //leds
 pinMode(10, OUTPUT);
 pinMode(11, OUTPUT);
 pinMode(12, OUTPUT);

}

int buttonPoll = 0;
int state = 0;  
int old = 0;

void loop() {


   buttonPoll = digitalRead(button);
   if (buttonPoll == 0){
       delay(50);
       buttonPoll = digitalRead(button);
         if(buttonPoll == 1){ state = old+1; }}
         
    else{delay(100);}



    switch (state){
         
     case 1 : //green
     
             digitalWrite(10, LOW);
             digitalWrite(11, HIGH);
             digitalWrite(12, HIGH);
             old = state;
             break;
                  
     case 2 : //blue
          
             digitalWrite(10, HIGH);
             digitalWrite(11, LOW);
             digitalWrite(12, HIGH);
             old = state;
             break;
                         
     case 3 : //red
      
             digitalWrite(10, HIGH);
             digitalWrite(11, HIGH);
             digitalWrite(12, LOW);
             old = state;
             break;
             
      case 4 : //light_blue
           
             digitalWrite(10, LOW);
             digitalWrite(11, LOW);
             digitalWrite(12, HIGH);
             old = state;
             break;

      case 5 : //yellow
           
             digitalWrite(10, LOW);
             digitalWrite(11, HIGH);
             digitalWrite(12, LOW);
             old = state;
             break;
             
      case 6 : //purp
           
             digitalWrite(10, HIGH);
             digitalWrite(11, LOW);
             digitalWrite(12, LOW);
             old = state;
             break;    

       case 7 : //white
           
             digitalWrite(10, LOW);
             digitalWrite(11, LOW);
             digitalWrite(12, LOW);
             old = state;
             break;

        case 8 : //cicle_all

             do{digitalWrite(10, LOW); //Verde On
               delay(1000);    
           
             digitalWrite(12, HIGH); //Vermelho Off
               delay(1000);   
           
             digitalWrite(11, LOW); //Azul On
               delay(1000);      
               
             digitalWrite(10, HIGH); //Verde Off
               delay(1000);  
           
             digitalWrite(12, LOW); //Vermelho On
               delay(1000);
           
             digitalWrite(11, HIGH); //Azul Off
               delay(1000);
               buttonPoll = digitalRead(button);
               }while(buttonPoll =! 1);
               
             old = state;
             
             break;
             
         default:

             digitalWrite(10, HIGH);
             digitalWrite(11, HIGH);
             digitalWrite(12, HIGH);
             old = 0;
             
             break;           
 
       
     }
 }[code]

You need to convert your sketch to a 'State Machine' and get rid of all those delay()s; Google 'Blink Without Delay' (BWD) (or check out the 100s of examples on this web site).


You have to be lucky enough to catch the user's action. :wink:
buttonPoll = digitalRead(button);
}while(buttonPoll =! 1);


Use CTRL T or CMD T to format your sketch, use code tags to display your code on this web site.

What Larry said.

You will find these useful:
Using millis for timing
Demonstration for several things at the same time

Ok, i've tried doing that and actually got a working loop but now instead of not changing, it just freezes in the color that is cicling through.

  unsigned long previousMillis = 0;        
  
  const long interval = 1000;    

  int greenState = HIGH;
  int redState = HIGH;             
  int blueState = HIGH;
  
void setup() {

  #define button 3
  #define red 12
  #define blue 11
  #define green 10 
         


  //pushButton
  pinMode(button, INPUT);

  //leds
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);
  pinMode(blue, OUTPUT);

}

int buttonPoll = 0;
int state = 0; 
int old = 0;

void loop() {
  
    Serial.begin(9600);
    Serial.println(buttonPoll);
    
    buttonPoll = digitalRead(button);
    if (buttonPoll == 0){
        delay(50);
        buttonPoll = digitalRead(button);
          if(buttonPoll == 1){ state = old+1; }}
         
     else{delay(100);}



     switch (state){
         
      case 1 : //green
     
              digitalWrite(green, LOW);
              digitalWrite(blue, HIGH);
              digitalWrite(red, HIGH);
              old = state;
              break;
                   
      case 2 : //blue
           
              digitalWrite(green, HIGH);
              digitalWrite(blue, LOW);
              digitalWrite(red, HIGH);
              old = state;
              break;
                         
      case 3 : //red
       
              digitalWrite(green, HIGH);
              digitalWrite(blue, HIGH);
              digitalWrite(red, LOW);
              old = state;
              break;
             
       case 4 : //light_blue
           
              digitalWrite(green, LOW);
              digitalWrite(blue, LOW);
              digitalWrite(red, HIGH);
              old = state;
              break;

       case 5 : //yellow
           
              digitalWrite(green, LOW);
              digitalWrite(blue, HIGH);
              digitalWrite(red, LOW);
              old = state;
              break;
             
       case 6 : //purp
           
              digitalWrite(green, HIGH);
              digitalWrite(blue, LOW);
              digitalWrite(red, LOW);
              old = state;
              break;   

        case 7 : //white
           
              digitalWrite(green, LOW);
              digitalWrite(blue, LOW);
              digitalWrite(red, LOW);
              old = state;
              break;

         case 8 : //cicle_all

               unsigned long currentMillis = millis();

               if (currentMillis - previousMillis >= interval) {
  
                previousMillis = currentMillis;
            
                
                if (greenState == LOW) {
                  greenState = HIGH;
                  redState = LOW;
                  blueState = HIGH;}
               else {
                  if(blueState == LOW){
                     greenState = LOW;
                     redState = HIGH;
                     blueState = HIGH;}
                  else{
                   greenState = HIGH;
                   redState = HIGH;
                   blueState = LOW;}}
            
                digitalWrite(green, greenState);
                digitalWrite(red, redState);
                digitalWrite(blue, blueState);
              }
               
              old = state;
             
              break;
             
          default:

              digitalWrite(10, HIGH);
              digitalWrite(11, HIGH);
              digitalWrite(12, HIGH);
              old = 0;
             
              break;           
 
       
      }
  }

OK, let's see your improved code then. :grinning:

But - first things first.

You need to go and read the forum instructions so that you can go back and modify your original post (not re-post it) - using the "More -> Modify" option below the right hand corner of your post - to mark up your code as such using the "</>" icon in the posting window. Just highlight each section of code (or output if you need to post that) from the IDE and click the icon.

In fact, the IDE itself has a "copy for forum" link to put these markings on a highlighted block for you so you then just paste it here in a posting window. But even before doing that, don't forget to use the "Auto-Format" (Ctrl-T) option first to make it easy to read. If you do not post it as "code" it can as easily be quite garbled and is always more difficult to read due to the font.

It is inappropriate to attach it as a ".ino" file unless it is clearly too long to include in the post proper. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And even that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space. Do use blank lines, but only single blanks between complete functional blocks.

Paul__B:
OK, let's see your improved code then. :grinning:

But - first things first.

You need to go and read the forum instructions so that you can go back and modify your original post (not re-post it) - using the "More -> Modify" option below the right hand corner of your post - to mark up your code as such using the "</>" icon in the posting window.

Done, thx for the heads up.