Hi there, reluctantly I am asking for help with the "if" command. The attached (amateur) code was intend to test how "if" works and the results were not as expected. Instead of the first "if" statements looping to flash the led ten times it drops through and flashes the red and green led from the second "if" statements. I am trying to constrain the flow to the "if" statements independently. I have read many articles and forum entries but it has not helped. Any help would be appreciated.
The behavior you see should not be unexpected. There's no looping construct in your sketch to make your code work the way you expect. You could replace the if statements with while though and that should give you what you want.
aheighway:
Instead of the first "if" statements looping to flash the led ten times it drops through and flashes the red and green led from the second "if" statements. I am trying to constrain the flow to the "if" statements independently. I have read many articles and forum entries but it has not helped. Any help would be appreciated.
An if is not a loop. An if will perform all the actions within the { and }, then drop out.
If you want to loop, you have to explicitly loop with a while or a do, or a for.
Here's your code, modified to use a for loop for the green LED, and a while loop for the red LED. Note that you do not need to increment greenCounter within the for loop, as we have used it as the counter in the for loop.
int greenLed = 3; // green led connected to pin 3
int redLed = 4; // red led connected to pin 4
int greenCounter = 0; // initialise green counter
int redCounter = 0; // initialise red counter
void setup() {
pinMode(greenLed,OUTPUT); // make pin 3 an output
pinMode(redLed,OUTPUT); // make pin 4 an output
Serial.begin(115200); // start serial monitor
}
void loop()
{
for(greenCounter = 0; greenCounter < 10; greenCounter ++) // test greenCounter
{ // repeat this code until greenCounter = 10
Serial.print("greenCounter "); // print "greenCounter" for testing
Serial.println(greenCounter); // print the value of greenCounter
digitalWrite(greenLed,HIGH); // turn on green led
delay(400); // wait one second
digitalWrite(greenLed,LOW); // turn off green led
delay(400); // wait one second
// greenCounter++; // increment the greenCount
} // end of if green statements
greenCounter = 0;
while(redCounter < 10) // test redCounter
{ // repeat this code until redCounter =10
Serial.print("redCounter "); // print "redCounter" for testing
Serial.println(redCounter); // print the value of redCounter
digitalWrite(redLed,HIGH); // turn on red led
delay(400); // wait one second
digitalWrite(redLed,LOW); // turn off red led
delay(400); // wait one second
redCounter++; // increment the redCount
} // end of red statements
redCounter = 0;
Serial.println("end of code. No, wait! We're looping in loop()"); // finally print this to show end of "void loop"
}
I have also shown you a could of other things: Serial.print() followed by Serial.println() to make things show up on one line, and a comment at the "end of code".
Thanks lar3ry, my led test routine was to learn to understand the" if" routine which was going to be applied to drive a stepper motor because I want actions depending upon switches being operated. How do I see the code you have posted because I cannot see it. I really do appreciate your help.
my led test routine was to learn to understand the" if" routine
Here's another modification of your code. I show you two other ways of specifying pins and two ways of specifying a conditional for the two switches.
If you wire a switch between pin 5 and GND, and another between pin 6 and GDN, you can control the flashing... neither, red only, green only, or both. The code compiles, but I have not tested it.
#define greenButton 5
const byte redButton = 6;
int greenLed = 3; // green led connected to pin 3
int redLed = 4; // red led connected to pin 4
int greenCounter = 0; // initialise green counter
int redCounter = 0; // initialise red counter
void setup() {
pinMode(greenButton, INPUT_PULLUP);
pinMode(redButton, INPUT_PULLUP);
pinMode(greenLed,OUTPUT); // make pin 3 an output
pinMode(redLed,OUTPUT); // make pin 4 an output
Serial.begin(115200); // start serial monitor
}
void loop()
{
if ( digitalRead(greenButton) == LOW ) { // if switch closed
for(greenCounter = 0; greenCounter < 10; greenCounter ++) // test greenCounter
{ // repeat this code until greenCounter = 10
Serial.print("greenCounter "); // print "greenCounter" for testing
Serial.println(greenCounter); // print the value of greenCounter
digitalWrite(greenLed,HIGH); // turn on green led
delay(400); // wait one second
digitalWrite(greenLed,LOW); // turn off green led
delay(400); // wait one second
// greenCounter++; // increment the greenCount
} // end of if green statements
greenCounter = 0;
}
if ( ! digitalRead(redButton) ) { // if switch is not open
while(redCounter < 10) // test redCounter
{
Serial.print("redCounter "); // print "redCounter" for testing
Serial.println(redCounter); // print the value of redCounter
digitalWrite(redLed,HIGH); // turn on red led
delay(400); // wait one second
digitalWrite(redLed,LOW); // turn off red led
delay(400); // wait one second
redCounter++; // increment the redCount
} // end of red statements
redCounter = 0;
}
Serial.println("end of code. No, wait! We're looping in loop()"); // finally print this to show end of "void loop"
}