While loop not working

I want to create a system that for as long as you press a button, the led stays on. if you release the button, the led will turn of. But I also want the system to act in such a way that for as long as you press one button, the other buttons should not function, even if you press another button.

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
void setup()
{
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
}

void loop()
{
  buttonState1 = digitalRead(2);
  buttonState2 = digitalRead(3);
  buttonState3 = digitalRead(4);
  buttonState4 = digitalRead(5);
 
  digitalWrite(12, digitalRead(2));
  while(buttonState1 == true){
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
    break;
  }
  digitalWrite(11, digitalRead(3));
  while(buttonState2 == true){
    digitalWrite(12, LOW);
    digitalWrite(10, LOW);
    break;
  }
  digitalWrite(10, digitalRead(4)); 

  if (buttonState4 == 1) {
    digitalWrite(12, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(10, HIGH);
  }
  
  digitalWrite(8, HIGH);
  
  delay(10);
}

I think the problem lies within the While loop. Any suggestions?

Yes, it is the while loops. Can you write the sketch without any while loop ?

The line "buttonState1 = digitalRead();" reads an input pin and puts that in a variable.
The line "while(buttonState1 == true){" checks the variable over and over again, but the variable never changes, the variable is only read.

Also those break statements are making you while loops into if statements.

What did you mean for them to accomplish?

a7

I am a beginner and I have no idea how to write my code without a while loop. Basicly I want to say, while the first button is pressed, the other buttons should be turned of. But it doesn't work.

I tried removing the break at the end. That actually solved my problem, by removing the break, no other button that was pressed turned a led on. But, when I removed the break, and then pressed one of the buttons, the led didn't power of when I released the button.
Thus introducing a new problem.

Basicly I want to say, while one button is pressed, the rest should be turned of and shouldn't turn on a led, even if their pressed.

Yeah, so you don't need a while loop, you said it almost

IF one button is pressed, the rest should turn off.

a7

Do you think by changing the while loops to if statements, the problem will resolve itself?

I just changed the while loops to IF's and it didn't work.

OK, so you do maybe want to use a while statement.

But read and appreciate first #2 @Koepel points out that you have to digitalRead() the pushbutton again to see if it has changed.

The while will hold you in place. Do to contact bouncing, you'll have to put some small delay in your while loops, like 20 milliseconds, so it will not react immediately and exit the loop.

And this is not how you'll want to do this kind of thing for very long, but it will work here.

Three while loops, each hogging up everything as long as its correspond button is being pressed.

a7

Another question:

Is it better to use buttonState == 1 or buttonState == true?

Or is it the same thing

I am a bit unclear on what you mean by this.

I inserted delays as you said, and it dims the light, so it is working. Not sure I understand why it is dimming the light.

Thanks for your help so far by the way! And please excuse my total lack of knowledge in this front, as I said, I am a beginner

How would you suggest I make it better?

Switches are active LOW. Your loop thinks all the buttons are on all the time.
Try:

buttonState1 = !digitalRead(2);

Note the ! (not) operator.

Please post your latest new code in a new addition to this thread.

Post complete sketches that compile.

I know you did what you think I meant, but there's only one way to know for sure.

Also, @madmark2150 points out you need to reverse the logic of the buttons, either when you read them or just remembering to compare the reading to LOW if you want to see if a button is presst.

I prefer to handle it immediately I digitslRead() the button using the '!' operator as suggested. Then you'd compare to HIGH to see if the button was pressed.

a7

This

  while(buttonState2 == true){
    digitalWrite(12, LOW);
    digitalWrite(10, LOW);
  }

will never exit, because buttonState2 is unchanged by anything inside the loop.

There is no continuous magic association between that variable and the state of the input pin.

The input pin must be read again and its value assigned to the variable for that variable to accurately reflect the current state of the button.

a7

That's one of the problems with the OP @janesmostert having two related threads running: that was pointed out at least twice in another thread already but didn't "take" so to speak:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.