# Millis with distance

I read all of your examples and I got a few questions.

I want to make codes which control LED. I intended to turn on/off the LED depending on the distance.(I use ultrasonic sensor to check distance)

For example if d is between 10cm and 20cm, LED will be on. (d=distance) And if d is between 10cm and 20cm for 5 seconds, LED will be off though it is in ‘ON-STATE’ distance.

My question is how I can do it by using ‘Millis’. plz help, especially ‘And if ‘ line .

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. 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.

Can we see the code you have and we can then help you add to it to get speed. There is a library that will do all the senor interface and distance calculations for you.

or

http://playground.arduino.cc/Code/NewPing

Tom... :)

The demo Several Things at a Time illustrates the use of millis() to manage timing. It may help with understanding the technique.

...R

I am very new in Arduino , so my program is weak.But I think that it may work.here it is

``````int trigger=7;
int echo=6;
int LED=5;
long time=0;
long dist=0;
void setup()
{
Serial.begin (9600);
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
pinMode(LED, OUTPUT);
}
void loop()
{
digitalWrite(trigger, LOW);

delay(5);
digitalWrite(trigger, HIGH);
delay(10);
digitalWrite(trigger, LOW);

time = pulseIn(echo, HIGH);

dist = (time/2) / 29.1;
if (dist >= 10000 || dist <= 0)

{
Serial.println("No measurement");
}
else
{
Serial.print(dist);
Serial.println("cm");
}
delay(1000);
if (dist>125)
{
digitalWrite(LED,HIGH);
}
else
{
digitalWrite(LED,LOW);
}
if (dist<40)
{
digitalWrite(LED,HIGH);
delay(1500);
digitalWrite(LED,LOW);
}

}
``````

farhan_ru_school: I am very new in Arduino , so my program is weak

Your program is using the delay() function which the OP is trying to avoid.

...R

That looks like it will work. A good example.

However it has a huge delay(1000) in the middle. The LED goes on 1 second after the distance condition is met. That delay is only relevant to limiting the flood of data going out the serial port. It should not slow down the main function of the program.

This is a good time to try to use the blink-without-delay method (or several-things-at-one-time linked above) to make the serial print only happen once a second while the rest of the loop runs hundreds of times per second.

MorganS: This is a good time to try to use the blink-without-delay method (or several-things-at-one-time linked above) to make the serial print only happen once a second while the rest of the loop runs hundreds of times per second.

With all due respect, that is what the OP was asking advice about in his Original Post.

...R

All you need is the NewPing library

You can find it here: https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home

Use the example "Simple NewPing Sketch". It works perfectly.

All I had to do to make it run was to change the Serial.begin line to Serial.begin(9600). Use higher values if you do many measurements per second. Do not forget to adjust your serial monitor.

If you want to use the tone() function you should read the documentation. Or do the following:

Open NewPing.h and change TIMER_ENABLED=true to TIMER_ENABLED=false.

Hi,

Post #7 see Post #1

Tom.... :)