What can cause "expected '}' before 'else'" error?

This is kind of driving me nuts. Thanks in advance for any help you may give.

I’m not a total noob but still a beginner. I keep getting the error “expected ‘}’ before ‘else’” but when I look at the code, the } is right there in plain sight. Here’s the full verbose error message (with the file name and location removed because it contains personal information):

Arduino: 1.8.9 (Windows Store 1.8.21.0) (Windows 10), Board: “Arduino/Genuino Uno”

In function ‘void motor_control()’:

Sensor___Motor_Trial:185:7: error: expected ‘}’ before ‘else’

} else if (abs(Azidif) <= ThrOFF); {

^

At global scope:

Sensor___Motor_Trial:190:1: error: expected declaration before ‘}’ token

}

^

exit status 1
expected ‘}’ before ‘else’

Now, here’s the code snippit (odd, the numbers didn’t come too)

void motor_control(void) {
  if (abs(Azidif) > ThrON) {
    if ( (avl > avr) && (digitalRead(LimitPinW) == HIGH))  //may need to add a threshold here too
    { //turn azimuth motor to the west
      digitalWrite(MDAziPinA, HIGH);
      digitalWrite(MDAziPinB, LOW);
      delay(500); //run for ½ second
      void motor_stop();
    } else if ((avr < avl) && digitalRead(LimitPinE) == HIGH); {
      //turn azimuth motor to the east – used to restart in AM)
      digitalWrite(MDAziPinA, LOW);
      digitalWrite(MDAziPinB, HIGH);
      //no delay,keep going until condition drops out)
      //void motor_stop(void);
    } else if (abs(Azidif) <= ThrOFF); {
      void motor_stop();
      delay(500);
    }
  }
}

This is draft code for a two-axis solar tracker. I’ve checked that all the { and } match. This was typed in, not copied and pasted from elsewhere. I have searched the forum, read a wide variety Arduino reference pages, and done a google search of the error. Learned lots of stuff, but didn’t find the source of the error.

There’s a screen shot too, though I can’t see it in the preview. I’ll attach the full code if necessary, perhaps the error source is elsewhere (but the compiler shows it on the highlighted line).

Error Capture.JPG

you have semicolons at the end of your else if… lines

remove them: else if ( conditions ) {

I find it much easier to troubleshoot when I put the top bracket above the bottom bracket:

void motor_control(void) 
{
  if (abs(Azidif) > ThrON)
 {
    if ( (avl > avr) && (digitalRead(LimitPinW) == HIGH))  //may need to add a threshold here too
    { //turn azimuth motor to the west
      digitalWrite(MDAziPinA, HIGH);
      digitalWrite(MDAziPinB, LOW);
      delay(500); //run for ½ second
      void motor_stop();
    }
     else if ((avr < avl) && digitalRead(LimitPinE) == HIGH) 
    {
      //turn azimuth motor to the east - used to restart in AM)
      digitalWrite(MDAziPinA, LOW);
      digitalWrite(MDAziPinB, HIGH);
      //no delay,keep going until condition drops out)
      //void motor_stop(void);
    } 
    else if (abs(Azidif) <= ThrOFF)
   {
      void motor_stop();
      delay(500);
    }
  }
}

there is no one right way, suit yourself.

The else if’s are ok… but take a close look at these cuts

inE) == HIGH); {
:::
 if (abs(Azidif) <= ThrOFF); {

Terminating the if statement before the next curly brace ?

[quote author=Geek Emeritus date=1559612456 link=msg=4197725] you have semicolons at the end of your else if... lines

remove them: else if ( conditions ) {

I find it much easier to troubleshoot when I put the top bracket above the bottom bracket:

[/quote]

Odd. Originally I didn't have semicolons at the end of if/else if, then thought I saw it in one of the tutorials and put them in. Took them all out, reformatted {} per your suggestion (yes, helps when they're lined up!)

The combination of misplaced semicolons and missing/extra curly brackets was causing various errors -- all gone now. Thanks for your help.

Post your updated code or provide all of us with a working crystal ball ;)