Hello, everyone. First, I wanted to say thank you to everyone that answers questions on this forum; it's been really helpful reading through old replies as I learn a little about the Arduino.
Also, I wanted to apologize in advance for asking a question that I know gets asked a lot. I've went through some archives and most of the fixes seem to involve deleting semi-colons, which I don't think is my problem. Any help here would be greatly appreciated!
I'm trying to learn the basics of programming the Arduino, so I decided I wanted to create a sketch that would blink certain lights on while others were off. I think I fundamentally don't understand if/else statements.
I don't have an Arduino yet, so I'm using 123circuits.io to program a fake one. The errors I'm getting are: 21:3: error: 'else' without previous 'if' and 22:3: expected ';' before '{' token.
I think the second error is only appearing because it doesn't recognize the 'if' statement; I've read on here that putting a semicolon between the parenthesis and curly braces is incorrect, so don't think that's a fix.
Again, any help would be greatly appreciated, and thanks again!
The semicolon before else is splitting your if / else into two statements: a valid if statement and an invalid else statement. Try with that semicolon removed, and remove the (ledPin == LOW) on the next line.
BUT ... you also need to think again about what you are testing for HIGH or LOW. Do you mean the pin number of the led pin, or is it meant to be the digitalRead value from that pin?
Thank you everyone for your quick replies! I know this is a simple program, but I learned a lot from it. I'm still picking the low hanging fruit, I guess.
The semicolon before else is splitting your if / else into two statements: a valid if statement and an invalid else statement. Try with that semicolon removed, and remove the (ledPin == LOW) on the next line.
BUT ... you also need to think again about what you are testing for HIGH or LOW. Do you mean the pin number of the led pin, or is it meant to be the digitalRead value from that pin?
Ray, thank you, that was a very helpful response. I made the changes you suggested, and the code is working great now! I am still discerning when it is appropriate to use 'digitalWrite', I suppose. This is my code now (the relevant portion):
The only blocks that need to end with ; are those that define classes, structs, and arrays.
Paul, thanks for your response. I am ending all my blocks with ';'s because honestly I thought I had to. When I didn't add semicolons, I got an error, something along the lines of "(line number) error: ';' expected". Thanks for letting me know they're not necessary.
Again, I appreciate your responses. I'm sure I'll be back!
Some advice: do a Tools > Auto Format on your sketch. This is very helpful for debugging because if the indents are off it indicates an issue with your code and also makes it easier for people on the forum to read.