Paso 2: código
El código para la música se escribe a mano, tocar de oído!
Mi código es una pequeña parte de la Carmen de Bizet
//Antonio Mainenti http://soundobject.eu/ Credit: Melo #define trigPin 7 // pin trig #define echoPin 6 //pin echo #define buzzer 9 // buzzer or output int variabile; //repeats 4 times the first part int freq = 294; //D int freq2 = 440; //A int freq3 = 698; //F int freq4 = 440; //A int freq5 = 294; //D int freq6 = 587; //D int freq7 = 554; //C# int freq8= 523; //C int freq9= 494; //B int freq10= 466; //A# int durata = 7000; //duration of notes int durata2 = 500; int durata3 = 70; int durata4 = 3000; int durata5 = 1000; int durata6 = 100; void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(buzzer, OUTPUT); } void loop() { long duration, distance; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; for(variabile=0; variabile<4; variabile++) // repetition { if (distance > 100 || distance <= 0){ Serial.println("Out of range"); noTone(buzzer); } else { Serial.print(distance); Serial.println(" cm"); tone (buzzer, freq, durata); //tone(pin, frequency, duration) } delay(500); if (distance > 100 || distance <= 0){ Serial.println("Out of range"); noTone(buzzer); } else { Serial.print(distance); Serial.println(" cm"); tone (buzzer, freq2, durata2); //tone(pin, frequency, duration) } delay(200); if (distance > 100 || distance <= 0){ Serial.println("Out of range"); noTone(buzzer); } else { Serial.print(distance); Serial.println(" cm"); tone (buzzer, freq3, durata3); //tone(pin, frequency, duration) } delay(500); if (distance > 100 || distance <= 0){ Serial.println("Out of range"); noTone(buzzer); } else { Serial.print(distance); Serial.println(" cm"); tone (buzzer, freq4, durata4); //tone(pin, frequency, duration) } delay(500); if (distance > 100 || distance <= 0){ Serial.println("Out of range"); noTone(buzzer); } else { Serial.print(distance); Serial.println(" cm"); tone (buzzer, freq5, durata5); //tone(pin, frequency, duration) } delay(500); } //second part if (distance > 100 || distance <= 0){ Serial.println("Out of range"); noTone(buzzer); } else { Serial.print(distance); Serial.println(" cm"); tone (buzzer, freq6, durata5); //tone(pin, frequency, duration) } delay(500); if (distance > 100 || distance <= 0){ Serial.println("Out of range"); noTone(buzzer); } else { Serial.print(distance); Serial.println(" cm"); tone (buzzer, freq7, durata5); //tone(pin, frequency, duration) } delay(500); if (distance > 100 || distance <= 0){ Serial.println("Out of range"); noTone(buzzer); } else { Serial.print(distance); Serial.println(" cm"); tone (buzzer, freq8, durata6); //tone(pin, frequency, duration) } delay(500); if (distance > 100 || distance <= 0){ Serial.println("Out of range"); noTone(buzzer); } else { Serial.print(distance); Serial.println(" cm"); tone (buzzer, freq8, durata6); //tone(pin, frequency, duration) } delay(500); if (distance > 100 || distance <= 0){ Serial.println("Out of range"); noTone(buzzer); } else { Serial.print(distance); Serial.println(" cm"); tone (buzzer, freq8, durata2); //tone(pin, frequency, duration) } delay(500); if (distance > 100 || distance <= 0){ Serial.println("Out of range"); noTone(buzzer); } else { Serial.print(distance); Serial.println(" cm"); tone (buzzer, freq9, durata5); //tone(pin, frequency, duration) } delay(500); if (distance > 100 || distance <= 0){ Serial.println("Out of range"); noTone(buzzer); } else { Serial.print(distance); Serial.println(" cm"); tone (buzzer, freq10, durata); //tone(pin, frequency, duration) } delay(500); if (distance > 100 || distance <= 0){ Serial.println("Out of range"); noTone(buzzer); } else { Serial.print(distance); Serial.println(" cm"); tone (buzzer, freq2, durata); //tone(pin, frequency, duration) } delay(1000); }