I'm relativley new with Arduino and programming in general. I've Just been playing around with it for a few days and I can't seem to get this basic "if" statement to work. I am trying to use as a counter so that "if" the LED flashes 50 times, it will turn off for 5 seconds, then reset the counter. Here is the code I have:
int LED = 6; //Assigning pin 6 as "LED"
int FlashCount = 0; //Flashcount setup
void setup() {
pinMode (LED, OUTPUT);
Serial.begin (9600);
}
void loop() {
digitalWrite(LED, HIGH); //LED on for 50 mills
delay (50);
digitalWrite(LED, LOW); //LED off for 50 mills
delay (50);
FlashCount = FlashCount + 1; //Counter + 1
Serial.print ("Flash Count = ");
Serial.println (FlashCount);
if (FlashCount == 50); //Flascount = 50?
{
digitalWrite(LED, LOW); //Do this if so
delay(5000);
FlashCount == 0; //Resets the counter
}
}
The program seems to simply skip the "if" statement all together and go straight to the 5 second "LOW" delay. so my LED will flash once and then turn off for 5 seconds. I'd like to figure this out so I can use it in more complicated programs.
Generally yes - but in this case the line of code is "extended", so to speak:
if (condition is met) { do stuff in here; } // so the line ends a little farther out, where the } is.
else {do stuff in here instead;} //and this part is optional, also ending where the } is.
Kemare:
Haha, yep that fixed it. Thanks! I just figured every line of code required a semi-colon after it.
It isn't lines, as C isn't line-oriented.
Statements require a semicolon after them, but statements can be recursively defined.
For example:
a = a + 1;
That's a statement. But you could write it as:
a
=
a
+
1;
Thus you can see that you don't just plonk semicolons at the end of physical lines.
Now an "if" statement is defined as:
if (<condition>) <statement>
So the semicolon goes after the whole "if statement" not the middle of it.
Notice how an "if" statement has a statement as part of it (this is the recursive definition). Thus the semicolon goes at the end of the whole thing, not the middle.