hello! everyone this is my first time in arduino fourm.
I made a security alarm using arduino uno, when the distance decreases by 10 cm, a buzzer attached to pin12 starts.
but i firstly have to set a specific value of distance in code() by taking value from serial monitor but is it possible that arduino can detect a sudden specific change in sensor value, if there is then please tell. my code is below. thankyou in advance.
int trigPin = A5;
int echoPin = A4;
int duration = pulseIn(echoPin, HIGH);
int distance = (duration/2) / 29.1;
int val=155;// change this value for different locations from serial monitor
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(12, OUTPUT);
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
long duration, wait;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;delay(10);
Serial.print(distance);
Serial.println("cm");delay(10);
if (val-10>distance) {
digitalWrite(12,HIGH);
}
}