Error: Expected unqualified-id before 'else'

Hi,

Im a beginner and just started learning how to use my Arduino UNO.
Here is my first code:

int switchState = 0;

void setup() {
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(2,INPUT);
}

void loop() {
switchState = digitalRead(2)
// digitalRead checks for voltage

;if(switchState == LOW);
// two equal signs compare two things
digitalWrite(3, LOW); //green LED
digitalWrite(4, LOW); //red LED
digitalWrite(5, HIGH);// red LED
}

else {
digitalWrite(3,LOW);
digitalWrite(4,LOW) ;
digitalWrite(5,HIGH);

delay(250);
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
delay(250);

}


Im not sure how to fix it. When I try to upload it to the UNO, it says this:
Error: Expected unqualified-id before 'else'

Can someone please help me?

This line looks very strange to me:

;if(switchState == LOW);

You have a few problems with semicolons and curly braces.

Have a look at these:

How to use this forum - please read.

Thanks for your responses!

I'm in exactly the same situation, working through the same project and am having the same problems!

I've tried everything suggested in this thread but nothing has helped, my code looks just like JVilkas.

Did you manage to sort it?

bob21:
I've tried everything suggested in this thread but nothing has helped, my code looks just like JVilkas.

I suspect there is a difference - perhaps a very small one.

Post your code!
And please use the code button </> so your code looks like this and is easy to copy to a text editor

...R

The problems were pointed out in the previous replies.
if statements do do end with a semicolon
Code lines do not begin with a semicolon

Did you fix all of them ?

Please post the fixed code here to get more help.

Where did you get the code ?

switchState = digitalRead(2)
Change to:
switchState = digitalRead(2);
And:
;if(switchState == LOW);
Get rid of both ;

hey - sorry
here's my code!

<int switchState = 0;

void setup() {
pinMode(3, OUTPUT) ;
pinMode(4, OUTPUT) ;
pinMode(5, OUTPUT) ;
pinMode(2, INPUT) ;
}

void loop() {
switchState = digitalRead(2);
if (switchState == LOW)

digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);

}

else {
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
}

the code is from the book that came with the arduino!

if (switchState == LOW)
Change to:
if (switchState == LOW) {

legend, sorted!! Thanks!!!

i have the same problem' this is my code [in quotations] please help me

"// Keypad - Version: Latest
#include <Key.h>
#include <Keypad.h>
#include <Keypad.h>

char* password = "159"; // change the password here, just pick any 3 numbers
int position = 0;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};

byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int RedpinLock = 12;
int GreenpinUnlock = 13;

void setup()
{
pinMode(RedpinLock, OUTPUT);
pinMode(GreenpinUnlock, OUTPUT);
}

void loop()
{
char key = keypad.getKey();
if (key == '*' || key == '#')

if (key == password[position])
digitalWrite(RedpinLock, HIGH);
digitalWrite(GreenpinUnlock, LOW);
}
else
{
digitalWrite(RedpinLock, LOW);
digitalWrite(GreenpinUnlock, HIGH);
}
}
"

if (key == password[position])see reply #9

And please remember to use code . . .oh god, what's the point?

That message means you used an else statement that was not immediately preceded by an if statement, usually due to messed up/missing curly braces. In both of your cases, you forgot an opening brace after the if

SO I AM COPYING THIS CODE FROM THE BOOK A I KEEP GETTING ERROR MESSAGE CAN SOMEONE PLEASE HELP ME DEBUG? THANK YOU

int switchState = 0;
void setup () {
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(2, INPUT);
}
void loop(){
switch = digitalRead(2);
if (switchState == LOW)
// the button is not pressed
digitalWrite(3, HIGH); // green LED
digitalWrite(4, LOW); // red LED
digitalWrite(5, LOW); // red LED
}
else { // the button is pressed
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
delay(250) // wait or the quarter second
// toggle the LEDs
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(250); // wait for the quarter second
}
} // go back to the beginning of loop

@javierj91

Please do not hijack other peoples thread, start your own.

Don't yell at us.

Always use code tags (click the </> button in the posting menu) when attaching your sketch.

Every { needs a friend }

'switch' is not defined as a variable.

Each code line needs a ; at the end of it.

Wait, so is there a book lots of people are getting with a missing openbrace in an example?

@larryd- I wouldn't call that hijacking, seeing as he seems to be working with near identical code (seems like many people in this thread are looking at same book)

this was what worked for me, you need to add another '}' at the end of your program but i sill have problems uploading to my arduino

int switchState = 0;
void setup() {
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(2,INPUT);
}

void loop() {
switchState = digitalRead(2);
// this is a comment
if (switchState == LOW){
// the button is not pressed

digitalWrite(3, HIGH); //green LED
digitalWrite(4, LOW); //red LED
digitalWrite(5, LOW); //red LED
}

else { // the button is pressed
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);

delay(250); //wait for a quarter of a second
//toggle the LED
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(250); //wait for a quarter second
}}

Alusine:
this was what worked for me, you need to add another '}' at the end of your program but i sill have problems uploading to my arduino
...
...

There is a different section for that; either post in Installation & Troubleshooting or Avrdude, stk500, Bootloader issues.

/*

  • made by technvation
  • uses open source blynk app to send data over the interent
  • This example code is in public domain.
  • feel free to modify, change, improve
    */
    #define BLYNK_PRINT Serial
    #include <Ultrasonic.h>

#include <SPI.h>
#include <WiFi101.h>
#include <BlynkSimpleMKR1000.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "12f7a8cb125e4e688bc22cedacd0ced9";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "ADIT's iPhone";
char pass[] = "A93rtur3";

BlynkTimer timer;
int distance = 0;
int thresh[3] = {25, 50, 75};
Ultrasonic ultrasonic(12, 13);

WidgetLED green(V1);
WidgetLED orange(V2);
WidgetLED red(V3);

void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
}

void loop()
{
distance = ultrasonic.distanceRead();
Serial.println(distance);
Blynk.run();

if(distance<thresh[0]){
green.on();
}
else if(distance<thresh[1])
green.on();
orange.on();
}
else if(distance<thresh[2])
green.on();
orange.on();
red.on();
}
else{
green.off();
orange.off();
red.off();
delay(100);
}