Steve Steve - 3 years ago 215
C++ Question

Arduino Error 1 expected unqualified-id before 'else'

Arduino Error: Where have i gone wrong in this simple and extremely small amount of code to receive the above error message? I cannot figure it out.

int ledPin = A0;
int bumpPin = A1;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(bumpPin, INPUT);
}

void loop() {
digitalRead(bumpPin);
if (bumpPin == HIGH);
digitalWrite(ledPin,HIGH);
}else{
digitalWrite(ledPin,LOW);
}

Answer Source

Definitely read a tutorial on C/C++. What you have here is a basic syntax error. An if/else statements uses the following syntax:

if (condition) {
    // Do stuff here
} else {
    // Do other stuff here
}

You have a semi colon after your condition in your if statement. Change that to a curly brace and you’re good! So this:

if (bumpPin == HIGH);

Should be this:

if (bumpPin == HIGH) {

Here is an online C++ tutorial.

Here is that tutorial’s section on if statements.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download