LDR Sensor

Completely new to programming, I thought to write an LDR sensor code myself and used the if else command. It showed quite a lot of problems and it would help if you would correct me please. thankyou. :slight_smile:

int ledPin = 13;
int sensorPin = 0;
int val = 0;
void setup () {
  pinMode (ledPin, OUTPUT);
  pinMode (sensorPin, INPUT);
void loop () {
  If (sensorPin == HIGH);
  (ledPin, HIGH);
  Else (ledPin, LOW);
}
const int ledPin = 13;
const int sensorPin = 0;
void setup () 
{
  pinMode (ledPin, OUTPUT);
  pinMode (sensorPin, INPUT);
}
void loop () 
{
  int val = digitalRead (sensorPin);
  digitalWrite(ledPin, val);
}

uncompiled, untested

Thankyou 8)

If (sensorPin == HIGH);
(ledPin, HIGH);

Lots of things:

  1. C is case-sensitive. "if" is NOT the same as "If"
  2. You gave "sensorPin" the value zero - it is never going to be the same as HIGH (aka 1)
  3. Never put a semicolon on an "if" statement like this. It will compile, but it won't do what you want.
  4. The second line may well compile (I can't be bothered to check), but evaluating two unrelated expressions and discarding the result will not turn a LED on.