Break "While" Loop using Digital Input toggle

I have been going through an Arduino starter kit tutorial and am trying to piece different bits of knowledge together as I go.

For fun, I wanted to control a single RGB LED using 5 digital input buttons.

Button 1: Lights only the RED light
Button 2: Lights only the GREEN light
Button 3: Lights only the BLUE light
Button 4: Cycles through a gradient of possible light combinations
Button 5: Resets (All lights off)

My problem is in the 4th part of the program, Cycling through a gradient. In order to make it continue cycling, I added a “While” statement, such that while y =1, the program would continue, and if the Reset button (Button 5) is pushed, it would break the loop and all lights would go out.

This is not happening. The “While” loop just continues cycling no matter what I do. I tried Googling what to do but none of the solutions seemed to apply in this situation.

Here is my code: (the While statement begins towards the botton) Any help would be greatly appreciated!

#define ledRedPin 6
#define ledGreenPin 5
#define ledBluePin 3

int buttonRpin = 8;
int buttonGpin = 9;
int buttonBpin = 10;
int buttonXpin = 11;
int buttonNpin = 12;

int redValue;
int greenValue;
int blueValue;

byte leds = 0;

void setup() 
    {
      pinMode(ledRedPin, OUTPUT);
      pinMode(ledGreenPin, OUTPUT);
      pinMode(ledBluePin, OUTPUT);
      pinMode(buttonRpin, INPUT_PULLUP);  
      pinMode(buttonGpin, INPUT_PULLUP);
      pinMode(buttonBpin, INPUT_PULLUP); 
      pinMode(buttonXpin, INPUT_PULLUP);
      pinMode(buttonNpin, INPUT_PULLUP); 
    }

void loop() 
    {
      if (digitalRead(buttonRpin) == LOW)
            {
              digitalWrite(ledRedPin, HIGH);
              digitalWrite(ledGreenPin, LOW);
              digitalWrite(ledBluePin, LOW);
            }
      if (digitalRead(buttonGpin) == LOW)
            {
              digitalWrite(ledRedPin, LOW);
              digitalWrite(ledGreenPin, HIGH);
              digitalWrite(ledBluePin, LOW);
            }
      if (digitalRead(buttonBpin) == LOW)
            {
              digitalWrite(ledRedPin, LOW);
              digitalWrite(ledGreenPin, LOW);
              digitalWrite(ledBluePin, HIGH);
            }
      
      if (digitalRead(buttonXpin) == LOW)
            {
              int y = 1;
              if(digitalRead(buttonNpin) == LOW) 
              {
                y - 1;
              }
                while(y = 1) 
              {   
                #define delayTime 10 // fading time between colors
                
                redValue = 255; // choose a value between 1 and 255 to change the color.
                greenValue = 0;
                blueValue = 0;
                
                for(int i = 0; i < 255; i += 1) // fades out red bring green full when i=255
                    {
                      redValue -= 1;
                      greenValue += 1;
                      
                      analogWrite(ledRedPin, redValue);
                      analogWrite(ledGreenPin, greenValue);
                      delay(delayTime);
                    }
              
                redValue = 0;
                greenValue = 255;
                blueValue = 0;
                
                for(int i = 0; i < 255; i += 1) // fades out green bring blue full when i=255
                    {
                    greenValue -= 1;
                    blueValue += 1;
                    
                    analogWrite(ledGreenPin, greenValue);
                    analogWrite(ledBluePin, blueValue);
                    delay(delayTime);
                    }
                
                redValue = 0;
                greenValue = 0;
                blueValue = 255;
                
                for(int i = 0; i < 255; i += 1) // fades out blue bring red full when i=255
                    {
                    blueValue -= 1;
                    redValue += 1;
                    
                    analogWrite(ledBluePin, blueValue);
                    analogWrite(ledRedPin, redValue);
                    delay(delayTime);
                    }
                if(digitalRead(buttonNpin) == LOW) 
                    {
                     break;
                    }
                 }
              }
      if (digitalRead(buttonNpin) == LOW)
          {
            digitalWrite(ledRedPin, LOW);
            digitalWrite(ledGreenPin, LOW);
            digitalWrite(ledBluePin, LOW);
          }
    }

Please indent your code properly and do not have more than 1 curly brace per line, you will probably be able to figure out what is wrong.

Deva_Rishi: Please indent your code properly and do not have more than 1 curly brace per line, you will probably be able to figure out what is wrong.

Thanks, I've indented my code and properly distributed the curly brackets (I think).

if(digitalRead(buttonNpin) == LOW) 
              {
                y - 1;
              }

What does that achieve?

It's sort of a redundant way to break the code. The while statement runs as long as y =1. So if by pushing the button I can subtract 1 from the y variable, which was previously set to 1, then I could break the while condition.

At the same time, there is another statement such that if I press that same button it applies the "break" action. Hence the redundancy, but neither one seemed to work on it's own either.

I should note that this is the part that is not working - so clearly my logic is broken... lol

It's sort of a redundant way to break the code. The while statement runs as long as y =1

Hard to see that. Please explain

well it does substract 1 from y, but it doesn't assign that to y thereafter.

y--;
y=y-1;
y-=1;

do all work.

Deva_Rishi: well it does substract 1 from y, but it doesn't assign that to y thereafter.

y--;
y=y-1;
y-=1;

do all work.

So, nothing to do with braces?

AWOL: So, nothing to do with braces?

No but it was not readable enough for me.. had to check the truth table to make sure. Was it readable enough for you ?

                y - 1;
              }
                while(y = 1)

The obvious bugs stick out like sore . . . semantic errors.

AWOL:

                y - 1;

}
                while(y = 1)



The obvious bugs stick out like sore . . . semantic errors.

I only tried that after the Break code I tried failed. Admittedly I am brand new to C and sort of fumbling through it using examples. I figured it was a logical enough statement and figured it followed syntax and could work. The break statement I used below was something I found in the forums to break out of a while loop, but it didn’t work either. I’m not sure if its the code that’s no good or if my button pushes aren’t being recognized once the loop is initiated. Because even pushing the Red button again wont bring back Red, it just keep looping through colors.

                if(digitalRead(buttonNpin) == LOW) 
                    {
                     break;
                    }

while (y = 1) will always loop, because "y = 1" is always true.

The construct is not syntactically incorrect (if it had been, the compiler would have barfed), but is semantically flawed. Probably.