I need help making a ultrasonic seat-reminding sensor using a buzzer

Hello all, I'm fairly new to the concept of coding and Arduino. I'm making a school project where an ultrasonic sensor is used to detect someone's sedentary action and to set off an alarm(buzzer)if the person has exceeded, let's say for 2 hours. However, being the hypocrite I am I've tried to learn how to code for almost 7 hours straight now sitting down. Looking for advice/help on how to code the project.
The specifics:

  • Able to detect someone within a 50cm range
  • Starts a timer/a delay where the buzzer will go off in 2 hours after sitting down
  • Timer/delay resets if the object within range is false for more than 5 mins
  • Able to restart this process
    Sorry for being a nuisance, I've copy+pasted most of the coding :wink:
int const trigPin = 9;
int const echoPin = 10;
int const buzzPin = 11;
unsigned long delayStart = 0; // the time the delay started
bool delayRunning = false; // true if still waiting for delay to finish

void setup()
{
pinMode(trigPin, OUTPUT); // trig pin will have pulses output
pinMode(echoPin, INPUT); // echo pin should be input to get pulse width
pinMode(buzzPin, OUTPUT); // buzz pin is output to control buzzering
  digitalWrite(buzzPin, LOW); // turn led on
  delayStart = millis();   // start delay
  delayRunning = true; // not finished yet
}

void loop()
{
// Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters
int duration, distance;
// Output pulse with 1ms width on trigPin
digitalWrite(trigPin, HIGH);
delay(1);
digitalWrite(trigPin, LOW);
// Measure the pulse input in echo pin
duration = pulseIn(echoPin, HIGH);
// Distance is half the duration devided by 29.1 (from datasheet)
distance = (duration/2) / 29.1;
// if distance less than 0.5 meter and more than 0 (0 or less means over range)
if (distance <= 50 && distance >= 0) {
// Buzz
Timer();
} else {
// Don't buzz
offTimer();
}
// Waiting 60 ms won't hurt any one
  delay(60); 
}

void Timer(){
if (delayRunning && ((millis() - delayStart) >= 3000)) {
    delayRunning = false; // // prevent this code being run more then once
    digitalWrite(buzzPin, HIGH); // turn buzzer on
}
}

void offTimer(){
    delayRunning = false; // // prevent this code being run more then once
    digitalWrite(buzzPin, LOW); // turn buzzer off
}

I'm not very good an critiquing other's code. However here is the code I used to measure distance with an HC-04 ultrasonic board.

uint16_t getEchoDistance (void){
  unsigned long duration;
  digitalWrite(trigPin, LOW);
  delay(5);

  // trigger the sensor
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(8);
  digitalWrite(trigPin, LOW);

  // Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
  duration = pulseIn(echoPin, HIGH);
  distance = (duration * 0.034 / 2);
  return distance;
}

What does it do?

Assuming that you have problems with it, add some serial prints to show your what's going on.

I think both codes work fine to find the distance, I've tested my code out in a simulator and the distance measuring was just fine. Thanks for the response however :+1:

You'd kinda expect the pin to already be LOW from the last time (it from being initialised), so all this does is wastes five milliseconds.

Yup..

Regarding my previous post, I'm wondering if I could make a variable(such as 'unsigned long') to start whenever (condition). I would like the variable to start when the echoPin receives an object that's closer than 50cm. The concept is for the delay/timer to begin when the object has come in range then sets off a buzzer after 3 seconds.
Sorry in advance for my coding :wink:

Edit: Is there also a way to set 'if the distance from object is less than 50 cm, then echoPin is read as HIGH. Or should I just make a boolean expression to start the timer.

int const trigPin = 9;
int const echoPin = 10;
int const buzzPin = 11;
unsigned long delayStart = 0; // the time the delay started
bool delayRunning = false; // true if still waiting for delay to finish


void setup()
{
pinMode(trigPin, OUTPUT); // trig pin will have pulses output
pinMode(echoPin, INPUT); // echo pin should be input to get pulse width
pinMode(buzzPin, OUTPUT); // buzz pin is output to control buzzering
  digitalWrite(buzzPin, LOW); // turn led on
  delayStart = millis();   // start delay
   delayRunning = true; // not finished yet
}

void Timer(){
if (delayRunning && ((millis() - delayStart) >= 3000)) {
    delayRunning = false; // // prevent this code being run more then once
    digitalWrite(buzzPin, HIGH); // turn buzzer on
}
}

void offTimer(){
    delayRunning = false; // // prevent this code being run more then once
    digitalWrite(buzzPin, LOW); // turn buzzer off
}

void loop()
{
// Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters
int duration, distance;
// Output pulse with 1ms width on trigPin
digitalWrite(trigPin, HIGH);
delay(1);
digitalWrite(trigPin, LOW);
// Measure the pulse input in echo pin
duration = pulseIn(echoPin, HIGH);
// Distance is half the duration devided by 29.1 (from datasheet)
distance = (duration/2) / 29.1;
// if distance less than 0.5 meter and more than 0 (0 or less means over range)
if (distance <= 50 && distance >= 0) {
// Buzz
Timer();
} else {
// Don't buzz
offTimer();
}
// Waiting 60 ms won't hurt any one
  delay(60); 
}

So, what have you tried?
Paul

Please explain what make a variable to start means.

Try putting the following code at the start of your Timer function:

void Timer() {
  if (!delayRunning) {
    delayStart = millis();
    delayRunning = true;
  }

@er_name_not_found I would like to replace my unsigned long "delayStart" into a variable that starts when the distance is closer than 50cm. Let's say delayStart grabs the time from when (distance < 50), then the delay timer starts. Due to my lack of knowledge, I don't know how to do that so I've asked for help :wink:

@gerivega Ill try it

I now understand what I want in the coding. So relating back to my previous posts, I want delayStart to act as a timestamp, ex; the object comes in range at 5 seconds after start time. So the delayStart variable will equal 5 seconds. The current run time will continue but the delayStart will stay as 5 seconds. So using the code (currentTime - delayStart) >= 3000) will mean the command carried out will be eligible at 8 seconds(3000 millis in the condition). However due to my lack of knowledge I've put

unsigned long currentTime = millis();

void loop();
//coding about calculating distance beforehand//
if (distance <= 50 && distance >= 0) {
delayRunning = true; // not finished yet
delayStart = currentTime;
// Buzz
Timer();

void Timer(){
if (delayRunning && (currentTime - delayStart) >= 3000) {
    delayRunning = false; // // prevent this code being run more then once
    digitalWrite(buzzPin, HIGH); // turn buzzer on
  	delayStart = currentTime;

I believe this causes the currenTime - delayStart = 0 continuously. Are there tips on how to fix it? I've seen other similar codes use ' ++ ' but I havn't grasped the concept of it.

The ++ operator is for incrementing a variable by one. It actually comes in two flavours: ++var (preincrement) and var++ (postincrement) .

For instance:

int foo = 5;
printf ("%i\n", ++foo); /* prints 6 */
printf ("%i\n", foo);   /* prints 6 */

int bar = 10;
printf ("%i\n", bar++); /* prints 10 */
printf ("%i\n", bar);   /* prints 11 */

However, this has little to do with the timed events you are trying to implement.

Ohh thanks for the explanation.

Topics about the same project merged so that the context is maintained

@iprinxe please do not start new topics when you don't need to

Hi iprinxe,

as a general advice:
You should describe the desired functionality first in normal words by avoiding any programming terms.

The reason why is: You are very familiar with normal words and as a newcomer you may have some misconceptions about how coding works. Using normal words as the first step ensures that the other users get a clear picture of the functionality you want.

After this normal-word-description you can and you should start asking programming questions.

best regards Stefan