#define ECHO_PIN 15
#define TRIG_PIN 2
#define l1 33
#define l2 32
#define l3 5
#define buz 4
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(l1, OUTPUT);
pinMode(l2, OUTPUT);
pinMode(l3, OUTPUT);
pinMode(buz, OUTPUT);
}
float readDistanceCM() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2;
}
void loop() {
// float distance = readDistanceCM();
Serial.print("Measured distance: ");
Serial.println(readDistanceCM());
delay(5000);
if(readDistanceCM()< 60)
{
Serial.println("Hati-hati Banjir");
digitalWrite(l1, HIGH);
digitalWrite(l2, HIGH);
digitalWrite(l3, HIGH);
tone(buz, 2000);
noTone(buz);
delay(2000);
tone(buz, 500, 2000);
delay(3000);
}
else if(readDistanceCM()>= 60 && readDistanceCM()< 70)
{
Serial.println("Potensi Bahaya Banjir");
digitalWrite(l1, HIGH);
digitalWrite(l2, HIGH);
digitalWrite(l3, LOW);
}
else if(readDistanceCM()>= 70)
{
Serial.println("Wilayah Aman Banjir");
digitalWrite(l1, HIGH);
digitalWrite(l2, LOW);
digitalWrite(l3, LOW);
}
}