das ist der code den ich benutze, einfach mal zusammen geschnibbelt weil ich mich mit Melody überhaupt nicht auskenne:
int speakerPin = 9;
void setup() {
pinMode(speakerPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int Green = analogRead(A0);
int Red = analogRead(A1);
int Yellow = analogRead(A2);
int Blue = analogRead(A3);
int Orange = analogRead(A4);
int Enter = analogRead(A5);
if(Green < 1){
playNote('c', 300);
}
if(Red < 1){
playNote('d', 300);
}
if(Yellow < 1){
playNote('e', 300);
}
if(Blue < 1){
playNote('f', 300);
}
if(Orange < 1){
playNote('g', 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' };
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);
}
}
}
vielen dank!
