Hello all,
I am trying to make a robot that will follow a person around using the hc-sr04 ultrasonic sensor. I have 3 hc-sr04's and 2 Arduino Uno's. At the moment I am not going to worry about motors so I am not listing them. 2 receiver sensors are connected to one Arduino and the other transmitter sensor is connected to the other Arduino. I saw this code that I modified (credit to element14 presents - YouTube) and am having some trouble with it. This is the code for the robot:
boolean oneFire = false;
boolean twoFire = false;
boolean finished = false;
long setPoint1 = 0;
long setPoint2 = 0;
long difference = 0;
const int echoPin1 = 7;
const int echoPin2 = 6;
void setup(){
pinMode(echoPin1, INPUT);
pinMode(echoPin2, INPUT);
Serial.begin(9600);
}
void loop(){
long timeout = 0;
long distance = 0;
oneFire = false;
twoFire = false;
finished = false;
setPoint1 = 0;
setPoint2 = 0;
difference = 0;
while ((oneFire == 0) || (twoFire == 0)){
if (((PIND & 0x10) == 0) && !oneFire){
oneFire = true;
setPoint1 = timeout;
}
if (((PIND & 0x20) == 0) && !twoFire){
twoFire = true;
setPoint2 = timeout;
}
delayMicroseconds(1);
timeout++;
if (timeout > 19000)
break;
}
difference = setPoint1 - setPoint2;
distance = (setPoint1 + setPoint2)/10;
Serial.println(difference);
}
The code for what the human wears is simply:
const int trigPin = 12;
void setup()
{
pinMode(trigPin, OUTPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
}
Again, I am not worrying about motors because once I get valid readings, The motor part will be easy. When I upload both, point them at each other, and start the serial monitor, all I get is 0. Any help is welcome.
Thanks