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
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...
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...