While the name earduino implies that this project is based on an Arduino, the device actually uses a Teensy 3.1 to think. An Arduino Uno was used for prototyping, but was swapped with a Teensy so that it could fit in a pocket-sized package. Using Teensyduino, an add-on for the regular Arduino IDE, it's easy to compile and run an Arduino sketch on a Teensy.
The intensity of each LED and their choreographed pulsing and blinking patterns are controlled using PWM. The waves that drive these PWM values are sine and step functions that can be seen at the end of the following code. These functions take the oscillation period and wave amplitude, as well as phase for inputs. The outputs vales of these functions are mapped to the LED PWM input values, thus controlling the brightness of each LED. The phase argument in each function allows similar waves to be generated out of phase to create patterns like alternating or consecutive pulses and flashes.
Code Overview:
As can be seen in the code below, the first step is reading the signal produced by the microphone using the Teensy's analog to digital converter (ADC). This data is smoothed by taking an average of the most recent 4000 data points. This large averaging window prevents the earduino from reacting to quickly to spikes in the ambient volume. Instead, the average ambient volume over the last second or two affects the output of the earduino.
This average value is then compared to a couple of values which represent the volume thresholds at which "safe listening" becomes "too loud" and when "too loud" becomes dangerous.
In "safe listening" mode, or mode 1, only the blue LED is lit. The intensity or the sensed sound is mapped to the PWM duty cycle controlling the LED. The steadily lit LED therefore becomes brighter as the volume gets louder, until the threshold at the upper limit of safe listening is reached. In mode 2, alternating blue and yellow pulsing lights warn the user that they should either turn down the volume or use hearing protection. In this mode, the intensity of the sound drives both the frequency and amplitude, or pulse speed and brightness, of the sine waves displayed by the two LEDs. In mode three, all three lights, including the red light, are lit consecutively and repeatedly. Here, instead of a sine wave, a square wave is used to convey more urgency with more abrupt flashing. Again, the volume is mapped to the blinking frequency and brightness of the lights, as well as the frequency and intensity of pulsing vibrations.