int tri= 15;
int echo= 14;
int buzzer = 16;
void setup() {
Serial1.begin(115200);
Serial1.println("Hello, Raspberry Pi Pico!");
pinMode(tri, OUTPUT);
pinMode(echo, INPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
// Start a new measurement:
digitalWrite(tri, LOW);
delayMicroseconds(2);
digitalWrite(tri, HIGH);
delayMicroseconds(10);
digitalWrite(echo, LOW);
// Read the result:
int duration = pulseIn(echo, HIGH);
Serial.print("Distance in CM: ");
Serial.println(duration / 58);
if((duration/58) <30){
for (int i = 0; i < 3; i++) {
tone(buzzer, 1000); // Phát âm thanh tần số 1000Hz
delay(200); // Kêu 200ms
noTone(buzzer); // Tắt còi
delay(300); // Nghỉ 300ms
}
}else if((duration/58) >= 30){
noTone(buzzer);
}
delay(100);
}