' } else { ' Error

I made this small section of code for the arduino:

/*
Door Lock
By Alex Mann

Based off of Button Example

This is a simple script that locks and unlock a door (acuator) and turns on a led
motor1 is to unlock
motor2 is to lock (Connect to switch that disconects the motor's circut when it is extended all the way out)
 */

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 2;     // the number of the pushbutton pins
const int buttonPin2 = 4;
const int buttonPin3 = 8;
const int buttonPin4 = 12;
const int ledPin1 =  3;      // the number of the LED pins
const int ledPin2 =  5;
const int motorPin1 =  9;     // the number of the motor pins
const int motorPin2 =  10;


// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);

}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin1);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(motorPin1, HIGH);
    delay(3000);
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(motorPin1, LOW);
  } else {
     buttonState = digitalRead(buttonPin2);
       if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(motorPin2, HIGH);
    delay(5000);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin1, LOW);
    digitalWrite(motorPin2, LOW);
  }
  } else {
     buttonState = digitalRead(buttonPin3);
       if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(motorPin1, HIGH);
    delay(3000);
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(motorPin1, LOW);
  }
  } else {
         buttonState = digitalRead(buttonPin4);
       if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(motorPin2, HIGH);
    delay(5000);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin1, LOW);
    digitalWrite(motorPin2, LOW);
  }
  }
}

and I get this error:

C:\Users\X\Documents\Arduino\Door_Unlock1\Door_Unlock1.ino: In function 'void loop()':

Door_Unlock1:63: error: 'else' without a previous 'if'

   } else {

     ^

Door_Unlock1:73: error: 'else' without a previous 'if'

   } else {

     ^

exit status 1
'else' without a previous 'if'

What does this mean and how do I fix it?

  }
  } else {

Why two closing braces before the else ?

for the if (buttonState == HIGH) {

Put each opening and closing brace on its own line and do an Auto Format in the IDE

You will see that you have if/else/else like this

  if (buttonState == HIGH)  //if
  {
    // turn LED on:
    digitalWrite(motorPin1, HIGH);
    delay(3000);
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(motorPin1, LOW);
  }
  else                               //else
  {
    buttonState = digitalRead(buttonPin2);
    if (buttonState == HIGH)
    {
      // turn LED on:
      digitalWrite(motorPin2, HIGH);
      delay(5000);
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin1, LOW);
      digitalWrite(motorPin2, LOW);
    }
  }
  else                                 //second else with no preceding if
  {

There is a second "If"

void loop() { // read the state of the pushbutton value: buttonState = digitalRead(buttonPin1);

// check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState == HIGH) { digitalWrite(motorPin1, HIGH); delay(3000); digitalWrite(ledPin1, HIGH); digitalWrite(ledPin2, LOW); digitalWrite(motorPin1, LOW); } ** else** { buttonState = digitalRead(buttonPin2); ** if (buttonState == HIGH)** ** {** digitalWrite(motorPin2, HIGH); delay(5000); digitalWrite(ledPin2, HIGH); digitalWrite(ledPin1, LOW); digitalWrite(motorPin2, LOW); } } ** else** { buttonState = digitalRead(buttonPin3); ** if (buttonState == HIGH)** { digitalWrite(motorPin1, HIGH); delay(3000); digitalWrite(ledPin1, HIGH); digitalWrite(ledPin2, LOW); digitalWrite(motorPin1, LOW); } } ** else** { buttonState = digitalRead(buttonPin4); if (buttonState == HIGH) { digitalWrite(motorPin2, HIGH); delay(5000); digitalWrite(ledPin2, HIGH); digitalWrite(ledPin1, LOW); digitalWrite(motorPin2, LOW); } } }

Your first "if" has two three "else"s.

ok, is there any other way to do this with out all of the elses and if's (this is my first ever script)

What is your intended logic?

(and please, please, use code tags when posting code)

skydazz2000: ok, is there any other way to do this with out all of the elses and if's (this is my first ever script)

There is nothing wrong using "if" construct, but you need to make sure it is logically correct. Add some comments to your code to indicate what is does.

For example, in pseudocode:

if(button pushed ) { run motor for 3 seconds turn on indicators } else { run motor for 10 seconds turn on indicators }

If you position your mouse cursor on the starting bracket ( { ) the IDE will show you where the corresponding bracket is so yu can follow the logic flow.

And if you use AutoFormat ( Tools) the IDE will line all the brackets for you , assuming they are in pairs.

And there are no practical limits to nest "if's / else ". The following is just "cut and paste of the first example.

if(button pushed ) { run motor for 3 seconds turn on indicators

another if nested here

if(button pushed ) { run motor for 3 seconds turn on indicators } else { run motor for 10 seconds urn on indicators }

} else { run motor for 10 seconds urn on indicators }

PS It may be easier for you to assign each button to its own , separate state, again in pseudocode

button 1 state = read button 1 button 2 state = read button 2

Good luck

Here is your Autofomatted code to illustrate the brackets pairs identification using mouse. 


void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin1);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH)
  {     if start 
    digitalWrite(motorPin1, HIGH);
    delay(3000);
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(motorPin1, LOW);
  }   if end 
  else
  { else start 
    buttonState = digitalRead(buttonPin2);
    if (buttonState == HIGH)
    {
      digitalWrite(motorPin2, HIGH);
      delay(5000);
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin1, LOW);
      digitalWrite(motorPin2, LOW);
    }
  } else end 

orphaned else 
  else
  {
    buttonState = digitalRead(buttonPin3);
    if (buttonState == HIGH)
    {
      digitalWrite(motorPin1, HIGH);
      delay(3000);
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, LOW);
      digitalWrite(motorPin1, LOW);
    }
  }
  else
  {
    buttonState = digitalRead(buttonPin4);
    if (buttonState == HIGH)
    {
      digitalWrite(motorPin2, HIGH);
      delay(5000);
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin1, LOW);
      digitalWrite(motorPin2, LOW);
    }
  }
}

What @Vaclav's post fails to make clear is the importance of indentation, which makes the structure of the code much easier to read. The compiler cares not a jot about indentation, but those who read your code will appreciate it.

That definitely help but still not sure by what is meant when you say "Nested". Do you mean for an example:

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin1);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH)
  {
    digitalWrite(motorPin1, HIGH);
    delay(3000);
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(motorPin1, LOW);
    buttonState = digitalRead(buttonPin2);
    if (buttonState == HIGH)
  }
  else
  {
    buttonState = digitalRead(buttonPin2);
    if (buttonState == HIGH)
    {
      digitalWrite(motorPin2, HIGH);
      delay(5000);
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin1, LOW);
      digitalWrite(motorPin2, LOW);
    buttonState = digitalRead(buttonPin3);
    if (buttonState == HIGH)
    }
  }
  else
  {
    buttonState = digitalRead(buttonPin3);
    if (buttonState == HIGH)
    {
      digitalWrite(motorPin1, HIGH);
      delay(3000);
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, LOW);
      digitalWrite(motorPin1, LOW);
    buttonState = digitalRead(buttonPin4);
    if (buttonState == HIGH)
    }
  }
  else
  {
    buttonState = digitalRead(buttonPin4);
    if (buttonState == HIGH)
    {
      digitalWrite(motorPin2, HIGH);
      delay(5000);
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin1, LOW);
      digitalWrite(motorPin2, LOW);
    }
  }
}

My code with lables (To show intentions)

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin1);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH)
  {
    //Turn motor on
    digitalWrite(motorPin1, HIGH);
    //Motor will run for 3 seconds before contiuing
    delay(3000);
    //Turn Green LED on
    digitalWrite(ledPin1, HIGH);
    //Turn Red Off
    digitalWrite(ledPin2, LOW);
    //Turn Motor Off
    digitalWrite(motorPin1, LOW);
  }
  else
  {
    //Read State of Button2
    buttonState = digitalRead(buttonPin2);
    //If its pressed ...
    if (buttonState == HIGH)
    {
      //Spin motor 2(Locking)
      digitalWrite(motorPin2, HIGH);
      //For 5 Seconds
      delay(5000);
      //Turn Red LED on
      digitalWrite(ledPin2, HIGH);
      //Turn Green Off
      digitalWrite(ledPin1, LOW);
      //Turn Motor Off
      digitalWrite(motorPin2, LOW);
    }
  }
  else
  {
    buttonState = digitalRead(buttonPin3);
    if (buttonState == HIGH)
    {
      digitalWrite(motorPin1, HIGH);
      delay(3000);
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, LOW);
      digitalWrite(motorPin1, LOW);
    }
  }
  else
  {
    buttonState = digitalRead(buttonPin4);
    if (buttonState == HIGH)
    {
      digitalWrite(motorPin2, HIGH);
      delay(5000);
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin1, LOW);
      digitalWrite(motorPin2, LOW);
    }
  }
}
void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin1);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH)
  {
    //Turn motor on
    digitalWrite(motorPin1, HIGH);
    //Motor will run for 3 seconds before contiuing
    delay(3000);
    //Turn Green LED on
    digitalWrite(ledPin1, HIGH);
    //Turn Red Off
    digitalWrite(ledPin2, LOW);
    //Turn Motor Off
    digitalWrite(motorPin1, LOW);
  }
  else
  {
    //Read State of Button2
    buttonState = digitalRead(buttonPin2);
    //If its pressed ...
    if (buttonState == HIGH)
    {
      //Spin motor 2(Locking)
      digitalWrite(motorPin2, HIGH);
      //For 5 Seconds
      delay(5000);
      //Turn Red LED on
      digitalWrite(ledPin2, HIGH);
      //Turn Green Off
      digitalWrite(ledPin1, LOW);
      //Turn Motor Off
      digitalWrite(motorPin2, LOW);
    }
    else
    {
      buttonState = digitalRead(buttonPin3);
      if (buttonState == HIGH)
      {
        digitalWrite(motorPin1, HIGH);
        delay(3000);
        digitalWrite(ledPin1, HIGH);
        digitalWrite(ledPin2, LOW);
        digitalWrite(motorPin1, LOW);
      }
      else
 .. etc

Maybe?

this is the proper syntax for an if/else statement.

void loop()
{ //<<<<<<<<< opening bracket for loop()
  if(true)
   {  //<<<<<<< opening bracket for the if
     do stuff;
   }  // <<<<<<  closing bracket for the if
   else
   {  //<<<<<<< opening bracket for the else 
     do stuff;
   }  // <<<<<< closing bracket for the else 
} //<<<<<<<<< closing bracket for loop()

notice how everything has 1 opening and 1 closing bracket and no more.
never put 2 else statements after an if.

it should be

if(something is true)
{
  do stuff;
}
else if(another thing is true)
{
  do stuff;
}
else
{
  do stuff;
}

you can have as many else if statements as you want.
but only 1 else.

if/else is meant to be used as a single set

Should you need more comparisons, use 'else if' in between if and else.

'if' is the comparison

in here only 'else if' can add more comparisons. That is what is causing the error, using else where else if is required.

'else' is what happens if the comparison(s) is/are false

-fab

I got this with no errors: Will this work right?

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin1);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH)
  {
    //Turn motor on
    digitalWrite(motorPin1, HIGH);
    //Motor will run for 3 seconds before contiuing
    delay(3000);
    //Turn Green LED on
    digitalWrite(ledPin1, HIGH);
    //Turn Red Off
    digitalWrite(ledPin2, LOW);
    //Turn Motor Off
    digitalWrite(motorPin1, LOW);
    buttonState = digitalRead(buttonPin2);
  }
  else if (buttonState == HIGH)
  { 
      //Spin motor 2(Locking)
      digitalWrite(motorPin2, HIGH);
      //For 5 Seconds
      delay(5000);
      //Turn Red LED on
      digitalWrite(ledPin2, HIGH);
      //Turn Green Off
      digitalWrite(ledPin1, LOW);
      //Turn Motor Off
      digitalWrite(motorPin2, LOW);
      buttonState = digitalRead(buttonPin3);
  }
  else if (buttonState == HIGH)
  {

      digitalWrite(motorPin1, HIGH);
      delay(3000);
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, LOW);
      digitalWrite(motorPin1, LOW);
    buttonState = digitalRead(buttonPin4);
  }
  else if (buttonState == HIGH)
  {
    
    
    
      digitalWrite(motorPin2, HIGH);
      delay(5000);
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin1, LOW);
      digitalWrite(motorPin2, LOW);
    
  }
}

You have:

if (buttonState == HIGH)
{
}
else if (buttonState == HIGH)
{
}
else if (buttonState == HIGH)
{
}
else if (buttonState == HIGH)
{
}

Does that really make sense to you?

Regards, Ray L.

The syntax looks much better but,

All your if , else if statements all have the same condition (buttonState == HIGH) so they all will activate when the button is pressed.

The only thing that is keeping them from happening at the same time is all those delay() statements. I'm guessing that's not your intention.

Those delay() statements are going to interfere with reading your button. delay() stops everything till it expires so we need to work on that. What is needed is timing code.

Here are several tutorials that will show you how that's done. Blink without Delay Several things at the same time Multi-tasking the Arduino

I don't want to guess what your trying to do so maybe you could explain what you want to happen with all those else if's .

It appears that you wish to read four different buttons. In a confusing style of coding you read them all into the same variable called buttonState. The digitalRead() of button2 is buried within the code to be executed if button1 is HIGH, and similarly the digitalRead() of button3 is buried with the code to be executed if button2 is HIGH, and button4 is within the button3 action.

If button1 is not high, none of the others will ever get read. Is this what you really intend?

Hutkikz: The syntax looks much better but,

All your if , else if statements all have the same condition (buttonState == HIGH) so they all will activate when the button is pressed.

No, ONLY the first one will ever execute. In fact, it's likely the compiler will be smart enough to optimize the others completely away, since they CANNOT ever be executed.

Regards, Ray L.