I’ve tried to alter it to give the following, but I’m getting the error “34: error: call of overloaded ‘write(int)’ is ambiguous”. I can’t work out where abouts the error is coming from.
#include <Wire.h>
#define srfAddress (0x70) //USV device address
#define cmdByte (0x00) //Command byte
#define rangeByte (0x02)
//SETUP
//-----------------------------------------------------------------------------
byte highByte = 0x00; // Stores high byte from ranging
byte lowByte = 0x00; // Stored low byte from ranging
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
delay(1000);
}
//MAIN PROGRAM
//----------------------------------------------------------------------
void loop()
{
int rangeData = getRange();
Serial.println(rangeData, DEC);
}
int getRange(){ // This function gets a ranging from the SRF08
int range = 0;
Wire.beginTransmission(srfAddress); // Start communticating with SRF08
Wire.write(cmdByte); // Send Command Byte
Wire.write(0x51); // Send 0x51 to start a ranging
Wire.endTransmission();
delay(100); // Wait for ranging to be complete
Wire.beginTransmission(srfAddress); // start communicating with SRFmodule
Wire.write(rangeByte); // Call the register for start of ranging data
Wire.endTransmission();
Wire.requestFrom(srfAddress, 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
return(range); // Returns Range
}
Thank you Nick I’ve updated the program with your suggestion and it compiles now. For some reason it’s not sending data; I’ve added in some text at different stages of the loop like so:
#include <Wire.h>
#define srfAddress (0x70) //USV device address
const byte cmdByte = 0x00; //Command byte
#define rangeByte (0x02)
//SETUP
//-----------------------------------------------------------------------------
byte highByte = 0x00; // Stores high byte from ranging
byte lowByte = 0x00; // Stored low byte from ranging
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
delay(1000);
}
//MAIN PROGRAM
//----------------------------------------------------------------------
void loop()
{
Serial.println("Program starting");
int rangeData = getRange();
}
int getRange(){ // This function gets a ranging from the SRF08
Serial.println("starting getRange");
int range = 0;
Wire.beginTransmission(srfAddress); // Start communticating with SRF08
Wire.write(cmdByte); // Send Command Byte
Wire.write(0x51); // Send 0x51 to start a ranging
Wire.endTransmission();
Serial.println("requested range sample");
delay(100); // Wait for ranging to be complete
Wire.beginTransmission(srfAddress); // start communicating with SRFmodule
Wire.write(rangeByte); // Call the register for start of ranging data
Wire.endTransmission();
Serial.println("requesting range data");
Wire.requestFrom(srfAddress, 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
Serial.println("finishing range data transmission");
range = (highByte << 8) + lowByte; // Put them together
Serial.println(range);
return(range); // Returns Range
}
The serial monitor shows up to: Serial.println("requesting range data");
Does that mean that I’m correct in saying that when trying to exectute this:
Wire.requestFrom(srfAddress, 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();
the program must get stuck because Wire never becomes available? i.e something wrong with the wiring or addressing of my sensor?
Wire.requestFrom(srfAddress, 2); // Request 2 bytes from SRF module
while(Wire.available() < 2); // Wait for data to arrive
requestFrom “blocks” until the requested number of bytes arrives, or it times out. So the second line achieves nothing except to potentially put it into a loop. In fact requestFrom returns the number of bytes. So a better plan would be:
if (Wire.requestFrom(srfAddress, 2) != 2)
{
// some sort of error
}
else
{
highByte = Wire.read(); // Get high byte
lowByte = Wire.read();
}
Try the “I2C bus scanner” on this page. That will confirm whether or not the device is responding and if so, what address it has:
Thanks Nick, I now have a fully functional sensor 8) The article on your website about the pullup resistor values also answered alot of questions that were in the back of my mind about what effect different values had on the signal :)