Problem
I've been trying to a create a receiver and transmitter beacon for a following robot, but have not been able to sync my two SRF02s to one another.
Any advice?
Code
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11); // RX, TX
void SendCmd(unsigned char address,unsigned char cmd)
{
mySerial.write(address);//set the address of SRF02(factory default is 0)
mySerial.write(cmd);//send the command to SRF02
}
void setup(void)
{
Serial.begin(9600);
mySerial.begin(9600);
Serial.println("SRF02 TEST!");
}
void loop(void)
{
unsigned long t3;
unsigned long t1=micros();//time tx command issued
SendCmd(1, 0x5C);//send pulse with transmitter
unsigned long t2=micros();//time rx command issued
SendCmd(2, 0x5B);//listen for pulse returns results in microsconds
while(mySerial.available() < 2)//if two bytes were received
{}
t3 = mySerial.read()<<8;//receive high byte (overwrites previous reading) and shift high byte to be high 8 bits
t3 |= mySerial.read();// receive low byte as lower 8 bits
unsigned long Tm = t3 + t2 - t1;//total amt of microseconds between pulse and reading
unsigned long Dist = (Tm/29); //converting microseconds to cm
Serial.print(t3); // print the reading
Serial.println("cm");
//Serial.print(tm);
//Serial.println("ms");
delay(250); // wait to read the output
}

t1, t2, t3as well asDist– James Waldby - jwpat7 Nov 29 '15 at 00:57