Hi All,
Noob trying to code. I've purchased the SIK starter kit. As good practice, Im rewriting each of the examples from scratch. This particular "project" is about dealing with buttons as digital reads. I've recreated (retyped from memory and hopefully understanding) the code successfully.
Then I decided to rewrite the for loop using a "while" statement, which seems to make better sense. i.e. Do something while a button is being pressed.
However, although the LED turns on when I press either button, it then stays on?
The for loop is commented out, in order for me to practice the while loop (but I assure you it works). In the for loop, either button turns on the LED while pressed and more importantly, the LED turns off when I release the button.
Just to clarify, when I attempt the while statement, the LED turns on if I press either button, but then remains on, as if to mock me of my stupidity whilst its penetrating light burns through to my very soul.
Please spot the obvious flaw, and chuckle as you dish out some coding wisdom to an obvious noob:
const int button1PIN = 2;
const int button2PIN = 3;
const int LEDPIN = 13;
void setup()
{
pinMode (button1PIN,INPUT);
pinMode (button2PIN,INPUT);
pinMode (LEDPIN,OUTPUT);
}
void loop()
{
int BS1 = digitalRead(button1PIN);
int BS2 = digitalRead(button2PIN);
while ((BS1 == LOW) || (BS2 == LOW))
{
digitalWrite (LEDPIN, HIGH);
}
digitalWrite (LEDPIN, LOW);
/*if ((BS1 == LOW) || (BS2 == LOW))
{
digitalWrite (LEDPIN, HIGH);
}
else
{
digitalWrite (LEDPIN, LOW);
}*/
}
int BS1 = digitalRead(button1PIN);
int BS2 = digitalRead(button2PIN);
while ((BS1 == LOW) || (BS2 == LOW))
{
digitalWrite (LEDPIN, HIGH);
}
digitalWrite (LEDPIN, LOW);
In order for the end condition to ever become true, you need to update the values of BS1 and BS2 at some point during the execution of the loop. For example, this would be one way:
while ((digitalRead(button1PIN) == LOW) || (digitalRead(button2PIN) == LOW))
{
digitalWrite (LEDPIN, HIGH);
}
digitalWrite (LEDPIN, LOW);
The while loop is a very restrictive way to implement this because your sketch is not capable of doing anything else until the end conditions are met and it drops out of te while loop. That might be acceptable in a trivial test case, but in the more general case you would want to choose an approach that makes it possible to handle multiple tasks concurrently. The principle way to do that is to design your code to be non-blocking. The original if/else implementation is better in this respect.
You don't have a for loop, you have an if....
Once you're in the while, it never looks at the pin again, so it doesn't know you've let it go. Remember the while is a loop so it's hanging around in there.
By contrast, the if is checked once and a certain decision is made. Then, loop() takes you back to the top and it reads the switches again, so next time thru the if, the decision is re-made, and the result may or may not be different from the previous visit.
PeterH:
int BS1 = digitalRead(button1PIN);
int BS2 = digitalRead(button2PIN);
while ((BS1 == LOW) || (BS2 == LOW))
{
digitalWrite (LEDPIN, HIGH);
}
digitalWrite (LEDPIN, LOW);
In order for the end condition to ever become true, you need to update the values of BS1 and BS2 at some point during the execution of the loop. For example, this would be one way:
while ((digitalRead(button1PIN) == LOW) || (digitalRead(button2PIN) == LOW))
{
digitalWrite (LEDPIN, HIGH);
}
digitalWrite (LEDPIN, LOW);
The while loop is a very restrictive way to implement this because your sketch is not capable of doing anything else until the end conditions are met and it drops out of te while loop. That might be acceptable in a trivial test case, but in the more general case you would want to choose an approach that makes it possible to handle multiple tasks concurrently. The principle way to do that is to design your code to be non-blocking. The original if/else implementation is better in this respect.
Ahah! I get it now. Thank you. Makes perfect sense. And more importantly, now that I understand it, I have more of an appreciation of what to use AND when. Thanks again.
JimboZA:
You don't have a for loop, you have an if....
Once you're in the while, it never looks at the pin again, so it doesn't know you've let it go. Remember the while is a loop so it's hanging around in there.
By contrast, the if is checked once and a certain decision is made. Then, loop() takes you back to the top and it reads the switches again, so next time thru the if, the decision is re-made, and the result may or may not be different from the previous visit.
My bad. I meant "if". Was stuck on for loops the other day. See my (only) previous post.
Btw, are you in South Africa? My previous help has mainly been from the UK crowd (time difference) and then I noticed your ZA username. If (you dont mind) {pm me your contact details so we can watsapp - id love to see what youve built so far} else {I understand}; 
I tried the following as a solution which worked. However, the LED seems dimmer. I believe this is because its constantly switching on and off. Can somebody please verify this:
while ((BS1 == LOW) || (BS2 == LOW))
{
digitalWrite (LEDPIN, HIGH);
break;
}
digitalWrite (LEDPIN, LOW);
It still works, but its obviously incorrect. ??
The break; is exiting the while loop unconditionally and the LED is then turned off only to be turned on again next time through the loop() function if the while conditions are met. Take out the break; you don't need it.
The purpose of "break" is to force an exit under unusual circumstances, outside of the parameters of the loop. The example here shows that the if should go round 255 times, BUT it kicks out regardless of where it got to in the sequence if the sensor says it must.
In your example, you're breaking on every pass through the while, becasue the break itself isn't conditional- it happens always. So if it goes in there based on the switches, it puts the led on, immediately breaks and the next line turns it off again. Then loop() takes you back to the top and if the switch is still pressed it goes into the while, turns on, breaks, turns off. So yes I think it's dim becasue it's actually going on and off very very fast.
Not sure what you were hoping that the break would buy you there?
Understood thanks. Was just trying to force a while loop to mimic the better suited "if" statement for this example. Im not too impressed with the while loop at this stage, although Im sure I will find a time when it does become useful.
Just seems like if/for can do a better job. But pay no mind to me, these are the ramblings of a noob programmer still fascinated by turning an LED on and off.
Thanks again. The learning journey continues....