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);}
}