#define echoPin 6 #define trigPin 7 #define buzzerPin 8 #define doo 1000// do komutu ile karismasin diye doo yazdim (kalin do) #define re 1122 #define mi 1260 #define fa 1335 #define sol 1498 #define la 1681 #define si 1887 #define dom 2000// do komutu ile karismasin diye dom yazdim (ince do) int maximumRange = 50; int minimumRange = 0; int melodiler[] = {doo, re, dom , si, doo, si, doo, re, dom , si, doo, si, doo, re, dom , si, doo, si, la, si, la, sol, fa, la }; int notasuresi[] = {6, 6, 2, 6, 6, 2, 6, 6, 2, 6 , 6, 2, 6, 6, 2, 6, 6, 2, 6, 9, 8, 5, 5, 2 }; // her notanin süresini array yaptim int tempo = 1200; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(buzzerPin, OUTPUT); for (int Nota = 0; Nota < 24; Nota++) { int sure = tempo / notasuresi[Nota]; tone(8, melodi[Nota], sure); // 8 numarali bacaktan notalarin frekanslarini nota uzunluklarina göre gönder delay(sure * 1.2); // notalar arasinda biraz beklesin. Eger 1 ile çarparsaniz notalar birbirine bitisik olur } } void loop() { int olcum = mesafe(maximumRange, minimumRange); //olcum *= 10; melodi(olcum * 10); } int mesafe(int maxrange, int minrange) { long duration , distance; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = duration / 58.2; delay(50); //50 ms bekliyoruz. if (distance >= maxrange || distance <= minrange) return 0; return distance; } int melodi(int dly) { tone(buzzerPin, 440); delay(dly); noTone(buzzerPin); delay(dly); }