Code not working (else with no previous if)

#include <Servo.h>

Servo penguin; // create servo object to control a servo
// twelve servo objects can be created on most boards
Servo turn;

// constants won’t change. They’re used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int buttongreen = 8; // the number of the pushbutton pin
const int buttonred = 4; // the number of the pushbutton pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonStategreen = 0;
int buttonStatered = 0;
int pos = 0; // variable to store the servo position
int posg = 0;
int posr = 0;

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(buttongreen, INPUT);
pinMode(buttonred, INPUT);
penguin.attach(9); // attaches the servo on pin 9 to the servo object
turn.attach(7); // attaches the servo on pin 9 to the servo object
}

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

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
{
for (pos = 180; pos >= 90; pos -= 5) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
penguin.write(pos); // tell servo to go to position in variable ‘pos’
delay(1); // waits 15ms for the servo to reach the position
}
for (pos = 90; pos <= 180; pos += 1) { // goes from 180 degrees to 0 degrees
penguin.write(pos); // tell servo to go to position in variable ‘pos’
delay(100); // waits 15ms for the servo to reach the position
}
// read the state of the pushbutton value:
buttonStategreen = digitalRead(buttongreen);
}
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
;} else if (buttonStategreen == HIGH); {
// turn LED on:
digitalWrite(ledPin, HIGH);
{
for (posg = 180; posg >= 90; posg -= 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
turn.write(posg); // tell servo to go to position in variable ‘pos’
delay(1); // waits 15ms for the servo to reach the position
}
for (posg = 90; posg <= 180; posg += 1) { // goes from 180 degrees to 0 degrees
turn.write(posg); // tell servo to go to position in variable ‘pos’
delay(5); // waits 15ms for the servo to reach the position
}
} }
buttonStatered = digitalRead(buttonred);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
; else if (buttonStatered == HIGH); {
// turn LED on:
digitalWrite(ledPin, HIGH);
{
for (posr = 180; posr >= 90; posr -= 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
turn.write(posr); // tell servo to go to position in variable ‘pos’
delay(1); // waits 15ms for the servo to reach the position
}
for (posg = 90; posr <= 180; posr += 1) { // goes from 180 degrees to 0 degrees
turn.write(posr); // tell servo to go to position in variable ‘pos’
delay(5); // waits 15ms for the servo to reach the position
}
} }
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

I can’t seem to figure out why I am getting the error message else with no previous if

Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called “copy error message”. Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

; else if (buttonStatered == HIGH); {

Do else if statements start and end with a semi colon? I don’t think so.

            delay(5); // waits 15ms for the servo to reach the position
         }
      }
   }
   buttonStatered = digitalRead(buttonred);  // ******* this interrupts the if-else if-else chain
   // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
   ; else if (buttonStatered == HIGH);  // ****** extra semi colons.
   {

You could move the digitalRead into the if else and eliminate the offending line.
else if (digitalRead(buttonred) == HIGH)

I’m a beginner and not familiar with the code yet so you may have to bear with me. I moved the digital read like you suggested, but I still get the error message. The exact error message is

Arduino: 1.8.5 (Windows 7), Board: “Arduino/Genuino Uno”

Build options changed, rebuilding all
H:\Documents\Arduino\slingshotver3\slingshotver3.ino: In function ‘void loop()’:

slingshotver3:91: error: ‘else’ without a previous ‘if’

else if (digitalRead(buttonred)== HIGH); {

^

slingshotver3:105: error: ‘else’ without a previous ‘if’

else {

^

exit status 1
‘else’ without a previous ‘if’

Is there anything else I can do?

When you make changes to your posted code, please post the new version so that we can keep up. Make sure to post with code tags as the forum guidelines instruct.

else if (digitalRead(buttonred)== HIGH);

Statements like if, else if and else DO NOT end with semi colons.

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