Hello all, i am new to arduino and i was wondering how do you make a led blink 3 times if a object is near the ultrasonic range finder... I have tried writing high/low 5 times as you can see from the code but it is still flashing continuously. Here is my code so far:
int redled = 3;
int yellowled = 4;
int greenled = 5;
int trigPin= 11;
int echoPin= 9;
// put your main code here, to run repeatedly:
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
float distance = pulseIn(echoPin, HIGH); // return 0 if no high pulse in 1sec
distance = distance/58;
Serial.print(distance);
Serial.println("cm");
delay(200);
Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.
Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.
You might want to pay attention to your own comments:
ItsYaBoy:
float distance = pulseIn(echoPin, HIGH); // return 0 if no high pulse in 1sec
...
if ((distance<=10))
So if the ultrasonic ping never returns you will have the same result as if there is an object within 10 cm. The output in the Serial monitor would show if that's the cause of the issue you're seeing but it would definitely make sense to modify your code to correctly handle a timeout regardless. Note that the default 1000 ms timeout of pulseIn() is much longer than necessary for this application.
ItsYaBoy:
else if (distance>10)
FYI, your code will be easier to maintain and more efficient if you just use else there instead of else if but it will work the same either way.
You never answered my question: what do the serial prints say?
so i will need to implement a timeout function in order for it to work?
I would concentrate on getting correct ranges from the rangefinder. If the pusleIn() is timing out, then there is nothing within range or your rangefinder is not working. And you don't "implement a timeout function", you add a timeout parameter to the pulseIn() function. You can also add something like this to your code so you know that the timeout occurred.:
else if(duration == 0)
{
Serial.print("nothing in range and/or timed out")
}
The op's code ran ok for me giving distances that looked ok- didn't actually reach for a ruler though.
If this is the issue:
I have tried writing high/low 5 times as you can see from the code but it is still flashing continuously.
that's becasue of loop and it just redoes the blinking as long as the object is close.
I fixed it just by using a boolean flag "newlyClose" &&'d with the "if", setting the flag true then. When the object moves out of range, set the flag false.
So it only does one set of blinks until it moves out of range and blinks again next time.