Error " 'else' without a previous 'if" trying to compile

Noob question: I got an error trying to compile this. It is from the Arduino site.

Arduino: 1.6.5 (Windows 8.1), Board: “Arduino/Genuino Uno”

sketch_nov02a.ino: In function ‘void loop()’:
sketch_nov02a:16: error: ‘else’ without a previous ‘if’
sketch_nov02b.ino: At global scope:
sketch_nov02b:3: error: redefinition of ‘int val’
sketch_nov02a:3: error: ‘int val’ previously defined here
sketch_nov02b.ino: In function ‘void setup()’:
sketch_nov02b:5: error: redefinition of ‘void setup()’
sketch_nov02a:6: error: ‘void setup()’ previously defined here
sketch_nov02b.ino: In function ‘void loop()’:
sketch_nov02b:10: error: redefinition of ‘void loop()’
sketch_nov02a:12: error: ‘void loop()’ previously defined here
‘else’ without a previous ‘if’

This report would have more information with
“Show verbose output during compilation”
enabled in File > Preferences.

int ledPin = 13; // choose the pin for the LED
int inPin = 7; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input

void loop() {
val = digitalRead(inPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, LOW); // turn LED OFF
} else {
digitalWrite(ledPin, HIGH); // turn LED ON


You have similar code in more than one tab of the IDE

When I compile the code you actually (but incorrectly) posted, I get:

Binary sketch size: 1,078 bytes (of a 30,720 byte maximum)

UKHeliBob: You have similar code in more than one tab of the IDE

Assuming the code posted is a full code in x.ino, can you elaborate on this? I see two codes / sketches in error printout. ( xa xb )

It appears it is not full / real code , something ( #include xx?) may be missing.

I am still betting on code in 2 tabs, each with a setup() and loop() function and variables with the same name. From the error messages the files would appear to be called nov02a and nov02b

PaulS: When I compile the code you actually (but incorrectly) posted, I get:

I deleted the other tab and shortened up the error msgs - now just the if else error. But you are saying it compiles on your IED?? Why?

sketch_nov03a.ino: In function 'void loop()': sketch_nov03a:16: error: 'else' without a previous 'if' 'else' without a previous 'if'


That is a different program than the ones that caused the original errors. Please post the program as it is now, even if you think that it is the same as before.

const int GREENLED = 13; const int PUSHBUTTON = 7; int val = 0; //mem loc of pushbutton

void setup(){

pinMode(GREENLED, OUTPUT); // put your setup code here, to run once: SETUP PINSTATES pinMode(PUSHBUTTON, INPUT); }

void loop(){ val = digitalRead(PUSHBUTTON); // put your main code here, to run repeatedly: read and store value in 0 if (val == HIGH); { digitalWrite(GREENLED,HIGH); } else { digitalWrite(GREENLED,LOW); } }

if statements shouldn't have semicolons on them.

Oh geez, I have a lot to learn. Thanks - that worked. It's all in the details. :confused: