class name
{
protected:
//variables;
//void write function(arguments){//sends data to I2C connection//variable data1;}
//void read function(arguments){//receives data over I2C in forms of bytes in variables. the bytes variables were declared in the protected identifier, outside the function.//variable data2;}
public:
//void switcher function(){//condition statement. 1st condition: print1(). 2st condition:print2().}
//int print1 function(){//assign values from read function to int variables, then it return the values}
//int print2 function()//assign values from read function to int variables, then it return the values}
private:
};
The functions marked in red indicate were the error appears.
The error that is giving me is:
-In function print1(function read wasn't declared in this scope, variable data1 wasn't declared in this scope)
-In function print2(function read wasn't declared in this scope, variable data2 wasn't declared in this scope)
and the error:D:\Programs\arduino-1.0.1\libraries\ITG3200\ITG3200.cpp: In function 'int getGyro()':
D:\Programs\arduino-1.0.1\libraries\ITG3200\ITG3200.cpp:87: error: 'read' was not declared in this scope
D:\Programs\arduino-1.0.1\libraries\ITG3200\ITG3200.cpp:88: error: 'gyro' was not declared in this scope
D:\Programs\arduino-1.0.1\libraries\ITG3200\ITG3200.cpp: In function 'int getTemp()':
D:\Programs\arduino-1.0.1\libraries\ITG3200\ITG3200.cpp:97: error: 'read' was not declared in this scope
D:\Programs\arduino-1.0.1\libraries\ITG3200\ITG3200.cpp:98: error: 'temp' was not declared in this scope
I don't want to start a new topic in Sensors because it's a quick question. After I've made the change in the code all seemed to work fine. After 1 hour of leaving it off, I started again to look at the measurements. This time the Z axis and the temperature data was the same.
What could have happened? I don't find any mistake in the code above.
output is an array of 2 ints, so how can you assign something to output[2]? (Out of bounds array access anyone...)
Also, you are returning output[2] , which ignoring the fact it is not part of the array doesn't return output[0] and output[1], so what is the point in calculating them?
Firstly,
the thing with the intr function is for tomorrow. I didn't have time to finish it. So I left a free space. For the moment it's being bypassed.
Secondly the counter must be made a static.
And thirdly, can you explain me how should I do return the 3 data from the gyro? honestly, at least in my perception, I've always done it this way (the array) and it always worked. Maybe I'm too tired, or for god damn I forgot C++.
Thank you
PS: I think I got it: using pointers. (for the output data)
int output[3] = {0}; //doesn't have to be called output, call it whatever you want
getGyro(output); //pass a reference to the local array
Then you can just access the array normally. e.g. something = output[0];
Note that there are 3 (THREE) bytes in the array declaration as you need array[0], array[1] and array[2].
An alternative is to make the output[3] array a private variable of your class, that way it can be accessed anywhere within the class without having to pass the function a pointer.
In header:
class itg3200
{
private:
int output[3];
protected:
...
If you wanted to be really clever, you could do this:
In the header
typedef union{
byte array[2];
int integer;
} GyroData;
class itg3200
{
private:
GyroData output[3];
protected:
...
In cpp:
void itg3200::getGyro(){
read(GYRO_OUT,6);
for(byte i = 0, j = 0; i < 3; i++,j+=2){
output[i].array[0]=gyro[j+1];
output[i].array[1]=gyro[j];
}
}
When used:
getGyro();
something = output[0].integer; //just an example of how to use the integer value.
In this situation unions are especially useful as they have a much smaller footprint compared to:
x = y<<8 + z;
which takes at least 8 clock cycles (possibly 25 depending on the compiler setting), compared to 2 for the union. (the avr has to perform 8 left shifts).
(I haven't compiled this, so I may have made a mistake somewhere, but the principal is right.)
The union method is becoming very popular, avoids nasty casts with pointers and provides a nice OO syntax.
Here is a type safe method.
template< typename _Type > union Serializer{
_Type value;
byte data[ sizeof( _Type ) ];
};
//You would use like
class itg3200
{
private:
Serializer< int > output[3];
protected:
...