I'm attempting to build a distance sensor with a buzzer that's kind of like a reverse backup sensor. Basically, it detects the presence of a cabinet door, if the door is opened then the buzzer goes off.
This project has to be linear, so I wanted to make the buzzer increase in volume depending on how many times the door has been opened.
If the door is opened two times or less, there's no sound.
if the door is opened three times, the tone is 800
four times, the tone is 700
five times, 1900
six times, 2000
The problem is, I'm not sure how to code in this limit trigger with the motion sensor, any help would be appreciated, below will be my code so far.
#define PIEZO_PIN 11
const int trigger = 9;
const int echo = 10;
float distance;
float dist_inches;
void setup() {
Serial.begin(9600);
// settings for ultrasonic sensor
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
pinMode(PIEZO_PIN, OUTPUT);
}
void loop() {
// Trigger the sensor to start measurement
// Set up trigger
digitalWrite(trigger, LOW);
delayMicroseconds(5);
// Start Measurement
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);
// Acquire and convert to inches
distance = pulseIn(echo, HIGH);
distance = distance * 0.0001657;
dist_inches = distance * 39.37;
if (dist_inches <= 3) {
delay(200);
noTone(PIEZO_PIN);
}
else if (dist_inches >= 5) {
tone(PIEZO_PIN, 800);
delay(50);
noTone(PIEZO_PIN);
}
}
Use a counter to keep track how often the door is opened. Set the desired volume (it's actually the frequency) based on the number of times. You can use if / else if / else for the latter.
Sorry I'm a little confused by the question, It's been years since I've used Arduino, could you elaborate a little more? To my understanding the values I'm observing are just the distance (in inches) the door is from the distance sensor
This gives me a good idea on how the counter program should look, as I've never used it before. I think I'm starting to get how to apply it and get it to set off the buzzer with the motion sensor, I'm still just a little confused on how to properly define and implement the trigger being the door opening more than 2 times and the buzzer going off after the distance sensor detects 5 inches being surpassed
Do you have any links to examples on how that would look given what I'm doing? Having trouble finding examples and it's a little difficult for me to fully understand
You have that right. The code sends a series of pukses and waits for an exho. pulseIn() captures the echo time. Your code then calculates the distance.
Your code compares the distance to 3, and if less or equal, stops the buzzer. Your code also compares the distance to 5, and stops the buzzer if equal or greater.
I want the buzzer to be silent when the door is closed (the distance from the sensor to the door is around 2 to 3 inches) and to make noise when the door is opened more than two times, which would exceed the 2 to 3 inches the sensor is detecting. Apologies for the lack of clarification