BCLab-UNM / SwarmBaseCode-ROS

The base code provided to teams participating in NASA Swarmathon IV.
http://nasaswarmathon.com
MIT License
46 stars 46 forks source link

No need to wait after Wire.requestFrom() and it should not be followed by Wire.endTransmission(). #257

Open Koepel opened 5 years ago

Koepel commented 5 years ago

The Wire library is used in the wrong way in the files:

Explanation: Common-mistakes, number 1 and 2.

After a Wire.requestFrom() there should be no Wire.endTransmission(). After a Wire.requestFrom(), all the waiting may be removed, for example while (Wire.available() < 3); or delay(10);. The waiting with millis() may also be removed. There is not timeout after a Wire.requestFrom(), so the variable did_timeout makes no sense.

It is possible to check if the data was received:

  Wire.requestFrom(address, (byte)6);
  if(Wire.available() != 6)
  {
    i2c_sensor_error = true;   // a variable name that I made up
    return;
  }

That will detect if the sensor was connected or not, but the return value of Wire.endTransmission() could also be used for that.