I changed a bit the code. I call "Sensor2" (receive the signal) before "Sensor1" (transmit the signal).
If i call Sensor2 after Sensor1 it could be that the Signal is just gone. That was my intention.
#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() // This function gets a ranging from the SRF02
{
//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();
//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-
delay(100); // Wait for ranging to be complete
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
}