// // LED-based proximity switch demonstration. // White LED is connected to P1_7 // Red LED is connected to P1_4 // Both of these LEDs are external to the Launchpad (not the built-in LEDs). // They are arranged so that they are right next to one another. // This lets the white LED illuminate anything in front of it. // The red LED is used as a sensor, to detect anything close enough to the white LED to be brightly lit. // // When you place your finger or a white card in front of the two LEDs, the program turns on the on-board green LED, // and sends text to the development computer telling what the light level was. // unsigned long int no_signal = 0; void setup( void ) { analogReference( INTERNAL1V5 ); pinMode( A4, INPUT ); pinMode( P1_7, OUTPUT ); pinMode( GREEN_LED, OUTPUT ); digitalWrite( P1_7, HIGH ); Serial.begin( 9600 ); for( int x = 0; x < 100; x++ ) { delay( 10 ); int val = analogRead( A4 ); if( val > no_signal ) no_signal = val; } Serial.println( no_signal ); } void loop( void ) { unsigned int level = (100 * analogRead( A4 ) ) / no_signal; Serial.print( level ); if( level < 50 ) { digitalWrite( GREEN_LED, HIGH ); Serial.println( "% ON" ); } else { digitalWrite( GREEN_LED, LOW ); Serial.println( "% OFF" ); } }