LowPowerLab Forum

Hardware support => Moteino => Topic started by: donaldhwong on March 28, 2016, 10:50:27 AM

Title: Wire Command issue
Post by: donaldhwong on March 28, 2016, 10:50:27 AM
Dear All,

The following code runs okay for several minutes.  Then after the Wire.endTransmission returns "!0" once, the next loop, it hangs at the Wire.endTransmission...  Can anyone help me out?  Thanks..

bool getCompassRaw()
{
  Wire.beginTransmission(0x1e);
  Serial.print(" Wire.beginTransmittion.");
  Wire.write(0x03); //select register 3, X MSB register - to start read from
  Serial.print(" Wire.write.");

  if (Wire.endTransmission() ==0) {
    Serial.print(" Wire.endTransmission. ");

    if (6 <= Wire.requestFrom(0x1e, 6)) {
    Serial.print(" Wire.requestFrom. ");
    if (6 <= Wire.available()) {

        //Read data from each axis, 2 registers per axis
        for (int i = 0; i < 3; i++) {
          compassNow[i] = Wire.read() << 8; //X msb
          compassNow[i] |= Wire.read(); //X lsb
        }
        Serial.print(" Wire.read. ");

        return true;
      }
    }
  } 
  return false;
}

Title: Re: Wire Command issue
Post by: syrinxtech on March 28, 2016, 11:01:56 AM
Can you post the whole section of code from the getCompassRaw() function? 

Something looks a little odd about your braces but I can't tell because some of the code is missing.
Title: Re: Wire Command issue
Post by: donaldhwong on March 28, 2016, 11:17:29 AM
Thank you, Syrinxtech,

Reposted the code just now.
Title: Re: Wire Command issue
Post by: syrinxtech on March 28, 2016, 11:34:17 AM
I don't usually code in "raw" I2C....but it seems to me from reading your code that the following is happening:

1.  You're calling Wire.endTransmission() and it's returning something other than 0.
2.  The next thing you do is try a Wire.requestFrom().  But, if the connection was closed then you are trying to read from a closed connection and it seems like that would hang you up.

Can you try printing out the return values from the functions that provide them to see exactly what's going wrong?

For example, the following all return values:

1.  Wire.endTransmission()
2.  Wire.requestFrom()
3.  Wire.available()
4.  Wire.read()

Somewhere in there it looks like you're trying to read from a closed connection.
Title: Re: Wire Command issue
Post by: donaldhwong on March 28, 2016, 12:25:32 PM
At the stage of endTransmission.  I am getting Error #4: Other error.

My code can ignore this and initiate a new loop.  But it can skip only once.  When a new loop Wire.Begin, it hangs at end.Transmission....  without getting any result.

Radio Begin.   Radio In
Radio Begin.   Radio In
GPS Begin.   GPS In
Compass Raw.   begin. write. endTransmission:0 requestFrom:6
Available:6 Wire.read. 175
Wire.read. 490
Wire.read. 217
Adjust Begin. Adjust Done.   Compass In

Key:x State:I Distance:3.53 Speed:42 Heading:-104.93 Bearing:48.31  (Above is the normal running with right feedback)

Radio Begin.   Radio In
Radio Begin.   Radio In
GPS Begin.   GPS In
Compass Raw.   begin. write. endTransmission:4 (In this cycle, it ignored and initiate a new loop)
Radio Begin.   Radio In
Radio Begin.   Radio In
Radio Begin.   Radio In
Radio Begin.   Radio In
GPS Begin.   GPS In
Compass Raw.   begin. write.   (This is where it hangs)