I am making an interactive LED circuit that takes a calibration reading at the beginning, then checks against that during the loop. I am using an infrared emitter, infrared photodiode, and an led.. ( with obvious other components like resistors, capacitors, etc.). What I want my program to do better is led fading. When my hand is as far away as the sensor will allow as a change, the led should be fairly dim, growing brighter and brighter as my hand gets closer to the receiver. My code can sort of do this, but instead of being smooth, the led pulses when my hand is anywhere but as close as possible to the receiver. I need it to be a very smooth transition as my hand gets closer and closer... brighter and brighter. Here is the code to test this...
int threshold;
void setup()
{
pinMode(10,OUTPUT);
pinMode(12,OUTPUT);
threshold = analogRead(A5);
delay(100);
}
void loop()
{
int Receiver = analogRead(A5);
digitalWrite(12,HIGH);
if( Receiver < ( threshold - 3 ))
analogWrite(10,1);
if( Receiver < ( threshold - 3)) && ( Receiver < ( threshold - 4)) // checks for when the Receiver is less than ( receiver - 4)
analogWrite(10,5);
// ... and so on... all the way to about (threshold - 45) .. so I skip Receiver val every 1, but the analogWrite val every 4, because of scaling issues
else
analogWrite(10,0);
}
any ideas on how to make it smoooooooth?