I bought it and it was workin gfine with the code and after inserting it in my project , when i checked it shows large negative values, I tired changing pins , but it doesnt work shows values like 3315,3325 random numbers
I dont know whether there is a problem with my sensor or my code is wrong. Really worried please help guys.
CODE
#define trigpin 3
#define echopin 2
void setup()
{
Serial.begin(9600);
pinMode(trigpin,OUTPUT);
pinMode(echopin, INPUT);
}
void loop()
{
int distance, duration; //I even tried putting long instead of int
digitalWrite(trigpin,HIGH);
delayMicroseconds(1000);
digitalWrite(trigpin,LOW);
duration =pulseIn(echopin,HIGH);
distance = (duration/2)/29.9;
Serial.println(distance);
}
Please help me as fast as you can i have no idea what to do and my deadline is nearing
my code now it shows values from 1000
#define trigpin 3
#define echopin 2
void setup()
{
Serial.begin(9600);
pinMode(trigpin,OUTPUT);
pinMode(echopin, INPUT);
}
void loop()
{
unsigned int distance, duration; //I even tried putting long instead of int
digitalWrite(trigpin,HIGH);
delayMicroseconds(10);
digitalWrite(trigpin,LOW);
duration =pulseIn(echopin,HIGH);
distance = (duration/60);
while (digitalRead (echopin) == LOW)
{
Serial.println(“echo low”);
}
Serial.println(distance);
delay(500);
}
it prints echo low
echo low
.
.
my code now it shows values from 1000
#define trigpin 3
#define echopin 2
void setup()
{
Serial.begin(9600);
pinMode(trigpin,OUTPUT);
pinMode(echopin, INPUT);
}
void loop()
{
unsigned int distance, duration; //I even tried putting long instead of int
digitalWrite(trigpin,HIGH);
delayMicroseconds(10);
digitalWrite(trigpin,LOW);
duration =pulseIn(echopin,HIGH);
distance = (duration/60);
while (digitalRead (echopin) == LOW)
{
Serial.println(“echo low”);
break;
}
Serial.println(distance);
delay(500);
}
it prints echo low
echo low
random numbers
echo low
random numbers
.
.
Try this
Comment out the pulsein line as well as the distance cal line and use the while loop to test the echo. Take the break from the while. If it never gets out of the loop, then you may have a defective sr04.
Please show your setup. Code
Please show your setup. Code is nothing w/o setup.