float dis; int trig = 12, echo = 11, speaker = 13, alarm = 2; bool flag = true; float ultrasonic(){ float interval; digitalWrite(trig, LOW); delayMicroseconds(5); digitalWrite(trig, HIGH); delayMicroseconds(10); digitalWrite(trig, LOW); interval = pulseIn(echo, HIGH); dis = (interval/2)/29.1; Serial.println(dis); return dis; } void setup(){ Serial.begin(9600); pinMode(trig, OUTPUT); pinMode(echo, INPUT); pinMode(speaker, OUTPUT); pinMode(alarm, INPUT); } void loop(){ if (digitalRead(alarm)&&flag){ //if the aralm is triggered and the flag is up tone(speaker, 1080); while (ultrasonic() >= 20){ //repeat until the distance is less than 20 Serial.println("Beep"); delay(50); } noTone(speaker); Serial.println("mute"); int cnt = 0; for (int i = 0; i<5;i++){ // snooze for 5 seconds before the alarm goes off again if (ultrasonic() < 20){ Serial.println("cool"); cnt ++; } Serial.println("Stop"); delay(1000); } if (cnt >= 5){//if you put your hand infront the detector for more than 5 seconds, the alarm will stop completely. flag = false; Serial.println ("Shut up"); tone(speaker, 1080, 500); delay(500); tone(speaker, 880, 500); delay(500); } } //Serial.println("nope"); }