Step 3: Testing the Sketch and Design Notes

In this step I'll talk about some of the trials and tribulations that I encountered on the way, and how I worked through them, in the hope that it will be useful.  Fell free to skip to the next step if Science Content isn't your thing :-)

The first step was deciding whether to use capacitive sensing or resistive sensing.  Resistive sensing is connecting the sensor through a resistor to one of the analog pins and doing analogRead and comparing against a threshold.  This is simplest to implement, but takes lots of calibration.

The theory of capacitive sensing is that when reverse biased (- to the + lead and vice versa), an LED will not allow current to flow, but electrons will collect on one side and leave the other side, effectively charging a capacitor.  Light falling on the LED at the frequency it normally emits will actually cause a smal current to flow, which discharges this capacitor.  

So if we charge the LED 'capacitor' and count how long it takes to discharge through a resistor, we get a rough idea of how much light is falling on the LED. This actually worked out to be more reliable across different devices, and even works for phototransistors!  Since we're not doing a precise lumen measurement, and the laser pointer should appear a lot brighter than ambient, we just look for a thresholded discharge time.

The other important part of this adventure is debugging.  For those familiar with programming non-embedded systems, a popular method is to add print statements at critical points in the code.  This also applies to embedded systems, but when every microsecond counts, the amount of time to Serial.write("x is "); Serial.writeln(x); is actually quite significant, and you may miss a lot of events in the process.  So remember to always put your print statements outside of critical loops, or any time you expect an event.  Sometimes blinking an LED is enough to let you know you got to a certain point in the code.