Need help coding a loop to trigger after a certain number of times

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);
  }
}

You talk about a motion sensor but the code you show is for a distance sensor.
Which one are you finally going to use?

I misspoke, I had meant to put distance sensor, that's my bad

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.

1 Like

What values do you observe?

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

Did you see the counting scheme in the State Change Detection built-in example?

If you understand state-change detection, and how that example works, it should be straightforward.

The pins you are using in the code are the pins you must use on the arduino. Tell me what you do not understand about this.

Sorry, the buzzer is pin 11, my echo is pin 10, and the trigger is pin 9

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.

What happens if distance is 4?

Ah i see, so for starters I need to change it to the buzzer will start if the distance is greater than 3, Thank you for catching that

I think you want the buzzer to warn at a small distance, and silence when safe.

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

Door open or closed is not the issue. You must write code for the distances you have set as thresholds.

cpp
int openCount = 0;

void loop() {
// Your existing sensor code here

if (dist_inches > 3) { // Door opened
openCount++;
delay(200); // Debounce

if (openCount == 3) tone(PIEZO_PIN, 800, 300);
else if (openCount == 4) tone(PIEZO_PIN, 700, 300);
else if (openCount == 5) tone(PIEZO_PIN, 1900, 300);
else if (openCount >= 6) tone(PIEZO_PIN, 2000, 300);

}
else {
openCount = 0; // Reset when door closes
}
}
Key points:

Tracks openings with openCount

Plays different tones based on count

Resets when door closes

Add this to your existing code

Let me know if you want me to explain any part!

Wrap your code in code block...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.