using millis() to create a timer

Hi everybody,
once again, I need your help. I am using an ultrasonic sensor (hc-sr04) to power a led when an object enters it's radius and is 15 inches or closer to the sensor. I need to have a delay of 5 seconds between the sensor registering that an object is close to it and the led powering on. I am trying to use millis() because I don't want to use the delay() command.

long lastTime = 0;

const int in = 10,  out = 11; //hc sr04 echo and trig pins

long duration, inches;

void setup() {

  lastTime = millis();
pinMode(2,OUTPUT);
lastTime = millis();
}

void loop() {

 pinMode(out, OUTPUT);
  digitalWrite(out, LOW);
  delayMicroseconds(2);
  digitalWrite(out, HIGH);
  delayMicroseconds(5);
  digitalWrite(out, LOW);
  
duration = pulseIn(in, HIGH);

inches = microsecondsToInches(duration);

long microsecondsToInches(long microseconds)
{
 
  return microseconds / 74 / 2;
}

if (inches < 15) {
  if ( millis() - lastTime > 5000 ) {
digitalWrite(2,HIGH);
 lastTime = millis();
}
}
}

Using an Arduino Uno

It works but the timer stores the data into a variable so the next time that someone gets close to the sensor the timer will continue and mess up my delay. Is there a way to reset the timer every time that the object/person exits the sensor's range? If so, how? Thanks in advance

Use CTRL T to format your sketch.

BTW:
lastTime = millis(); // lastTime has to be unsigned long <-----<<<

Set a flag when the sensor detects someone.
In loop(), when the flag is set do your timing.

.

Also, why are you defining a function within loop()?

And...

quantumofchaos:
I am trying to use millis() because I don't want to use the delay() command.

That's nice, but sometimes using delay() makes sense. This might be one of those times, but whether or not it is depends on exactly how you want your code to react...

  1. Need to specify requirements more completely:
  • What should happen if object leaves 15in radius before 5 second delay is over?

  • What should happen if object leaves and returns to 15in radius before 5 second delay is over?

  • How long after object leaves 15in radius before LED should be turned OFF?

  1. The formatting of your code hurts my eyes. Please CTRL T

  2. Timer variables should be of type 'unsigned long'. Or, better yet, 'uint32_t'.

Your code will not even compile. Why do you have a function definition inside loop?

hmmmm...seems to be an echo in here! :slight_smile:

DaveEvans:
hmmmm...seems to be an echo in here! :slight_smile:

OOPS, sorry. All credits to you, will add a karma as compensation :wink:

gfvalvo:

  1. Need to specify requirements more completely:
  • What should happen if object leaves 15in radius before 5 second delay is over?

  • What should happen if object leaves and returns to 15in radius before 5 second delay is over?

  • How long after object leaves 15in radius before LED should be turned OFF?

If the object leaves the 15in radius before the led turns on the timer should reset and the led shouldn't be turned on (only if the timer reaches 5sec the led should be turned on). The led should instantly turn off after the object leaves the radius.

quantumofchaos:
If the object leaves the 15in radius before the led turns on the timer should reset and the led shouldn't be turned on (only if the timer reaches 5sec the led should be turned on). The led should instantly turn off after the object leaves the radius.

OK, take that information along with the suggestions others have offered and rewrite your code. Post it here (formatted) along with a description of the behavior you observe and how it differs from the behavior you want.

Also, check out the "state machine" links in the sticky at the top of the Programming Questions forum.

From Gammon's write-up:

When using a state machine you basically do this:

When an event happens (eg. serial input, switch press, object enters 15" radius or leaves it) then
Check what the current state (waiting, timing, LED on) is...

If the current state is A, then do X, and maybe change to state B.
If the current state is B, then do Y, and maybe change to state C.
And so on ...

PS: now that we know more about what you want to do, it is clear that "delay()" is not appropriate...