Created
November 30, 2022 07:41
-
-
Save idriszmy/1f720602f622d4e86a9319bf0d34dcf9 to your computer and use it in GitHub Desktop.
Build fun piano with ultrasonic using Maker Nano RP2040
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define echoPin 12 | |
#define trigPin 13 | |
#define led1 14 | |
#define led2 15 | |
#define buzzer 22 // buzzer pin in Maker Nano RP2040 is 22 | |
// defines variables | |
long duration; // variable for the duration of sound wave travel | |
int distance; // variable for the distance measurement | |
void setup() | |
{ | |
Serial.begin(9600); | |
pinMode(led1, OUTPUT); | |
pinMode(led2, OUTPUT); | |
pinMode(buzzer, OUTPUT); | |
pinMode(trigPin, OUTPUT); | |
pinMode(echoPin, INPUT); | |
} | |
void loop() | |
{ | |
// Ultrasonic Code | |
digitalWrite(trigPin, LOW); | |
delayMicroseconds(2); | |
digitalWrite(trigPin, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(trigPin, LOW); | |
duration = pulseIn(echoPin, HIGH); | |
distance = duration * 0.034 / 2; | |
Serial.print("Distance: "); | |
Serial.print(distance); | |
Serial.println("cm"); | |
if (distance < 5) { | |
tone(buzzer, 523); // Play tone C | |
digitalWrite(14, HIGH); | |
digitalWrite(15, HIGH); | |
delay(500); | |
noTone(buzzer); | |
digitalWrite(14, LOW); | |
digitalWrite(15, LOW); | |
delay(500); | |
} | |
else if (distance < 10) { | |
tone(buzzer, 587); // Play tone D | |
digitalWrite(14, HIGH); | |
digitalWrite(15, HIGH); | |
delay(500); | |
noTone(buzzer); | |
digitalWrite(14, LOW); | |
digitalWrite(15, LOW); | |
delay(500); | |
} | |
else if (distance < 15) { | |
tone(buzzer, 659); // Play tone E | |
digitalWrite(14, HIGH); | |
digitalWrite(15, HIGH); | |
delay(500); | |
noTone(buzzer); | |
digitalWrite(14, LOW); | |
digitalWrite(15, LOW); | |
delay(500); | |
} | |
else if (distance < 20) { | |
tone(buzzer, 698); // Play tone F | |
digitalWrite(14, HIGH); | |
digitalWrite(15, HIGH); | |
delay(500); | |
noTone(buzzer); | |
digitalWrite(14, LOW); | |
digitalWrite(15, LOW); | |
delay(500); | |
} | |
else if (distance < 25) { | |
tone(buzzer, 783); // Play tone G | |
digitalWrite(14, HIGH); | |
digitalWrite(15, HIGH); | |
delay(500); | |
noTone(buzzer); | |
digitalWrite(14, LOW); | |
digitalWrite(15, LOW); | |
delay(500); | |
} | |
else if (distance < 30) { | |
tone(buzzer, 880); // Play tone A | |
digitalWrite(14, HIGH); | |
digitalWrite(15, HIGH); | |
delay(500); | |
noTone(buzzer); | |
digitalWrite(14, LOW); | |
digitalWrite(15, LOW); | |
delay(500); | |
} | |
else if (distance < 35) { | |
tone(buzzer, 987); // Play tone B | |
digitalWrite(14, HIGH); | |
digitalWrite(15, HIGH); | |
delay(500); | |
noTone(buzzer); | |
digitalWrite(14, LOW); | |
digitalWrite(15, LOW); | |
delay(500); | |
} | |
else { | |
noTone(buzzer); | |
digitalWrite(14, LOW); | |
digitalWrite(15, LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment