hi all
at first I am sorry for my weak language ..
I am using car parking piezo electric sensors for distance measurements ..
I have made a Transmitting and receiving circuit that can sense reflected Echo up to 3 meters ...
I am using two different sensors one for transmitting and one for receiving ...
I am getting 5 v pulse when any reflected signal received ..
all what I need is to calculate the time between transmitting and receiving signals to get the distance...
I am using Tone() code to generate 40 khz signal to drive the transmitter transistor.
I have tried to write a code for that but when echo received I got almost the same result whatever the distance is ...
I always get values like 4 , 8, 12 almost in a random manner using macros() ...
I have attached a buzzer into Echo pin to hear and test the reflected echo and I have got very clear buzzer pulse ..
also there is no noise in the circuit I mean when there is no Echo,, the Echo Pin digital read is always 0
this is my code
any help will be greatly appreciated ...
float startt, endd;
void setup() {
pinMode(3, OUTPUT); // 40 khz pin
pinMode(A0, INPUT); // Echo Pin
Serial.begin(9600);
}
void loop(){
// Transmitting 40khz signal
digitalWrite(3, LOW);
tone(3,40000);
delay(50);
noTone(3);
startt = micros(); // record transmitting pulse time
// Waiting for the reflected Echo.
if (digitalRead(A0) == HIGH) {
endd= micros(); // record received Echo time.
Serial.println(endd-startt);
delay(100);
}
else {
Serial.println("No Signal Received");
}
}