Reverse biasing a LED makes the LED act as a capacitor, holding a small charge. However, how does one find out how long a LED should be reverse biased to ensure it is fully charged before detecting the time it takes to discharge?
void loop() //LED anode is pin 4, cathode is pin 5
{
DDRC = 0xFF; //All ports set to output
PORTC = 0b00100000; //HIGH cathode LOW anode, starts reverse bias
delay(1); //charges LED for 1 millisecond
PORTC = 0b00000000; //LOW cathode and anode, ends reverse bias
DDRC = 0b11011111; //turns cathode pin from OUTPUT to INPUT
int counter = 0;
while(analogRead(5) > 500) //counts the time it takes to reach roughly 2.5 volts, analog voltage is read from the cathode pin.
{
counter++;
}
Serial.println(counter); //prints out how many ticks it took
counter = 0;
}