Hello ! I have a question. I can only use while ( no if/ case / goto )
Therefore, If i have , say 4 push buttons , 1 start push button
An initial of digitalWrite(redled, HIGH);
and i am required to write a programe, for example to on a green led(but1 controls), blue led(but2 controls) , yellow led(but3 controls) and a pink led(but4 controls).
With only whiles , how do i do this? i am stuck with this
startvar=digitalRead(start);
While(startpushbutton==HIGH)
{
digitalWrite(redled, HIGH);
but1var=digitalRead(but1);
}
but1var=digitalRead(but1);
while(but1var==LOW) //
{
digitalWrite(greenled, HIGH);
but
????
????
????
?????
Thanks in advance!
Hey!! sorry can I know what exactly you are trying to do ?
I mean as far as I understood this ...
button 1 will ON green led
button 2 will ON blue led
button 3 will ON yellow led
button 4 will ON pink led
then what is function of start push button ? red led ?
It's a strange requirement. Homework? Anyway, you can break from a while loop
If your code (using if) would be something like
if(digitalRead(btnRed) == HIGH)
{
digitalWrite(redLed, HIGH);
}
if(digitalRead(btnGreen) == HIGH)
{
digitalWrite(redGreen, HIGH);
}
You can change it to
while(digitalRead(btnRed) == HIGH)
{
digitalWrite(redLed, HIGH);
break;
}
while(digitalRead(btnGreen) == HIGH)
{
digitalWrite(redGreen, HIGH);
break;
}
Even without the use of "break;" you can still use a 'while' in place of an 'if':
// While the input is HIGH and the output is LOW
while (digitalRead(start) == HIGH && digitalRead(redled) == LOW) {
// Change the output to HIGH
digitalWrite(redled, HIGH);
}
// While the input is LOW and the output is HIGH
while (digitalRead(start) == LOW && digitalRead(redled) == HIGH) {
// Change the output to LOW
digitalWrite(redled, LOW);
}
johnwasser:
Even without the use of "break;" you can still use a 'while' in place of an 'if':
Nice. Thinking outside the box is an art 