LDR as a sensor

hi again. got my LED to work, but now im struggling with the code for the an LDR sensor.

I used the blinking code to mimic what i wanted. so when the LDR is sensitive to light it reflects on the the LED connected to PIN13, any chance anyone can help me source out my code that is wrong?

/*

  • Blink
  • The basic Arduino example. Turns on an LED on for one second,
  • then off for one second, and so on... We use pin 13 because,
  • depending on your Arduino board, it has either a built-in LED
  • or a built-in resistor so that you need only an LED.
  • http://www.arduino.cc/en/Tutorial/Blink
    */

int ledPin = 13; // LED connected to digital pin 13
int LDR = 0; //LDR input
int val = 0; //setup inital stored value

void setup() // run once, when the sketch starts
{
pinMode(LDR, INPUT);
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}

void loop() // run over and over again
{
val = analogRead(LDR);
digitalWrite(ledPin, HIGH); // sets the LED on
delay(val); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(val); // waits for a second
}

i feel it may be the 'delay' part of the code. what i'm actually trying to do is when i cover the LDR the LED goes OFF , but when there is light on the LDR then the LED turns on. anyone know a website or can help me in my quest. thanks

sorry should've explained the current result that i'm getting.

if the LDR at present is receiving light then the LED blinks
but when i cover the LDR, the LED remains ON

thanks in advance

Well, as writen your code reads the LDR and blinks at a rate determined by the light level. But it blinks always at a 50 percent duty cycle just faster or slower.

Terri, I think your code is working. When the LDR is completely covered, val is 0. So you are turning the LED on, waiting for 0ms, turning it back off, and waiting for 0ms. In other words, you are turning it on and off so quickly that it just looks perpetually on (if slightly dimmer).

You can prove this by changing the delays to something like "delay(val + 80)". When you cover the LDR, the blinking rate will increase, but not so much as to be invisible.

Mikal

yep, thats what its doing. when i increase the delay with the '+' value it does change. but im not sure what the correct code would be. is there something i could replace the 'delay' line with. i would like to avoid if else then statement, as im still relatively new to this. thanks :stuck_out_tongue:

nevermind, change the script to an if else statement. AND IT WORKS!!!

thanks for all your help. wouldn't have gotten here without advice thanks
;D