Glendar
1
int trig = 9;
int echo = 6;
float distance = 0;
float value = 0;
void setup() {
pinMode (trig,OUTPUT);
pinMode (echo,INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trig,LOW);
delay(2);
digitalWrite(trig,HIGH);
delay(10);
digitalWrite(trig,LOW);
delay(2);
value = pulseIn(echo,HIGH);
distance = (value*0.0340/2);
Serial.println(distance);
}
What's the mistake I've done on the code it keeps on giving 0 as the result...
Thanks in advance
pulseIn() returns 0 if no echo was seen or if it misses the echo.
Change your "delay()" calls to "delayMicroseconds() calls.
system
3
Uncompiled, untested(but in code tags)
const byte trig = 9;
const byte echo = 6;
void setup()
{
pinMode (trig,OUTPUT);
digitalWrite (trig,LOW);
pinMode (echo,INPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
unsigned long pulseLength = pulseIn(echo,HIGH);
float distance = (pulseLength * 0.0340 / 2);
Serial.println(distance);
}
Glendar
4