'else' without a previous 'if' error in ide

void loop()
{
  if (digitalRead(SwitchPin1) == LOW);
  { 
      SentMessage[0] = 111;
      radio.write(SentMessage, 1);
  }
    else if (digitalRead(SwitchPin2) == LOW);
  {
      SentMessage[0] = 000;
      radio.write(SentMessage, 1);
  }
}

error 'else' without a previous if
can anyone help

OLD:

void loop()
{
  if (digitalRead(SwitchPin1) == LOW);
  { 
      SentMessage[0] = 111;
      radio.write(SentMessage, 1);
  }
    else if (digitalRead(SwitchPin2) == LOW);
  {
      SentMessage[0] = 000;
      radio.write(SentMessage, 1);
  }
}

NEW:

void loop()
{
  if (digitalRead(SwitchPin1) == LOW)
  { 
      SentMessage[0] = 111;
      radio.write(SentMessage, 1);
  }
    else if (digitalRead(SwitchPin2) == LOW)
  {
      SentMessage[0] = 000;
      radio.write(SentMessage, 1);
  }
}

Have a nice day and enjoy programming in C++ and learning.
Errors and omissions excepted.

1 Like

Lose the semi colons

if (digitalRead(SwitchPin1) == LOW);
else if (digitalRead(SwitchPin2) == LOW);

They prematurely end those lines. So the "else if" doesn't have an "if" to belong to, since the "if" closed early.

The lines in the {} pairs will always run, since their respective "if" and "else if" are ended early.

1 Like

Welcome to the forum

if (digitalRead(SwitchPin1) == LOW);

The only code in the code block for the if is the semicolon, hence the error

delete the semicolon

1 Like

ty so much :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.