Skip to content

Commit

Permalink
Improve SRF08 example and fixes calling
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoColomb committed May 13, 2014
1 parent 0c28c01 commit ba54582
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
9 changes: 3 additions & 6 deletions SonarSRF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,10 @@ void SonarSRF::sendCommand(int command, int addressRegister)
{
Wire.write(command); // SRF Command
}
if (_gainRegister)
if (_gainRegister && _rangeLocation)
{
Wire.write(_gainRegister);
}
if (_rangeLocation)
{
Wire.write(_rangeLocation);
Wire.write(_gainRegister); // SRF Location 1
Wire.write(_rangeLocation); // SRF Location 2
}
// end I2C transmission
Wire.endTransmission();
Expand Down
14 changes: 10 additions & 4 deletions examples/SRF08/SRF08.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,34 @@
#define MAIN_08_ADDRESS (0xF8 >> 1)
SonarSRF08 MainSonar;

// Setup Analogue Gain
// http://www.robot-electronics.co.uk/htm/srf08tech.html section "Analogue Gain"
#define GAIN_REGISTER 0x00
// Setup Range Location
// http://www.robot-electronics.co.uk/htm/srf08tech.html section "Changing the Range"
#define LOCATION_REGISTER 0x00

char unit = 'c'; // 'i' for inches, 'c' for centimeters, 'm' for micro-seconds

void setup() {
Serial.begin(9600);

MainSonar.connect(MAIN_08_ADDRESS);
MainSonar.connect(MAIN_08_ADDRESS, GAIN_REGISTER, LOCATION_REGISTER);
isConnected("SRF08", MainSonar.getSoft());
}

void loop() {
float sensorReading = 0;
sensorReading = MainSonar.getRange(unit);
distance("main", sensorReading);
distance("sensor", sensorReading);
}

// Print out distance
void distance(String reference, int sensorReading) {
Serial.print("Distance from " + reference + ": ");
Serial.print(sensorReading);
Serial.print(" ");
Serial.print(unit);
Serial.println();
Serial.println(unit);
}

// Print out distance
Expand Down

0 comments on commit ba54582

Please sign in to comment.