#define led 8
#define Trigger 13
#define Echo 12
void setup(){
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(Trigger, OUTPUT);
pinMode(Echo, INPUT);
digitalWrite(Trigger, LOW);
}
void loop(){
long t;
long d;
digitalWrite(Trigger, HIGH);
delayMicroseconds(10);
digitalWrite(Trigger, LOW);
t = pulseIn(Echo, HIGH);
d = t/59;
if (d<10){
digitalWrite(led,HIGH);
}else{
digitalWrite(led,LOW);
}
}
Since you have serial.print() available to show you a message, why not use it to verify your assumption?
Thanks! Its looping but i am not getting any readings from the ultrasonic sensor
Solved it, i had defined them wrong and i had put the cables in the wrong polarity, the tutorial i saw put them wrong.
You are also jumping to conclusions that are not necessarily true! Why not add a time-out value to the pulseln function call? Might also add a test to see if you got a valid return time or got the time-out time. Only then can you know if you got an echo.
The function times out by default after 1000 milliseconds, so any other activity would happen only once a second in the absence of quickly returned pulses.
pulseIn(pin, value, timeout);
pukseIn() accepts a third parameter, the timeout interval in milliseconds. If you try it, set it to something like 2 or 3 times the longest you expect to wait for a valid pulse.
Also handy to test is the returned value, where 0 will mean you did not get a pulse within the timeout period.
See
https://docs.arduino.cc/language-reference/en/functions/advanced-io/pulseIn/
where I just again "learned" about that. The beauty of being older - you can learn something every day and who cares if it's something you've forgotten as many times as you had. ![]()
a7
Thanks for reminding me too.
Hi, @bautic
Welcome to the forum.
You might like to try the NewPing library to do the Ultrasonic work for you.
https://docs.arduino.cc/libraries/newping/
If you are using the IDE 2x then you shold just have to install it.
Tom....
![]()