Arduino code

From Medien Wiki
Revision as of 09:58, 1 December 2021 by Capo0368 (talk | contribs) (Created page with "int melody[32] = { 131,147,165,175,196,220,247, 262,294,330,349,392,440,494, 523,587,659,698,784,881,988, 1047,1175,1319,1397,1568,1760,1976, }; int TRIG = 3, ECHO = 4, speake...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

int melody[32] = { 131,147,165,175,196,220,247, 262,294,330,349,392,440,494, 523,587,659,698,784,881,988, 1047,1175,1319,1397,1568,1760,1976, }; int TRIG = 3, ECHO = 4, speaker = 7; void setup() { Serial.begin(9600); pinMode(TRIG,OUTPUT); pinMode(ECHO,INPUT); pinMode(speaker, OUTPUT); }

void loop() { float distance, duration; digitalWrite(TRIG, HIGH); delayMicroseconds(1); digitalWrite(TRIG,LOW);

duration = pulseIn(ECHO,HIGH); distance = ((float)(duration*340)/10000)/2; Serial.println(distance);

int i; if (distance <= 32) { i=map(distance,0,32,0,7); tone (speaker, melody[i], 250); delay (300); } else { noTone(speaker);

}


}