I have an ultra-sonic sonar responding (when asked) with a HIGH pulse which length is proportional to the distance to the object. The problem is that I can't get pulseIn working proprely.
Here is my very simplified code, always displaying "0". I have connected pin 6 and 7 with a wire (emulating a valid response) :
const int sonarPing = 6;
const int sonarPong = 7;
void setup() {
Serial.begin(57600);
// Sonar
pinMode(sonarPing, OUTPUT);
pinMode(sonarPong, INPUT);
}
void loop() {
digitalWrite(sonarPing, LOW); // Should be already to LOW
delay(5);
Serial.println(pulseIn(sonarPong, HIGH));
delay(5);
digitalWrite(sonarPing, HIGH);
delay(5);
digitalWrite(sonarPing, LOW);
}
I can't get it working. Do you have a working code to demo pulseIn, using only the Arduino board ?
You haven't told us which device you're using, but usually you ping the transmit pin and only then do the pulseIn.
You're doing the pulseIn in the middle of the transmit pulse.
Unfortunately, the Arduino doesn't do that kind of concurrent operation.
Your pulseIn will sit waiting for a low to high transition that can never come, because you only write the HIGH after pulseIn exits.
If you want to test pulseIn, I suggest you use something like the servo library or a PWM pin.
OK, I understand why my real-life tests don't work.
I'll try to change my code and see if my sonar is working properly. The ping pin is PWM enabled, it might be faster than I was expecting at first thought
The ping pin is PWM enabled, it might be faster than I was expecting at first thought
I don't know what you mean by that, but you should understand that sonars (like radar) have a maximum pulse repetition rate, beyond which they will either not work, or return garbage results, because there are still returns in flight back to the receiver.