Wire library in Arduino 0012 vs Arduino 0015

hi all:

Some code I was using in Arduino 0012 appears broken in Arduino 0015. Tracing things it seems that the Wire library functions in 0012 that talk to the HMC6532 compass module no longer work in 0015. Using a Duemilanove/168 in both cases. Reprogramming using 0012, everything works perfectly.

Software is not exactly my strong point- but is there a simple answer to why the above might happen? The code reads the compass by 2Wire and sends a position command to a Parallax servo controller.

Daniel

/* 
HMC6532 compass to servo test
With 2Wire HMC6532 code by vaibhav bhawsar 
http://recombine.net/blog/article/49/hmc6352-sparkfun-compass-and-arduino)
and Parallax servo code by voidnoise
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1201077061

HMC6532 connections: 
SDA to Analog pin 4 and SCL to analog pin 5
No pullup resistors on SDA and SCL
Using 3v instead of 5v to the Vcc of the compass module.

*/

// include the SoftwareSerial library so you can use its functions:
#include <SoftwareSerial.h>
#define rxPin 8
#define txPin 9
// set up a new serial port
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

// include the Wire library to read from the compass
#include <Wire.h>
int HMC6352Address = 0x42;
// This is calculated in the setup() function
int slaveAddress;
byte headingData[2];
int i, headingValue;



void setup()
{
// Shift the device's documented slave address (0x42) 1 bit right
// This compensates for how the TWI library only wants the
// 7 most significant bits (with the high bit padded with 0)
slaveAddress = HMC6352Address >> 1;   // This results in 0x21 as the address to pass to TWI
Serial.begin(115200);
  Serial.println("serial started: ");
Wire.begin();

 // define pin modes for tx, rx:
 pinMode(rxPin, INPUT);
 pinMode(txPin, OUTPUT);
 // set the data rate for the SoftwareSerial port
 mySerial.begin(2400);

  changeServo(0, 50);
  delay(500);
  changeServo(0, 100);
}


void loop()
{
  // Send a "A" command to the HMC6352
  // This requests the current heading data
  Wire.beginTransmission(slaveAddress);
  Wire.send("A");              // The "Get Data" command
  Wire.endTransmission();
  delay(10);                   // The HMC6352 needs at least a 70us (microsecond) delay
  // after this command.  Using 10ms just makes it safe
  // Read the 2 heading bytes, MSB first
  // The resulting 16bit word is the compass heading in 10th's of a degree
  // For example: a heading of 1345 would be 134.5 degrees
  Wire.requestFrom(slaveAddress, 2);        // Request the 2 byte heading (MSB comes first)
  i = 0;
  while(Wire.available() && i < 2)
  { 
    headingData[i] = Wire.receive();
    i++;
  }
  headingValue = headingData[0]*256 + headingData[1];  // Put the MSB and LSB together

  Serial.print("Current heading: ");
  Serial.print(int (headingValue / 10));     // The whole number part of the heading
  Serial.print(".");
  Serial.print(int (headingValue % 10));     // The fractional part of the heading
  Serial.println(" degrees");
  delay(200);

  headingValue= headingValue/4;    //compensate for limited servo rotation angle
  changeServo(0, headingValue);
//  delay(150);      // try to get rid of jittering

}

int changeServo(int chan, int posi){
 // position is expressed in degrees: 0-180
 // then converted to 300-1250 range
int pw = posi;
pw= (pw * 5) + 300;
byte lsb = pw;
byte msb =  pw >> 8;
int ramp = 0;
 
 
   mySerial.print(33, BYTE);  //!
   mySerial.print(83, BYTE);  //S
   mySerial.print(67, BYTE);  //C
   mySerial.print(chan, BYTE);  //channel
   mySerial.print(ramp, BYTE);  //rampspeed
   mySerial.print(lsb, BYTE);  //LSB
   mySerial.print(msb, BYTE);  //MSB
   mySerial.print(13, BYTE);
}

“No longer works” isn’t much to go on… what acts differently? Can you compile? Can you upload? Can it get any data at all?

In 0012 and 0015, compile, upload, and serial print statements, software serial work perfectly

In 0012, the Wire code quoted below works as well, but in 0015 it seems to hang when it hits this code. Is there some known incompatibility between the Wire library in 0012 and 0015? (EDIT:file compare in BBedit says wire.h libraries in 0012 and 0015 are identical. Hmm....)

Thanks

D

 // Send a "A" command to the HMC6352
  // This requests the current heading data
  Wire.beginTransmission(slaveAddress);
  Wire.send("A");              // The "Get Data" command
  Wire.endTransmission();
  delay(10);                   // The HMC6352 needs at least a 70us (microsecond) delay

A little more snooping shows the Wire library folders in the two versions are identical, save for a line in twi.c in 0015 that adds ATMEGA328 compatibility.

So basically the Wire code above works in 0012 but not 0015....

D

I would make sure to delete all .o files in the Wire library area and in your sketch/build area, and recompile it all.

Thanks Ed:

I tried deleting the applet folder in the build area, restarting 0015 and recompiling/upload. No action in 0015, but going back to 0012 it works.

Daniel

I tried the in-between versions, deleting the build folder before starting each version:

Arduino 0012: works perfectly, compass responds Arduino 0013: works perfectly, compass responds Arduino 0014: works perfectly, compass responds Arduino 0015: execution hangs around the Wire code above; compass does not respond

Daniel

Just tried the above code on an Arduino MEGA w/Arduino 0015 and it works- the Wire library is able to get a heading from the compass.

There's a hardware difference though: in 0012 to 0014 on a Duemilanove, to make it work the the compass' SDA & SCL pins go to Analog 4 & 5, while on the Mega the compass SDA/SCL pins go to the Mega pins marked SDA/SCL (20 & 21). So the updated list of what works and what doesn't is:

Duemilanove with SDA/SCL on pins 4/5 Arduino 0012: works perfectly, compass responds Arduino 0013: works perfectly, compass responds Arduino 0014: works perfectly, compass responds Arduino 0015: execution hangs around the Wire code above; compass does not respond

MEGA with SDA/SCL on pins 20/21 Arduino 0015: works perfectly, compass responds

Daniel

OK scratch all the above, it works in 0015 now on a Duemilanove.

Weird, but it started working right after I added pullup resistors on the SDA and SCL. And kept working after I removed them. I might have had a wiring problem that I did not notice, although that would not explain why it would work flawlessly with 0012/13/14 and not 15 for so many tries.

D