# Add time to if statement

Hi there!

I am doing a project with Ultrasonic sensors and buzzer.

My plan is to activate the buzzer ONLY IF an object is detected within a certain range for 5 minutes.

If it is less than 5 minutes, it will not activate the buzzer.

The following is the code I did so far WITHOUT the time function in the if statement.

if (distance < 25 || distance1 < 25) {
digitalWrite(Buzzerpin, HIGH);
}

else{
digitalWrite(Buzzerpin, LOW);
}

If you want to check that something is in range continuously for a certain length of time do it like this

``````void loop() {
// code to check range
if (range > limit) {
lastTimeOutOfRange = millis();
}

if (millis() - lastTimeOutOfRange >= requiredTime) {
// has been in range throughout the required time
// do whatever needs to be done
}
}
``````

...R

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom...

``````   if (range > limit) {
lastTimeOutOfRange = millis();
}
``````

Note: If nothing is in range you will probably get a range of zero. This might be better:

``````   if (range == 0 || range > limit) {
lastTimeOutOfRange = millis();
}
``````

my understanding is that millis() is the time in milliseconds since the arduino started. Divide by 1000 to equal seconds or by 60000 to equal minutes.

pmagowan:
my understanding is that millis() is the time in milliseconds since the arduino started. Divide by 1000 to equal seconds or by 60000 to equal minutes.

Where in the original post do you see any requirement for using the time in milliseconds since the arduino started? Nowhere, because it’s not there.

aarg:
Where in the original post do you see any requirement for using the time in milliseconds since the arduino started? Nowhere, because it's not there.

The OP stated 5 minutes. I assume that this is a variable that may need changed and since he uses minutes as his scale and the arduino uses millis it may be useful to do a conversion. Certainly that is what I would do to make the code readable to an amateur since I am one. I also note that everyone else used millis() so not sure what your point is. Maybe you have better ideas but just choose not to post them.

5UL * 60UL * 1000UL is the only number you need.
No division required.

pmagowan:
Maybe you have better ideas but just choose not to post them.

I don't need to post ideas that are absolutely standard approaches that are exhaustively explained in the many tutorials and help links on this site and many others. Often I suggest that people consult the obvious sources of information like the reference section, the tutorials and the example sketches that ship with the IDE. However, I always marvel that someone wouldn't just go there themselves.

Your attempt at advice was completely misleading, and could seriously waste another beginners time. You should contemplate whether you are at a level of knowledge where you can fully grasp the implications of a question that is posted here. Because, in this case, you completely missed.

aarg:
Your attempt at advice was completely misleading, and could seriously waste another beginners time. You should contemplate whether you are at a level of knowledge where you can fully grasp the implications of a question that is posted here. Because, in this case, you completely missed.

Well, well-done for adding to the body of knowledge. I presumed that a forum was for discussion leading to greater understanding. It seems you think differently. Of course, people don't need to chat about stuff, they could just do some dry research but sometimes a kindly word from someone more experienced is useful. Sometimes the things those most experienced say goes well over the head of a beginner and so a couple of amateurs using a forum to have a chat can inform both of them.

The internet is full of people with negative attitudes and I don't want to be one of them so, good luck with whatever makes you feel happy. If nothing else you are helping me to get my post count up and remove restrictions!

pmagowan:
my understanding is that millis() is the time in milliseconds since the arduino started.

This is covered here

Divide by 1000 to equal seconds or by 60000 to equal minutes.

This largely irrelevant in this context.

Thanks previously AWOL,

In the general brainstate of a beginner my thought process was that converting from a millis() to a minutes timescale gives numbers that are far more in keeping with the beginner level question of the OP. Of course you can simply work everything out in millis() and make your requiredTime variable by 5601000 to convert to the 5 minutes the OP wanted and I am sure this is the correct technical way to do it but, if your brain is working on a minute timescale not a millis timescale then it could make sense to do the conversion even just for debugging. Not sure why this would be controversial but happy to be informed.

In fact if I was doing this project I may even think about allowing the user to change the time through the serial monitor as required and code a conversion from human time of minutes to machine time of millis but maybe I am mad

I can’t comment on the state of your mental health, but even if you allowed someone to enter different times, your calculations wouldn’t involve divisions.

I would think you could do it either way. Either convert your requiredTime variable to millis using multiplication or convert millis to minutes using division. Both achieves the same although, granted, it is more cumbersome to convert everything to minutes rather than just converting requiredTime to millis. I would assume, however, that most people know that the inverse of division is multiplication and thus understanding one allows you to understand the other and the relationship of machine time to human time is then understood sufficiently to do whatever conversion is required. Who would have thought my innocuous statement would have started such a philosophical discussion.

I think what people are saying is that it is better to convert minutes to millis() than to convert millis() to minutes because millis() is the natural timescale for an Arduino.

...R

No, I think I’m saying that I’m not at all interested in when the clock was made / started.

Ever.

(Apart from, I’m concerned they handle the Unix clock rollover in 2038 correctly, so they don’t screw-up my pension payments)

Not to worry, you'll be gone from some COVID19 variant by then anyway ...