Stap 3: De Code
int speakerPin = 9; int sensePin = A4; int ledPin1 = 13; //Green LED Pin int ledPin2 = 12; //Red LED Pin int length = 15; char notes[] = "a aaagg gfeedd"; float beats[] = {1, 1, 1, .5, .5, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, };<br>int tempo = 300; 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'}; float tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956}; for (int i = 0; i < 8; i++) { if (names[i] == note) { playTone(tones[i], duration); } } } void setup() { Serial.begin(9600); pinMode(speakerPin, OUTPUT); pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); } void playSong() { for(int i = 0; i < length; i++) { if(notes[i] == ' ') { delay(beats[i] * tempo); } else { playNote(notes[i], beats[i] * tempo); } delay(tempo / 2); } } void loop() { int value = analogRead(A4); Serial.println(value); if (value >= 500) { //This threshold can be changed for different weights digitalWrite(ledPin1, HIGH); digitalWrite(ledPin2, LOW); } else { digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, HIGH); playSong(); } }