The software is written in assembler. The LEDs are controlled by five 100 Hz interrupt driven software PWMs. LED brightness is not perceived linearly. A lookup table helps to compensate for that.



The servo is controlled by a 50 Hz software PWM. Servos from different manufacturers have slightly different PWM pulse width min/max values. It is important that these are adjusted accordingly. Otherwise the servo may get damaged if it is driven repeatedly over it's limits.



The piezo requires a frequency in the kHz range. The AVRs hardware PWM is used to drive it.



One of the ADC channels is used to read the output from the microphone amplifier. When a certain threshold is reached, the head movement is triggered. Different microphones have an impact on the output of the amplifier. The trigger level can be adjusted in software. Mine is set so that it ignores normal conversations, but get's triggered when things get excited. It's also a good sneeze detector. :-)



The ADC is read from an ADC interrupt. If this interrupt handler is active while the PWM timer interrupt fires, it can introduce a slight jitter to the PWM. This jitter may cause the servo to move a little, which produces enough noise to trigger sound detector. There are two possible solutions to this problem. One would be to not use an interrupt for the ADC and read it in a wait loop instead. This can be a bit tricky and ugly. Instead the current solution is to simply disable the servo PWM pulse while the head is not moving. This will turn of the motor, save energy and PWM jitter will not cause any problems. In general the sound detection is only active while the Android is quiet, as any Morse code beeps or head movement would also trigger it.



One of eight Morse code messages is played at random. Wikipedia has a great Morse Code article. The Morse Code alphabet is encoded in a table which translates ASCII characters to the corresponding Morse signals using a two bit encoding. The actual Morse Code messages can therefor be stored in easy to read and space efficient ASCII strings.



The source code can be compiled and programmed into the MCU wit the free AVR Studio IDE. There are open source alternatives as well.



