Hi everyone,
I have been trying to measure the distance between two SRF02 sensors (in Fake Mode).
I have connected two SRF02 sensors on my Arduino by I2C Bus system.
The idea is that one SRF02 should send a signal and the second SRF02 should be waiting for the signal.
The problem is that I get wrong distances as result.
Here is my Code....
#include <Wire.h>
#define srfAddress1 0x70 // Address of the SRF02
#define srfAddress2 0x72 // Address of the SRF02
#define cmdByte 0x00 // Command byte
#define rangeByte 0x02 // Byte for start of ranging data
byte highByte = 0x00; // Stores high byte from ranging
byte lowByte = 0x00; // Stored low byte from ranging
int range = 0;
int rangeData;
void setup()
{
Serial.begin(9600);
Wire.begin();
}
void loop()
{
rangeData=getRange();
}
int getRange()
{
//Sensor 1 –Sender-
Wire.beginTransmission(srfAddress1); // Start communticating with SRF02
Wire.write((uint8_t)cmdByte); // Send Command Byte
Wire.write((uint8_t)0x5C); // Transmit an 8 cycle 40khz burst - no ranging takes place
Wire.endTransmission();
//Sensor 2 –Empfänger-
Wire.beginTransmission(srfAddress2); // Start communticating with SRF02
Wire.write((uint8_t)cmdByte); // Send Command Byte
Wire.write((uint8_t)0x57); // Fake Ranging Mode - Result in centimeters
Wire.endTransmission();
delay(100);
Wire.beginTransmission(srfAddress2); // start communicating with SRFmodule
Wire.write((uint8_t)rangeByte); // Call the register for start of ranging data
Wire.endTransmission();
Wire.requestFrom(srfAddress2, 2); // Request 2 bytes from SRF module
while(Wire.available() < 2); // Wait for data to arrive
highByte = Wire.read(); // Get high byte
lowByte = Wire.read(); // Get low byte
range = (highByte << 8 ) + lowByte; // Put them together
//print it
Serial.print("Entfernung zwischen 1&2: ");
Serial.println(range, DEC);
delay(1500);
return(range); // Returns Range
}