Led light sensor help...

Hi all. So I have been trying to find out what is wrong with my code or wiring but I am not able to find the problem. I would like to let you know that this is one of my first arduino projects, so please be easy on me. Can anyone tell me what is wrong (a hint is preferred rather than the answer)? I have searched google and have also checked the arduino forums, I thought these sites were helpful:

http://www.thebox.myzen.co.uk/Workshop/LED_Sensing.html

http://arduino.cc/forum/index.php/topic,9474.0.html

I do not want to copy/edit someone's code and have it work. I would like to know what is wrong with my code.
You may want to know how I have my hardware connected. I have a LED and 200Ohm resistor connected in series acting as a sensor and also another 200Ohm resistor and a LED that acts as a simple light source. One of the LEDs acts as the sensor and the other will turn on if dark and off if ambient light exists. The sensor LED's anode is connected directly to the arduino's digital pin 8 and the sensor LED's cathode is connected to a 200Ohm resistor that connects to the arduino's analog pin A1. The light source anode connects to the arduin's digital pin 2 and it's cathode connects to a resistor which connects to the arduin's ground.

Here is my code:

const int sensyAn = 8;
const int sensyCa = A1;
const int led = 2;
int refVal = 0;
int finVal = 0;

void setup(){
  Serial.begin(9600);
  pinMode(led, OUTPUT);
}

void loop(){
  pinMode(sensyAn, OUTPUT);
  pinMode(sensyCa, OUTPUT);

  /*Free ledCAP by forward biasing*/
  digitalWrite(sensyAn, HIGH);
  digitalWrite(sensyCa, LOW);
  //delay(10);
  
  /*Charge ledCAP by reverse biasing*/
  digitalWrite(sensyAn, LOW);
  digitalWrite(sensyCa, HIGH);
  delay(10);
  
  /*Take initial reference reading 
  and send to cpu*/
  pinMode(sensyCa, INPUT);
  refVal = analogRead(sensyCa);
  Serial.print("Reference value: ");
  Serial.println(refVal);
  
  /*Read ledVol after some time*/
  pinMode(sensyCa, INPUT);
  delay(40);
  finVal = analogRead(sensyCa);
  Serial.print("Final reading: ");
  Serial.println(finVal);
  Serial.print("diff: ");
  Serial.println(refVal-finVal);

  /*Light LED accordingly*/
  if( (refVal-finVal) >= 300){ //light exists
    digitalWrite(led, LOW);
    Serial.println("****");
  }else{ //no light
    Serial.println("----");
    digitalWrite(led, HIGH);  
  }
  Serial.println("");
}

I would also like to point out that after looking at the Serial monitor, I have come to realize that the "Final" reading is fluctuating almost like a sin wave, for example: 1023, 949, 848, 416, 0, 0, 17, 91, 191, 644, 1023... and the cycle repeats.

Thank you...

Not all leds are good for 'light sensing'.
The led will sense light with the same or shorter wavelength. So you need a red led. But it could be 3mm or 5mm or new or old. I tested 20 or 30 leds and one was 100 times better than the other.

I you write a 'HIGH', and after that make it an input, this 'HIGH' sets the internal pull-up resistor. So you have to write a 'LOW' after you switched it to input.

Your code is roughly okay. But the 40ms is a fixed time. The reverse charge could need 1ms(or less) ... 10000ms to be removed.

You have commented out the delay after the normal forward current is set, but that delay must be there.

The fluctuating could be electric noise. The led has to be very near the AVR chip (the microcontroller on the Arduino board) to get a good reading. The led in reverse is very high impedance, so it will pick up electric noise.

Thank you for your reply and information.

The led will sense light with the same or shorter wavelength. So you need a red led.

I am familiar with this as I have a red led.

Your code is roughly okay. But the 40ms is a fixed time. The reverse charge could need 1ms(or less) ... 10000ms to be removed.

I don't quite understand what you mean here, do you mind expanding your explanation?

I you write a 'HIGH', and after that make it an input, this 'HIGH' sets the internal pull-up resistor. So you have to write a 'LOW' after you switched it to input.

I have also read a bit on the arduino pull-up resistor but I don't know why I need to disable the pull up resistor in this case. I will think about it today. Why is it that setting the pin to "HIGH" before making it an input sets the pull-up resistor? Why does "LOW" turn off the pull up resistor?

I will try to edit my code according to what you have stated while I await a reply. Thanks again.

Read about the pull-up resistor : digitalWrite() - Arduino Reference
That is how the microcontroller is made.

A negative charge is loaded into the capacitance of the led.
The solar effect of the led generates a very small positive current.
This current will remove the negative charge.
So the time that negative charge is removed depends on the amount of light. And that is how the light intensity is measured.
If it is dark, this could be very long. One second before half of the charge is removed is possible.
You wait 40ms, but with very bright lights, the charge could be gone completely, and with very little light the charge could be still the same. But everything depends on the led. Do you have a few leds to try ?

I used this sequence:

set anode to '0'.
cathode:
pinMode OUTPUT
digitalWrite HIGH
delay 5 ms
pinMode INPUT
digitWrite LOW  // remove pull-up
delay 4 to 1200 ms in a loop while reading the cathode

So I didn't free the charge from the led with a forward current. In my case that was not needed. And I used a digital input.

Interesting... I uncommented my delay(10) and increased the delay(40) to delay(1000) and it worked perfectly. However setting and disabling the pull-up resistor made no observable difference, do you know why that may be (i will double check my result now)? I will read the link you sent now also. Thank you so much, you have been a great help.

And no, I only have a bunch of the same type of LED. But after your correction this LED seems to work fine. XD
Also, I now I realize that if my hand gets close to the arduino pins, it triggers the LED to turn on (with pull up resistor disabled or enabled..), would you know why that is?

In my case I used a digital input. So I check the digital input during that 4 ... 1200 ms.
In your case, you use the analog value. So 1000 ms is (far) too long I think. But that all depends on the led and the amount of light.

Since this is done with the capacitance of the led and the circuit is high impedance, every little electric noise will influence the reading. Your hands will probably introduce noise from the mains 50Hz or 60Hz into the circuit. The longer the delay, the more this is a problem.

Hmmm, that makes sense. I will have to find the correct delay somehow. I will play with it and figure it out. Thank you once again for all your help. I will begin working on another project soon (a heat sensor, using a diode XD).

Two last question though (if you have the time).

What would be the advantage of using a digital input instead of an analog input?

You say:

So I check the digital input during that 4 ... 1200 ms.

I don't understand the sentence (sorry :frowning: ). Specifically what does "4 ... 1200ms" mean?

There is no advantage of a digital input over an analog input.
But it is possible, and I used my analog inputs for other things.

To know the time, an interrupt intput could be used or the pulseIn function : http://arduino.cc/en/Reference/pulseIn
But I did a loop with to test if the input changed.
It turned out that my led needed somewhere between 4ms (very light) and 1200ms (very dark) to get rid of the charge.

Thank you so much for your help. You have answered all my questions and made my little project work..