Paso 3: El código:
Hora de programar el Arduino! Por suerte, el programa es un simple como el cableado. No dude en violín con el código!
Hay dos programas diferentes, con código ligeramente diferente, que lograr diferentes looks. La primera parece menos animoso y parpadea/se descolora los LEDs más. El segundo es enrollar y no descolora los LEDs tanto. Cualquiera puede descargarlos mediante los archivos .zip, o copia/pasado de aquí.
Aquí está el código para la primera versión, AudioVisualDisplay:
/* * AudioVisualDisplay, a program by Jacob Field, written for the arduino * UNO. This program uses 6 LEDs connected to pins 8, 9, 10, 11, 12, * and 13 as a visual volume display for audio. The audio to be displayed * is fed into the Arduino via the analog pin A0. I recommend using a * second audio jack so you can hear the audio being played. ;) Feel free * to use this code, or any part of it, in your own personal projects, * or distribute it freely as you see fit, just give credit where credit * is due. */ byte ledValue; int signalIn; void setup() { //Set all the LED pins as outputs pinMode(8, OUTPUT); pinMode(9, OUTPUT); pinMode(10, OUTPUT); pinMode(11, OUTPUT); pinMode(12, OUTPUT); pinMode(13, OUTPUT); } void loop() { signalIn = analogRead(A0); //Read the analog value and store it ledValue = map(signalIn, 0, 100, 0, 6); //Convert the 10 bit value down to only 7 different values switch(ledValue){ case 0: //If the value is 0, set all the LEDs low digitalWrite(8, LOW); digitalWrite(9, LOW); digitalWrite(10, LOW); digitalWrite(11, LOW); digitalWrite(12, LOW); digitalWrite(13, LOW); break; case 1: //If the value is 1, set the first LED high digitalWrite(8, HIGH); digitalWrite(9, LOW); digitalWrite(10, LOW); digitalWrite(11, LOW); digitalWrite(12, LOW); digitalWrite(13, LOW); break; case 2: //If the value is 2, set the first 2 LEDs high digitalWrite(8, HIGH); digitalWrite(9, HIGH); digitalWrite(10, LOW); digitalWrite(11, LOW); digitalWrite(12, LOW); digitalWrite(13, LOW); break; case 3: //If the value is 3, set the first 3 LEDs high digitalWrite(8, HIGH); digitalWrite(9, HIGH); digitalWrite(10, HIGH); digitalWrite(11, LOW); digitalWrite(12, LOW); digitalWrite(13, LOW); break; case 4: //If the value is 4, set the first 4 LEDs high digitalWrite(8, HIGH); digitalWrite(9, HIGH); digitalWrite(10, HIGH); digitalWrite(11, HIGH); digitalWrite(12, LOW); digitalWrite(13, LOW); break; case 5: //If the value is 5, set the first 5 LEDs high digitalWrite(8, HIGH); digitalWrite(9, HIGH); digitalWrite(10, HIGH); digitalWrite(11, HIGH); digitalWrite(12, HIGH); digitalWrite(13, LOW); break; case 6: //If the value is 6, set all the LEDs high digitalWrite(8, HIGH); digitalWrite(9, HIGH); digitalWrite(10, HIGH); digitalWrite(11, HIGH); digitalWrite(12, HIGH); digitalWrite(13, HIGH); break; } }
Aquí está el código para la segunda versión, AudioVisualDisplayRev2:
/* * AudioVisualDisplay, Revision 2, a program by Jacob Field, written for the * arduino UNO. This version uses a different algorithm to analyze the * signal, creating a diffrent look This program uses 6 LEDs connected to pins * 8, 9, 10, 11, 12, and 13 as a visual volume display for audio. The * audio to be displayed is fed into the Arduino via the analog pin A0. * I recommend using a second audio jack so you can hear the audio being * played. ;) Feel free to use this code, or any part of it, in your own * personal projects, or distribute it freely as you see fit, just give * credit where credit is due. */ byte ledValue; int valueHigh; int signalIn; void setup() { //Set all the LED pins as outputs pinMode(8, OUTPUT); pinMode(9, OUTPUT); pinMode(10, OUTPUT); pinMode(11, OUTPUT); pinMode(12, OUTPUT); pinMode(13, OUTPUT); } void loop() { signalIn = analogRead(A0); //Read the analog value and store it valueHigh = valueHigh - 5; //Subtract 5 from valueHigh so that it goes down over time if(signalIn >= valueHigh){ //If the value coming from the audio jack is higher than the previous value of valueHigh: valueHigh = signalIn; //Set valueHigh to thge value coming in from the audio jack } ledValue = map(valueHigh, 0, 100, 0, 6); //Convert the 10 bit value down to only 7 different values switch(ledValue){ case 0: //If the value is 0, set all the LEDs low digitalWrite(8, LOW); digitalWrite(9, LOW); digitalWrite(10, LOW); digitalWrite(11, LOW); digitalWrite(12, LOW); digitalWrite(13, LOW); break; case 1: //If the value is 1, set the first LED high digitalWrite(8, HIGH); digitalWrite(9, LOW); digitalWrite(10, LOW); digitalWrite(11, LOW); digitalWrite(12, LOW); digitalWrite(13, LOW); break; case 2: //If the value is 2, set the first 2 LEDs high digitalWrite(8, HIGH); digitalWrite(9, HIGH); digitalWrite(10, LOW); digitalWrite(11, LOW); digitalWrite(12, LOW); digitalWrite(13, LOW); break; case 3: //If the value is 3, set the first 3 LEDs high digitalWrite(8, HIGH); digitalWrite(9, HIGH); digitalWrite(10, HIGH); digitalWrite(11, LOW); digitalWrite(12, LOW); digitalWrite(13, LOW); break; case 4: //If the value is 4, set the first 4 LEDs high digitalWrite(8, HIGH); digitalWrite(9, HIGH); digitalWrite(10, HIGH); digitalWrite(11, HIGH); digitalWrite(12, LOW); digitalWrite(13, LOW); break; case 5: //If the value is 5, set the first 5 LEDs high digitalWrite(8, HIGH); digitalWrite(9, HIGH); digitalWrite(10, HIGH); digitalWrite(11, HIGH); digitalWrite(12, HIGH); digitalWrite(13, LOW); break; case 6: //If the value is 6, set all the LEDs high digitalWrite(8, HIGH); digitalWrite(9, HIGH); digitalWrite(10, HIGH); digitalWrite(11, HIGH); digitalWrite(12, HIGH); digitalWrite(13, HIGH); break; } }