Just did my first little project with my new Arduino. All my electronics stuff is stored away after I moved this summer, so all I had to play with were some LEDs. So, I made a proximity-toggleswitch using a LED as a sensor. Pretty simple, one LED is connected between digital out 2 and 3 (long leg on 2), and another between 13 and ground for indication. When the switch is toggled on or off the Arduino sends a 1 or 0 (respectively) to my computer running a botched-up 2 minute Visual Basic program.
/* LED sensor -- photodiode.
int anodePin = 2;
int cathodePin = 3;
unsigned long lastupdate = millis();
//deactivates pullup resistors?
_SFR_IO8(0x35) |= 4;
_SFR_IO8(0x35) |= (1<<4);
pinMode(anodePin, OUTPUT); // sets the digital pin as output
pinMode(cathodePin, OUTPUT); // sets the digital pin as output
//switch potentials -- charge LED to -5V
//measure time for potential to equalize (for cathode to be LOW)
//measure time it takes for cathodePin to go to zero
//this value probably depends on the chip clock or something
value = 0;
while(digitalRead(cathodePin) != 0)
//check if the change in measured value is greater than a certain threshold, and if the last update was more than a second ago
if((prev - value < -1000 || prev - value > 1000) && millis() > lastupdate + 1000)
lastupdate = millis();
if(digitalRead(13) == LOW)
prev = value;
I know it ain't much to brag about, but it's kinda cool as the program sort'a autocalibrates, so it will work reliably both in light and dark conditions.