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...