Square Synth
Esempio didattico di come creare un piccolo sintetizzatore audio con Arduino.
Per questo progetto sono stati usati 5 pulsanti ma è espandibile a piacere, magari con un multiplexer.

Codice Sorgente
/* Square Synth * 2009, A. Degani * * Speaker on digital pin 9 * switch button fron digital pin 2 to 6 * */ int speakerPin = 9; int btn1Pin = 2; int btn2Pin = 3; int btn3Pin = 4; int btn4Pin = 5; int btn5Pin = 6; int val1 = 0; int val2 = 0; int val3 = 0; int val4 = 0; int val5 = 0; void playTone(int tone, int duration) { for (long i = 0; i < duration * 1000L; i += tone * 2) { digitalWrite(speakerPin, HIGH); delayMicroseconds(tone); digitalWrite(speakerPin, LOW); delayMicroseconds(tone); } } void playNote(char note, int duration) { char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; // play the tone corresponding to the note name for (int i = 0; i < 8; i++) { if (names[i] == note) { playTone(tones[i], duration); } } } void setup() { pinMode(speakerPin, OUTPUT); pinMode(btn1Pin, INPUT); pinMode(btn2Pin, INPUT); pinMode(btn3Pin, INPUT); pinMode(btn4Pin, INPUT); pinMode(btn5Pin, INPUT); } void loop() { val1 = digitalRead(btn1Pin); // read input value val2 = digitalRead(btn2Pin); // read input value val3 = digitalRead(btn3Pin); // read input value val4 = digitalRead(btn4Pin); // read input value val5 = digitalRead(btn5Pin); // read input value if (val1 == LOW) { // check if the input is HIGH playNote('c',10); } if (val2 == LOW) { // check if the input is HIGH playNote('d',10); } if (val3 == LOW) { // check if the input is HIGH playNote('e',10); } if (val4 == LOW) { // check if the input is HIGH playNote('f',10); } if (val5 == LOW) { // check if the input is HIGH playNote('g',10); } }
|
|
Scarica i sorgentisquare_synth.pde (1,7 KB) |







