#include /** * File: beatDuino.ino * Description: Make your Engduino show equaliser style light show to the * beat of your music. Remember you will need the microphone * circuit from the Engduino.org website for it to work! * Author: Andreas Frangopoulos with enormous thanks to * Dimitar Kovachev for his excellent tips for ARduino audio detection. * Arduino forum member Arduinoid for his great beat detection self adjustment idea. */ int tic = 0; int Bass = 0; int Bdelay = 0; int Bmin = 0; int Tmin = 0; int Bmax = 0; int Tmax = 0; int B = 0; int T = 0; int Brange = 0; int Trange = 0; int Hmic = 0; int Lmic = 0; //Set up Engduino LEDs and the analogue reference to 3.3V. This is the default behaviour but it doens't hurt. void setup() { analogReference(INTERNAL); //We have attached our output cable to analogue input number two. Change this if you went for a different pin. pinMode(A2, INPUT); EngduinoLEDs.begin(); Serial.begin(9600); } void loop() { //Loop counter, and auto correcting /** * Instead of just having the device light up if it hears a loud noise we want it to be able to increase the sensitivity for * music at low volumes. We use a simple counter, tic and Bdelay to create some delay between our instructions. Every 100 times the program * cycles we increase the minimum voltage reading that sets the baseline measurement. This stops ambient noise from triggering the lights and * adjusts the sensitivity down if the music is very loud. */ ++tic; ++Bdelay; if (tic > 1000) { //determines rate of correction. 1000 works well but if you want it to be longer between check so you don't //get any random flashes, increase this value ++Bmin; --Bmax; tic = 0; //reset our tic timer } //Order of calculations is important!! //We are getting the smaller of the detected sound value and comparing it to our current mimimum. If the bass is softer that becomes the new minimum. // Bmin = min(Bmin, Bass); //We are now taking a reading as a difference of it from a minumum. When we have total silence from our microphone it will register a value of //half of the total voltage it can detect. This gives us the ability to get the up and down vibrations of the sound wave. Bass = analogRead(A2) - Bmin; // We are checking that the maximum sound level we detect is remembered as the maximum. Bmax = max(Bmax, Bass); //This bit of code prevents a sudden loud noise from messing up our maximum. It listens for the current ambient noise every 10 of the //tic counts. if (Hmic > 10000 || Lmic > 10000) { Bmax = (analogRead(A2) / 2) + 10; Bmin = (analogRead(A2) / 2) - 5; Hmic = 0; Lmic = 0; } //This makes up for small changes in microphone output at low volumes. We are comparing it to a range of values determined from these //and previous sounds in the near past so that different volumes will produce similar results. Brange = Bmax - Bmin; B = Bass - Bmin; //Series of if statements to control actions. Here we compare our current reading with the calculated range above and then //show more lights if that value is a greater portion of the entire range. // if (B > 0.55 * Brange) { EngduinoLEDs.setLED(1,RED); EngduinoLEDs.setLED(14,RED); if (B > 0.65 * Brange) { EngduinoLEDs.setLED(2,GREEN); EngduinoLEDs.setLED(13,GREEN); if (B > 0.75 * Brange) { EngduinoLEDs.setLED(3,BLUE); //EngduinoLEDs.setLED(12,BLUE); if (B > 0.85 * Brange) { EngduinoLEDs.setLED(4,YELLOW); EngduinoLEDs.setLED(12,YELLOW); if (B > 0.90 * Brange) { EngduinoLEDs.setLED(5,CYAN); EngduinoLEDs.setLED(6,CYAN); EngduinoLEDs.setLED(11,CYAN); if (B > 0.95 * Brange) { EngduinoLEDs.setLED(6,MAGENTA); EngduinoLEDs.setLED(10,MAGENTA); if (B > 0.99 * Brange) { EngduinoLEDs.setLED(7,WHITE); EngduinoLEDs.setLED(9,WHITE); } } } } } } } if (B < 0.5 * Brange) { Hmic = 0; ++Lmic; } //Bass lights refresh rate. This value works quite well. If you want to reduce flickering you can try increase this value. if (Bdelay > 20) { EngduinoLEDs.setAll(OFF); Bdelay = 0; } }