Repeating An (if) Statement Until It Becomes False

Hello, I am new to Arduino programming and was wondering if it's possible(more likely how) to repeat a (if) statement until it becomes false. I am attaching the code.

However this is the block that I need help with. But should you want to see the full, please reference the attached.

if (buttonState == 1) {
    for (int brightness = 0; brightness <= 255; brightness++) { 
    for (int rsBright = 3; rsBright <= 11; rsBright++) {
      analogWrite(rsBright, brightness);
    }
  }
    }

On a second though please do look at the full code. But I know the rest works.

Edit
So this is now solved but a new problem has popped up. Once I let go of the button, the loop doesn't stop. What can I do so that it stops doing the (while) loop once the button is let go of?
New code:

while (buttonValue == 1) {
    for (int brightness = 0; brightness <= 255; brightness++) {
      for (int runSet1 = 3; runSet1 <= 11; runSet1++) {
        analogWrite(runSet1, brightness);
        delay(1);
      }
    }
  }

Edit
So I got the time to edit the code and everything works perfectly. As well as learning many new things, I got to understand Arduino coding better.
Thank you econjack for identifying flaws and gaps in my code.
And pert, thanks for showing me the proper format for asking questions.

Here is the new code:
If anything can be approved please don't hesitate to identify it.

const int potInPut = A0; //which set to run
const int pot2InPut = A1; //brightness
static const int analogPins[] = {3, 5, 6, 9, 10, 11};
const int buttonInPut = 2;

void setup() {

  Serial.begin(9600);
  for (int runSet1 = 3; runSet1 <= 11; runSet1++) {
    pinMode(runSet1, OUTPUT);
  }
}

void loop() {
  int pot1Value = analogRead(potInPut);
  int pot2Value = analogRead(pot2InPut);
  int outputValue = map(pot2Value, 0, 1023, 0, 255);
  int buttonValue = digitalRead(buttonInPut);

  if (buttonValue == 1) {       //Ok, the button is pressed
    while (digitalRead(buttonValue) != 1) {        //While its pressed do the following
      for (int brightness = 0; brightness <= 255; brightness++) {
        for (int runSet1 = 3; runSet1 <= 11; runSet1++) {
          analogWrite(runSet1, brightness);
          delay(1);
        }
      }
    }
  }



  if (pot1Value < 400) {
    for (int runSet1 = 3; runSet1 <= 11; runSet1++) {
      analogWrite(runSet1, outputValue);
      delay(100);
      digitalWrite(runSet1, LOW);
      delay(30);
    }
  } else if (pot1Value > 500) {
    for (int runSet1 = 11; runSet1 >= 3; runSet1--) {
      analogWrite(runSet1, outputValue);
      delay(100);
      digitalWrite(runSet1, LOW);
      delay(30);
    }
  } else if (pot1Value > 400 && pot1Value < 500) {
    for (int runSet1 = 3; runSet1 <= 11; runSet1++) {
      digitalWrite(runSet1, LOW);
    }
  }



  Serial.print("Case Trigger =");
  Serial.print(pot1Value);
  Serial.print("\t Brightness =");
  Serial.print(outputValue);
  Serial.print("\tButton Value =");
  Serial.println(buttonValue);
  delay(1);
}

SelfMade2.0.ino (1.74 KB)

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor you will not have access to this useful tool. I recommend you to use the standard IDE instead.

DarkKing202:
was wondering if it's possible(more likely how) to repeat a (if) statement until it becomes false.

For that purpose, you want to use while(), not if(). See:

So I tried using (while) and it works perfectly. But now when I let the button go it doesn't stop. What could be causing this?

 while (buttonValue == 1) {
    for (int brightness = 0; brightness <= 255; brightness++) {
      for (int runSet1 = 3; runSet1 <= 11; runSet1++) {
        analogWrite(runSet1, brightness);
        delay(1);
      }
    }
  }

And the new full code is attached. I'm also going to put an edit into the original question.

SelfMade2.0.ino (1.74 KB)

The fact that you never update buttonValue in your while()

Do you think you might need to reread the button in the loop to see if it is no longer pressed? Perhaps something like:

   while (digitalRead(myButton) != 0) {        // Or whatever value it needs to be
      // code...
   }

Hi, can you elaborate on that? I understand why it needs to be done I'm just not sure how. I'll post the full code.

const int potInPut = A0; 
const int pot2InPut = A1;
static const int analogPins[] = {3,5,6,9,10,11};
const int buttonInPut = 2;

void setup() {
 
  Serial.begin(9600);
  for (int runSet1 = 3; runSet1 <= 11; runSet1++) {
    pinMode(runSet1, OUTPUT);
  }
}

void loop() {
  int pot1Value = analogRead(potInPut);
  int pot2Value = analogRead(pot2InPut);
  int outputValue = map(pot2Value, 0, 1023, 0, 255);
  int buttonValue = digitalRead(buttonInPut);

  while (digitalRead(buttonValue) == 1) {                                   //this is the block
    for (int brightness = 0; brightness <= 255; brightness++) {
      for (int runSet1 = 3; runSet1 <= 11; runSet1++) {
        analogWrite(runSet1, brightness);
        delay(1);
      }
    }
  }
  
 
        if (pot1Value < 400) {
          for (int runSet1 = 3; runSet1 <= 11; runSet1++) {
            analogWrite(runSet1,outputValue);
            delay(100);
            digitalWrite(runSet1,LOW);
            delay(30);
          }
        }

                  if (pot1Value > 500) {
                    for (int runSet1 = 11; runSet1 >= 3; runSet1--) {
                      analogWrite(runSet1,outputValue);
                      delay(100);
                      digitalWrite(runSet1,LOW);
                      delay(30);
                    }
                  }
                              if (pot1Value > 400 < 500) {
                                for (int runSet1 = 3; runSet1 <= 11; runSet1++) {
                                  digitalWrite(runSet1,LOW);
                                }
                              }
   
   
   Serial.print("Case Trigger =");
   Serial.print(pot1Value);
   Serial.print("\t Brightness =");
   Serial.print(outputValue);
   Serial.print("\tButton Value =");
   Serial.println(buttonValue);
   delay(1);
}

Several things. First:

  int buttonValue = digitalRead(buttonInPut);

  if (buttonValue == 1) {                                 // Okay, it's pressed...

    while (digitalRead(buttonValue) != 0) {        // As long as it is still pressed...
       // do this stuff...
    }

Next, you have a series of if statement blocks:

        if (pot1Value < 400) {      
                // some code...
        }

        if (pot1Value > 500) {
               // some code
         }
         
 //    if (pot1Value > 400 < 500) {       // ERROR: Not a valid statement
        if (pot1Value > 400 && pot1Value < 500) {       // Corrected
                // Some code
        }

Now ask yourself this: If the first test is true (pot1Value < 400), why even perform the second or third tests? They can't possibly be true, so why execute those tests? To fix this, read up on the if-else statement block. Finally, the last if block has an error. Check the use of logical AND (&&) operator.

Yes one has to be careful about using while loop. If you miss something, it can remain permanently stuck in loop. such as

while (x < 0) {
  x ++;
}

As I already requested, please do an Auto Format on your code before posting it to the forum.