enum { LED_ANODE=PIN3, LED_CATHODE=PIN2 };
void
charge_led_capacitance( void )
{
pinMode( LED_ANODE, OUTPUT ); // Set both pins
pinMode( LED_CATHODE, OUTPUT ); // to be outputs
digitalWrite( LED_ANODE, LOW ); // Set one low
digitalWrite( LED_CATHODE, HIGH ); // and the other high, so LED is reverse-biased (no light)
}
void
set_input_mode( void )
{
pinMode( LED_CATHODE, INPUT ); // Set the pin to read mode
digitalWrite( LED_CATHODE, LOW ); // Turn off the internal pull-up resistor on the pin
}
unsigned
read_light_level( void )
{
charge_led_capacitance();
set_input_mode();
unsigned level = 0;
for( ; level < 20000; level++ )
if( digitalRead( LED_CATHODE ) == 0 )
break;
return level;
}
void
setup( void )
{
Serial.begin( 115200 );
}
void
loop( void )
{
Serial.print( "Light level is " );
Serial.println( read_light_level() );
delay( 100 );
}