#include <Arduino.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
// Sensor configuration
const int trigPin = 5;
const int echoPin = 18;
const int buzzerPin = 2;
const int dangerousDistance = 10;
const int forbiddenDistance = 20;
float distance = 0;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
// Send a short pulse to trigger the sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the time it takes for the pulse to return
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance in cm
distance = duration * 0.0344 / 2;
// Print the measured distance
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Compare the distance to the threshold and activate the buzzer if the distance is less than the threshold
if (distance <= forbiddenDistance) {
tone(buzzerPin, 1000); // Activate the buzzer at 1000 Hz frequency
if(distance <= dangerousDistance)
Serial.println("Break");
}
delay(100); // Wait 100 milliseconds to make the beep sound
noTone(buzzerPin); // Deactivate the buzzer
delay(500); // Wait for 100 milliseconds before the next measurement
}