Stap 3: De Code:
Tijd voor het programmeren van de Arduino! Gelukkig, het programma is een eenvoudige als de bedrading. Voel je vrij om te knoeien met de code!
Er zijn twee verschillende programma's, met iets andere code, die verschillende looks te bereiken. De eerste ziet er minder veerkrachtige en flikkert/verdwijnt de LEDs meer. De tweede is springerige en de LEDs zo veel niet vervagen. U kunt downloaden hen via de zip-bestanden, of exemplaar/verleden vanaf hier.
Hier is de code voor de eerste versie, 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; } }
Hier is de code voor de tweede versie, 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; } }