I'm new to Arduino and I'm doing an ultrasonic project where I test for the speed of sound. My code is below and for some reason, this is what the serial port returns:
This is the measured speed of sound: inf m/s This is the measured speed of sound: 0m/s This is the measured speed of sound: 0m/s etc...
Here's my code. Any help's massively appreciated:)
int trig = 13;
int echo = 11;
float target_distance = 0.3;
float recieved;
float speed_of_sound;
void setup() {
Serial.begin(9600);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
}
void loop() {
digitalWrite(trig, LOW);
delayMicroseconds(2000);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
recieved=pulseIn(echo, HIGH);
recieved = recieved*1000000.0;
speed_of_sound = (2*target_distance)/recieved;
Serial.print("This is the measured speed of sound: ");
Serial.print (speed_of_sound);
Serial.println("m/s");
delay(3000);
}