Controlling the blink "speed" of a led with the ultrasonic distance sensor

The blink of the led depends on the distance of an object, which is measured by the sensor. I achieved this by stablishing some parameters for example, if the distance is lower than 33 cm and higher than two turn on and off the led with a delay of "500"; but if the distance is higher than 33cm and lower than 100 cm delay "1000".

Is there another way to stablish that when the distance value is closer to 0 for example, the speed at which the led blinks increase?

here is my code:

float  dist = 0;
void setup()
{
  
  //led
  pinMode(12, INPUT);
  
  //sensor
  pinMode(5, OUTPUT);
  pinMode(4, INPUT);
  Serial.begin(9600);
}

void loop()
{
  //meausre the distance
 digitalWrite(5,LOW);
 delayMicroseconds(5);        
 digitalWrite(5,HIGH);  
 delayMicroseconds(10);      
 digitalWrite(5,LOW); 
 dist = pulseIn(4,HIGH);      
 dist = dist/58;// CM convertion.
 Serial.print ("Distance = ");
 Serial.print (dist);         
 Serial.println (" cm");
  
  
	//The further the distance the slower the blink is 
  if (dist >= 2.3 && dist<= 50.0){digitalWrite (12, HIGH);
                                   delay(400);
                                    digitalWrite (12, LOW);
                                  delay(400);}
  
  if (dist >= 50.0 && dist<= 100.0){digitalWrite (12, HIGH);
                              delay(800);
                               digitalWrite (12, LOW);
                                  delay(800);}
  
   if (dist >= 100.0 && dist<= 200.0){digitalWrite (12, HIGH);
                              delay(1200);
                               digitalWrite (12, LOW);
                                  delay(1200);}
  
  if (dist >= 200.0 && dist<= 336.0){digitalWrite (12, HIGH);
                              delay(2000);
                               digitalWrite (12, LOW);
                                  delay(2000);}  
}
1 Like

Welcome to the forum. Have a look at 'blink without delay' etc to see how to avoid using blocking code. Read the forum guide.

you could play with map with the 'interval' variable in the blink without delay example

1 Like

Which type of sensor are you using?

Can it measure 2.3 cm?

Play with the BlinkWithOutDelay example of the IDE too.

p.s. pmagowan was faster :nerd_face:

2 Likes

I would put the measurement part into a function which you can call at intervals that you can determine/change as needed. That way you are not adjusting the blink on the led many times a second. I would then map a range of blinking speeds to the distance that the function returns. That way you can have a very fluid adjustment without thinking too much about it.

1 Like

It's what do you mean?

if((dist > 2.0) && ( dist < 33))
{
 digitalWrite (12, HIGH);
 delay(500);
 digitalWrite (12, LOW);
}

Now say the thing can measure from 0 to 100. At 0 the blink would be 0 blinks per time and at 50 could be 5 blinks per time and 100 blinks per time.

Now check how map() could be used to get the blink rate.

Don't forget the suggestion from post#4.

1 Like

Yes, but what i was trying to achieve is to use another function so that the blink speed of the led was inverse proportional to the distance. But thank you

Thank you

Thanks

To remove all this ifs you can use the map function as was said before.

Like this:

int myDelay = map (dist, 2, 336, 500, 2000);

Or:

int myDelay = map (dist, 2, 336, 2000, 500);

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.