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.
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.
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.
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.
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.
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: