I'm trying to write a library to interface with the HMC6343 sensor and in short, no matter what i seem to do, i get massive amounts of errors and i'm hoping for some help.
without further ado,
//arduino
#include <Wire.h>
#include <sensor.h>
int heading;
int pitch;
int roll;
int H[3];
void setup(){
Serial.begin(9600); //set up serial baud rate
Wire.begin();
}
void loop(){
H[3]=sensor.getHeading();
Serial.print("Heading: x: ");
Serial.print(H[0], DEC);
Serial.print(" y: ");
Serial.print(H[1], DEC);
Serial.print(" z: ");
Serial.print(H[2], DEC);
delay(5000);
}
//sensor.cpp
#include <WProgram.h>
#include <sensor.h>
#include <Wire.h>
#define compassAddress 0x32>>1 //I2C slave address from datasheet
int[3] sensor::getHeading() {
Wire.beginTransmission(compassAddress);
Wire.send(0x50); //send getHeading command to sensor
Wire.endTransmission();
delay(2); //pause for sensor to read data
Wire.requestFrom(compassAddress,6); //request six bits of data from sensor
for(int i=0; i<6; i++) {
while(Wire.available()==0);
_data[i] = Wire.receive(); //write recieved data to data[]
}
H={(_data[0]*256 + _data[1])/10, _data[2]*256 + _data[3], _data[4]*256 + _data[5]} //scale heading variable
return H;
}
//sensor.h
#ifndef sensor_h
#define sensor_h
#include <WProgram.h>
#include <Wire.h>
#define compassAddress 0x32>>1 //I2C slave address from datasheet
class sensor
{
public:
//heading variables
int H[3];
//heading functions
int getHeading();
private:
byte _data[6];
};
#endif
this is just controlling the heading variables. I keep getting errors "expected primary-expression before '.' token" at the function call" in the arduino code. I also wanted to use pointers to output the three variables separately rather than the ugly H[3] array, but it was more trouble than i had anticipated. Any guidance would be much appreciated. Thanks!!
you can not return an array as it will be destroyed by the stack
you should do as you first thought like this:
//in header
public:
void getHeading(int &x, int &y, int &z);
//in cpp
void sensor::getHeading(int &x, int &y, int &z) {
Wire.beginTransmission(compassAddress);
Wire.send(0x50); //send getHeading command to sensor
Wire.endTransmission();
delay(2); //pause for sensor to read data
Wire.requestFrom(compassAddress,6); //request six bits of data from sensor
for(int i=0; i<6; i++) {
while(Wire.available()==0);
_data[i] = Wire.receive(); //write recieved data to data[]
}
x = (_data[0]*256 + _data[1])/10;
y = _data[2]*256 + _data[3];
z = _data[4]*256 + _data[5]; //scale heading variable
//x, y and z will be changed at the scope outside this method
}
The pointers have been added as you said, but i'mstill having trouble accessing the functions withing the sensor class for some reason. When i compile i'm getting
sketch_feb01a.cpp: In function 'void loop()':
sketch_feb01a:44: error: expected unqualified-id before '.' token
and line 44 is void loop() { so i'm assuming it has more to do with the function call at void sensor.getHeading(&x,&y,&z); as it is written now.
Any ideas on why this is? Is my syntax incorrect in the library or the class? i can reprint the code as it is written now if it might help someone smarter than i am help me solve this issue
Create an instance of class sensor like this: sensor mySensor; then call the getHeading like this: mySensor.getHeading(x,y,z); //assumes x y and z already exists and that they are declared as ints
one final question is whether i can create the instance like AlphaBeta said, sensor mysensor; within the library...i only ask because i notice that you dont have to actively create an instance of libraries like wire or others. I looked through some other libraries and noticed things like:
// Constructors
TwoWire::TwoWire()
{
}
in Wire.cpp and a corresponding
class TwoWire
{
public:
TwoWire();
}
in Wire.h
thinking this might be what i was looking for, i tried to implement it in my library, but it didnt work. The library in general is coming along nicely,however, and controlling my sensor like a champ with the sensor mysensor; in the arduino code, it just seems that there might be a way for the inclusion to take care of that as well.
This final question then i'm out of your hair on this issue, i promise!