Hi all
I am making a small robot bug, got H-bridges and two motors connected. These are function and I can control them well from arduino. Now I added 3 distance sensors one facing ahead and 1 each facing 30degrees to left and one to 30d right. These diagonal sensors are to assess the clearance not only directly ahead but also to the sides a bit since the bug is wide and won’t fit in the narrow strip of clearance monitored by the front facing sensor alone.
Now I wired them with gnd to gnd, vcc to 5v on arduino board. Each echo pin returns to a different pin on arduino nano. However in order to save pins, I joined the trigger pins of all three sensors and connected them to one digital pin, so even if I only read one sensor at a time, all three are triggered in effect each time.
They seem to work ok for the most part but randomly one or the other gives a distance of 0! I will enclose the relevant[?] code below. I looked at all the soldering, continuity, solder bridges, loose connects etc. For my life I can not predict a pattern.
So I guess my question is did anyone else try triggering multiple HCSR04 distance sensors from a single digital pin and if so did you notice any madness / what helped.
Thanks folks
#include "seaweedlib.h"
struct cmove cmoves;
int fwclears[3];
int fwclear=0;
int triggers[]={9,9,9};
int echos[]={10,12,11};
//left , front, right
void mondist(){
for (int n=0;n<(sizeof(fwclears)/sizeof(int));n++) {
fwclears[n]=getdist(triggers[n],echos[n]) ;
}
}
int getdist(int trig, int echo){ //get distance from one sensor
long dist, duration;
digitalWrite(trig,LOW);
delayMicroseconds(2);
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
duration=pulseIn(echo,HIGH);
dist=(duration/2)/29.1;
return dist;
}