I'm using 2 SRF02 sensors on the same Arduino board to attempt to have the transmitter only pulse the 40 KHz wave, while the receiver will be only taking a fake range, i.e. picking up a reading based on another transmission.
I've been using SoftwareSerial and need to know how to synchronize the time for the receiver to know when the transmitter sent the data. What I found is that I'm going to have to do this using start and stop bytes to set them in the same cycle.
How can I do this?
This is my current 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)
delayMicroseconds(100);
//serial data is fixed at 9600,N,8,2,so we need some time to create the second stop bit
mySerial.write(cmd);
//send the command to SRF02
delayMicroseconds(100);
//serial data is fixed at 9600,N,8,2,so we need some time to create the second stop bit
}
void setup(void) {
Serial.begin(9600);
mySerial.begin(9600);
Serial.println("SRF02 TEST!");
}
void loop(void) {
unsigned int reading;
SendCmd(0x00,0x5C);
//Transmit 8 cycle 40khz pulse from address 0x00 - transmitter
delay(70);
//time for SRF02 to measure the range
SendCmd(0x02,0x57);
//Get Fake Range at Address 0x02 - receiver
delay(10);
//wait for some time,let the Arduino receive 2 bytes data from the TX pin of SRF02
if(mySerial.available()>=2) {
//if two bytes were received
reading = mySerial.read()<<8;
//receive high byte (overwrites previous reading) and shift high byte to be high 8 bits
reading |= mySerial.read();
// receive low byte as lower 8 bits
Serial.print(reading);
// print the reading
Serial.println("cm");
//Serial.println("Time Sent: ");
//Serial.print(T_sent);
}
delay(250);
// wait a bit since people have to read the output :)
}