expected unqualified-id before '{' token

so this is my code and I'm very new to Arduino so could someone please help me with this error. I keep getting it no matter what I change.

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(12, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
int ldr = 1023 - analogRead(A0);
Serial.println(ldr);
delay(500);
if(ldr > 1022);
digitalWrite(12, LOW);
if else(ldr < 1022);
digitalWrite(12, HIGH);
}

I don't see the error right away. But for better help, have a look at the stickey How to get the most out of this forum. For example about posting code. And the fact your error does in fact contain more info then you showed.

 if else(ldr < 1022);

Take good look at this line. What is the if doing in that position ?

You also need to remove the semicolon from the if and else lines as otherwise it is the only code dependant on the outcome of the test

On the whole it is better to enclose dependant code in pairs of { } even if it is only one statement

Please post your code within </> code-tags
and use ctrl-T (auto-format) before.

if else(ldr < 1022);

This is where the code lights up, so here is where to have a closer look, it should be 'else if' mind you once that is changed and you compile i am pretty sure your code won't work as expected, due to the semicolons at the end of the if statements.

if(ldr > 1022);  // this ';' shouldn't be there
 digitalWrite(12, LOW);

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