Wire.h won't compile

While trying to compile the following program sketch to run a Digital Compass (HMC5983, GY-282) I get errors as follows;

Program Code Listing

/*
An Arduino code example for interfacing with the HMC5883

by: Jordan McConnell
 SparkFun Electronics
 created on: 6/30/11
 license: OSHW 1.0, http://freedomdefined.org/OSHW

Analog input 4 I2C SDA
Analog input 5 I2C SCL
*/

#include <Wire.h> //I2C Arduino Library

#define address 0x1E //0011110b, I2C 7bit address of HMC5883

void setup(){
  //Initialize Serial and I2C communications
  Serial.begin(9600);
  Wire.begin();
  
  //Put the HMC5883 IC into the correct operating mode
  Wire.beginTransmission(address); //open communication with HMC5883
  Wire.send(0x02); //select mode register
  Wire.send(0x00); //continuous measurement mode
  Wire.endTransmission();
}

void loop(){
  
  int x,y,z; //triple axis data

  //Tell the HMC5883 where to begin reading data
  Wire.beginTransmission(address);
  Wire.send(0x03); //select register 3, X MSB register
  Wire.endTransmission();
  
 
 //Read data from each axis, 2 registers per axis
  Wire.requestFrom(address, 6);
  if(6<=Wire.available()){
    x = Wire.receive()<<8; //X msb
    x |= Wire.receive(); //X lsb
    z = Wire.receive()<<8; //Z msb
    z |= Wire.receive(); //Z lsb
    y = Wire.receive()<<8; //Y msb
    y |= Wire.receive(); //Y lsb
  }
  
  //Print out values of each axis
  Serial.print("x: ");
  Serial.print(x);
  Serial.print("  y: ");
  Serial.print(y);
  Serial.print("  z: ");
  Serial.println(z);
  
  delay(200);
}

Error Messages Listing

CompassTest1.ino: In function 'void setup()':
CompassTest1.ino:24:8: error: 'class TwoWire' has no member named 'send'
CompassTest1.ino:25:8: error: 'class TwoWire' has no member named 'send'
CompassTest1.ino: In function 'void loop()':
CompassTest1.ino:35:8: error: 'class TwoWire' has no member named 'send'
CompassTest1.ino:42:14: error: 'class TwoWire' has no member named 'receive'
CompassTest1.ino:43:15: error: 'class TwoWire' has no member named 'receive'
CompassTest1.ino:44:14: error: 'class TwoWire' has no member named 'receive'
CompassTest1.ino:45:15: error: 'class TwoWire' has no member named 'receive'
CompassTest1.ino:46:14: error: 'class TwoWire' has no member named 'receive'
CompassTest1.ino:47:15: error: 'class TwoWire' has no member named 'receive'
Error compiling.

Clearly the wire.h file is not finding the TwoWire.send and the TwoWire.receive class because it has not been declared.
I notice that Jordan McConnell has updated the wire.cpp file but I can not find the updated wire.h file.
Can soneone please point me to the updated (and companion) wire.h file.

Thanking you in advance for your assistance here.

Sciencez:
Thanking you in advance for your assistance here.

You got the code from a computer museum?

Either you downgrade your Arduino IDE to a version number less than 1.0.

Or you replace Wire.receive(); with Wire.read(); and Wire.send(xxx); with Wire.write(xxx);

1 Like

Can soneone please point me to the updated (and companion) wire.h file.

Why don't you just change your code to match the methods that the Wire library, delivered with the IDE, actually has?

jurs:
You got the code from a computer museum?

Either you downgrade your Arduino IDE to a version number less than 1.0.

Or you replace Wire.receive(); with Wire.read(); and Wire.send(xxx); with Wire.write(xxx);

Jurs,

I did not get the code "... from a computer museum..", - you will notice that it was written by Jordan McConnell of SparkFun Electronics - he's employed as a professional coder, so I assumed that he'd have addressed all of these issues.

To address your other suggestion when I looked in the *.h and *.cpp files I did not see Wire.send and Wire.write but I'll look again before I comment further. Thnak you for the suggestion.

PaulS:
Why don't you just change your code to match the methods that the Wire library, delivered with the IDE, actually has?

PaulS,

Very good idea (and logical, of course!), - I'd already looked at the Wire.h and Wire.cpp files (before asking for assistance) but couldn't see the matching methods, - can you tell me what they are, as it seems you know the wire.* files better than I do.

Regards,

Sciencez

/* Or you replace Wire.receive with Wire.read and Wire.send with Wire.write as jurs suggested below*/

#include <Wire.h> //I2C Arduino Library

#define address 0x1E //0011110b, I2C 7bit address of HMC5883

void setup(){
  //Initialize Serial and I2C communications
  Serial.begin(9600);
  Wire.begin();
  
  //Put the HMC5883 IC into the correct operating mode
  Wire.beginTransmission(address); //open communication with HMC5883
  Wire.send(0x02); //select mode register
  Wire.send(0x00); //continuous measurement mode
  Wire.endTransmission();
}

void loop(){
  
  int x,y,z; //triple axis data

  //Tell the HMC5883 where to begin reading data
  Wire.beginTransmission(address);
  Wire.send(0x03); //select register 3, X MSB register
  Wire.endTransmission();
  
 
 //Read data from each axis, 2 registers per axis
  Wire.requestFrom(address, 6);
  if(6<=Wire.available()){
    x = Wire.receive()<<8; //X msb
    x |= Wire.receive(); //X lsb
    z = Wire.receive()<<8; //Z msb
    z |= Wire.receive(); //Z lsb
    y = Wire.receive()<<8; //Y msb
    y |= Wire.receive(); //Y lsb
  }
  
  //Print out values of each axis
  Serial.print("x: ");
  Serial.print(x);
  Serial.print("  y: ");
  Serial.print(y);
  Serial.print("  z: ");
  Serial.println(z);
  
  delay(200);
}

I did not get the code "... from a computer museum..", - you will notice that it was written by Jordan McConnell of SparkFun Electronics - he's employed as a professional coder, so I assumed that he'd have addressed all of these issues.

The code worked at the time it was written. Why would you expect that it would be maintained through all the versions of the IDE?

Sciencez:
I did not get the code "... from a computer museum..", - you will notice that it was written by Jordan McConnell of SparkFun Electronics - he's employed as a professional coder, so I assumed that he'd have addressed all of these issues.

Look at the date when the code was written: It was written before Arduino 1.0 was out.

In ancient times Arduino IDE versions started with a double zero like 0017, 0018, 0019, 0020, 0021, 0022.

The code you posted is a prehistoric relict from that time for one of the Arduino double-zero versions.

I suppose one year in the Arduino life is to be calculated like dog years:

1 year in human life is 7 years of Arduino development.

So just four years of code age seem to be just a little for you, but its a very long time in Arduino years!

You cannot expect everything to be fully compatible 50 versions of the Arduino IDE later.
:smiling_imp:

Jurs,

The code should not be that old as the sensor (i.e. HMC5883) is quite recent.

I can only assume it was modified from the previous sensor (i.e. HMC5883) to accomodate the new sensor (i.e. HMC5883) and was not properly tested.

Unfortunately computer manufacturers have an annoying habit of improving1 things. They think that "write" is better than "send" so they change the library. Meanwhile lots of tutorials (such as ones I wrote) and other code now fails when people "upgrade" to the new system.


  1. Definition of upgrade: Swap your old bugs for new ones.

Nick,

Thank you for your post. Like you I've been at this for some time (i.e. programming etc) so I deeply apprecaite your comemnts and I had a very good laugh at your Definition, (i.e. "Definition of upgrade: Swap your old bugs for new ones"), - never a more true word was spoken !

Thank you to everyone who assisted me - it works !!!!!!!!!!!!!!!