Another i'else' without previous 'if' question

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.

const int ledPin = 11;
const int ledPinB = 9;
const int ledPinC = 13;
	void setup (){
  pinMode(ledPin, OUTPUT);
  pinMode(ledPinB, OUTPUT);
      pinMode(ledPinC, OUTPUT);
    
};
void loop(){
  digitalWrite(ledPin, HIGH);
  digitalWrite(ledPinB, LOW);
    delay(1000);
    digitalWrite(ledPin, LOW);
  digitalWrite(ledPinB, HIGH);
    delay(1000);
  if(ledPin == HIGH)
  {
    digitalWrite(ledPinC, HIGH);
  }; 
  else(ledPin == LOW)
  {
    digitalWrite(ledPinC, LOW);
  };
    };

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!

Hi Joshua

  if(ledPin == HIGH)
  {
    digitalWrite(ledPinC, HIGH);
  };  <====================================== HERE
  else(ledPin == LOW)
  {
    digitalWrite(ledPinC, LOW);
  };

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?

Regards

Ray

  if(ledPin == HIGH)
  {
    digitalWrite(ledPinC, HIGH);
  };

Why are you ending all of your blocks with ;?

The only blocks that need to end with ; are those that define classes, structs, and arrays.

also just for my self, is it valid to have a compasion on a "Else" statemen

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):

void loop(){
  digitalWrite(ledPin, HIGH);
  digitalWrite(ledPinB, LOW);
    delay(1000);
    digitalWrite(ledPin, LOW);
  digitalWrite(ledPinB, HIGH);
    delay(1000);
  if(digitalRead(ledPin == HIGH))
  {
    digitalWrite(ledPinC, HIGH);
  }
  else
  {
    digitalWrite(ledPinC, LOW);
  };
    };

Why are you ending all of your blocks with ;?

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!

Josh

if(digitalRead(ledPin == HIGH))

This is better but not right. You are reading the value on an input pin, where the pin number is the result of comparing ledPin and HIGH.

You should read the value of pin number ledPin, and then compare that value with HIGH:

if (digitalRead(ledPin) == HIGH)
  digitalWrite(ledPinB, LOW);

delay(1000);
   digitalWrite(ledPin, LOW);
 digitalWrite(ledPinB, HIGH);
   delay(1000);

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.

also just for my self, is it valid to have a compasion on a "Else" statemen

If you do, it becomes and else if statement, not an else statement. Yes, else if is perfectly valid.