Mikro 2023 Modul 1 Aplikasi I/O sederhana (kipas dan suara)
9:58:00 PM
Jarak dan Suara
1. Foto Hardware dan Diagram Blok [Kembali]
Diagram Blok:
2. Prosedur Percobaan [Kembali]
Prinsip Kerja:
Inputan masuk dari sensor jarak ( Ultrasonik ) dengan jara tertentu maka speaker akan hidup
kondisi pertama
dibawah 500 cm tidak hidup speaker
kondisi kedua
diatas 500 cm hidup
4. Flowchart dan Listing Program [Kembali]
Flowchart:
Listing Program:
int trigPin = 7;
int echoPin = 6;
int buzzerPin = 8; // Define the buzzer pin
int distance, duration;
int buzzerThreshold = 20; // Set the distance threshold for the buzzer to sound, adjust as needed
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT); // Set the buzzer pin as an output
}
void loop() {
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
Serial.print("Jarak objek: ");
Serial.print(distance);
Serial.println(" cm");
if (distance < buzzerThreshold) {
// If the distance is less than the threshold, play a tone on the buzzer
tone(buzzerPin, 1000); // You can adjust the frequency as needed
} else {
// If the distance is greater than or equal to the threshold, turn off the buzzer
noTone(buzzerPin);
}
delay(500); // You may want to adjust this delay based on your application.
}
-Input dari sensor jarak akan menentukan keluarn dari speaker
File_keperluan_modul_Jarak dan Suara