"Else without a previous if" error message, no idea what to do!

I am quite new at Arduino circuits. I understand circuits, but I am totally new at programming. I received an Arduino Uno Rev3 starter kit for Christmas and immediately began assembling the circuits and using the pre-made example codes to make them function. I got the hang of that, and decided to start building some circuits of my own and programming. I wanted to give else…if programs a try so I made a simple circuit that (hypothetically) uses a photoresistor input to control an LED. This is the code I constructed:

"int lightPin = A0;
int ledPin = 9;

void setup() {
pinMode(A0, INPUT);
pinMode(9, OUTPUT);
}

void loop (){
if
(digitalRead (lightPin <= 300));
{
digitalWrite(ledPin, HIGH);
}
else
{
(digitalWrite(ledPin, LOW));}
}"

My IDE is telling me that i have an “else without a previous if”, but it seems to me as if my previous if is present. Not sure what to do. Help is much appreciated!

No semicolon at the end of if conditions.

if
(digitalRead (lightPin <= 300));
{
  digitalWrite(ledPin, HIGH);
}
if(digitalRead (lightPin <= 300))
{
  digitalWrite(ledPin, HIGH);
}
1 Like

Delta_G:
No semicolon at the end of if conditions.

if

(digitalRead (lightPin <= 300));
{
 digitalWrite(ledPin, HIGH);
}






if(digitalRead (lightPin <= 300))
{
 digitalWrite(ledPin, HIGH);
}

Well… That got the code to verify and upload, but the light seems to be irresponsive. It is on, regardless of the light level picked up by the photoresistor.

if( analogRead (lightPin)<=300 )

Jiggy-Ninja:

if( analogRead (lightPin)<=300 )

Thank you! This made it work! I figured it was just a small technicality in the syntax!

Using the entirely wrong function doesn’t really qualify as a “small technicality”.

My bad. After I posted that, I noticed the digital part and changed it. I had the parenthesis in the wrong place. But thanks!