Wire.endTransmission() not working??

Hello,

I’m trying to use the Devantech CMPS03 compass sensor… I got this code straight from their website: http://www.robot-electronics.co.uk/htm/cmps3tech.htm. The code looks good as far as I can tell and I’ve read posts from other users saying that this code works for them. BUT… it doesn’t work for me. I’ve been messing around with it for a while now and I know it gets to Wire.endTransmission();. But it never gets past that line.

Can somebody please help me out? (I’m using Windows 7 and Arduino 0018)

/*
CMPS03 with arduino I2C example

This will display a value of 0 - 359 for a full rotation of the compass.

The SDA line is on analog pin 4 of the arduino and is connected to pin 3 of the CMPS03.
The SCL line is on analog pin 5 of the arduino and is conected to pin 2 of the CMPS03.
Both SDA and SCL are also connected to the +5v via a couple of 1k8 resistors.
A switch to callibrate the CMPS03 can be connected between pin 6 of the CMPS03 and the ground.


*/
#include <Wire.h>

#define address 0x60 //defines address of compass

void setup(){
  Wire.begin(); //conects I2C
  Serial.begin(9600);
}

void loop(){
  byte highByte;
  byte lowByte;
  
   Wire.beginTransmission(address);      //starts communication with cmps03
   Wire.send(2);                         //Sends the register we wish to read
   Wire.endTransmission();

   Wire.requestFrom(address, 2);        //requests high byte
   while(Wire.available() < 2);         //while there is a byte to receive
   highByte = Wire.receive();           //reads the byte as an integer
   lowByte = Wire.receive();
   int bearing = ((highByte<<8)+lowByte)/10; 
   
   Serial.println(bearing);
   delay(100);
}

Hi

Is this the problem

while(Wire.available() < 2)[glow]{[/glow]         //while there is a byte to receive
     highByte = Wire.receive();           //reads the byte as an integer
     lowByte = Wire.receive();
     int bearing = ((highByte<<8)+lowByte)/10;
   }

-Fletcher

How is that a problem? The comment is incorrect, but the semi-colon's a good 'un. Less than 3, maybe.

Is it not the case that requestFrom is blocking - ?

  Wire.requestFrom(address, 2);        //requests high byte
  while(Wire.available() < 2);

That is it will not return until the transaction is complete (success or fail). If for some reason it fails - the program will never move past the while loop.

An alternative may be as follows:

  Wire.requestFrom(address, 2);        //requests high byte
  if (Wire.available()>=2) {
     // read data
  }

I really don't think it's even getting that far. I put the line: Serial.println("Made it"); between the Wire.endTransmission and the Wire.requestFrom lines but it never prints anything out. If I just move that line up one, so that it's between Wire.send and Wire.endTransmission, it will print out the message no problem.

I'm just saying I don't thing the problem is the Wire.requestFrom or the while loop.

If it hangs on endTransmission I would recheck wiring.

SDA goes to Arduino analog input 4, SCL to analog input 5 and ground to ground. Add pullup resistors if not provided on the board you interface. One 4k7 resistor from SDA to +5V and another 4k7 resistor from SCL to +5V.

If you get passed endTransmission - you still need to fix the blocking while.

Just rewired it using a different breadboard and its workin fine now. Thanks everyone