Quantcast
Channel: Intel Communities: Message List
Viewing all articles
Browse latest Browse all 17819

Is the wire library for galileo the same as arduino?

$
0
0

Hi,

 

Does the wire.h library for the Galileo work exactly the same as the Arduino wire.h library?

 

I'm using some Arduino code that depends on the wire library to talk date in from a sensor via I2C.

 

But I'm getting an error "'class TwoWire' has no member named 'send'" and "'class TwoWire' has no member named 'receive'.

 

Thanks

Tim

 

 

#include <Wire.h>

// We will be using the I2C hardware interface on the Arduino in
// combination with the built-in Wire library to interface.

// Arduino analog input 5 - I2C SCL
// Arduino analog input 4 - I2C SDA

/ In this example we will do a basic read of the CO2 value and checksum verification.
 For more advanced applications please see the I2C Comm guide.
*/

int co2Addr = 0x68;
// This is the default address of the CO2 sensor, 7bits shifted left.

void setup() {
 Serial.begin(9600);
 Wire.begin ();
 pinMode(13, OUTPUT); // We will use this pin as a read-indicator
 Serial.println("What a wonderful day, to read atmospheric CO2 concentrations!");
}


///////////////////////////////////////////////////////////////////
// Function : int readCO2()
// Returns : CO2 Value upon success, 0 upon checksum failure
// Assumes : - Wire library has been imported successfully.
// - LED is connected to IO pin 13
// - CO2 sensor address is defined in co2_addr
///////////////////////////////////////////////////////////////////
int readCO2()
{
 int co2_value = 0;
 // We will store the CO2 value inside this variable.

 digitalWrite(13, HIGH);
 // On most Arduino platforms this pin is used as an indicator light.

 //////////////////////////
 /* Begin Write Sequence */
 //////////////////////////

 Wire.beginTransmission(co2Addr);
 Wire.send(0x22);
 Wire.send(0x00);
 Wire.send(0x08);
 Wire.send(0x2A);
 Wire.endTransmission();

 /////////////////////////
 /* End Write Sequence. */
 /////////////////////////

 /*
 We wait 10ms for the sensor to process our command.
 The sensors's primary duties are to accurately
 measure CO2 values. Waiting 10ms will ensure the
 data is properly written to RAM

 delay(10);

 /////////////////////////
 /* Begin Read Sequence */
 /////////////////////////

 /*
 Since we requested 2 bytes from the sensor we must
 read in 4 bytes. This includes the payload, checksum,
 and command status byte.

 */

 Wire.requestFrom(co2Addr, 4);

 byte i = 0;
 byte buffer[4] = {0, 0, 0, 0};

 /*
 Wire.available() is not nessessary. Implementation is obscure but we leave
 it in here for portability and to future proof our code
 */
 while(Wire.available())
 {
 buffer[i] = Wire.receive();
 i++;
 }

 ///////////////////////
 /* End Read Sequence */
 ///////////////////////

 /*
 Using some bitwise manipulation we will shift our buffer
 into an integer for general consumption
 */

 co2_value = 0;
 co2_value |= buffer[1] & 0xFF;
 co2_value = co2_value << 8;
 co2_value |= buffer[2] & 0xFF;


 byte sum = 0; //Checksum Byte
 sum = buffer[0] + buffer[1] + buffer[2]; //Byte addition utilizes overflow
 if(sum == buffer[3])
 {
 // Success!
 digitalWrite(13, LOW);
 return co2_value;
 }
 else
 {
 // Failure!
 /*
 Checksum failure can be due to a number of factors,
 fuzzy electrons, sensor busy, etc.
 */

 digitalWrite(13, LOW);
 return 0;
 }
}

void loop() {

 int co2Value = readCO2();
 if(co2Value > 0)
 {
 Serial.print("CO2 Value: ");
 Serial.println(co2Value);
 }
 else
 {
 Serial.println("Checksum failed / Communication failure");
 }

 delay(2000);
}

Viewing all articles
Browse latest Browse all 17819

Trending Articles