Code Example
/* TinkerKit! Gyroscope [T000060-64]
*
* This sketch shows how to read this 2-axis gyroscope,
* turning in a given angular velocity and then converting it
* in the simplest way in an angular position (/inclination).
*
* Connect: the X-axis to the Analog Input Pin 0 (I0)
* the Y-axis to the Analog Input Pin 1 (I1)
* Optional: connect a servo to Analog Output Pin 9 (O2)
*
* created by Federico Vanzati / f.vanzati@arduino.cc
* in September 2011
*
* inspired from www.arduino.cc/playground/Main/Gyro
* by eric barch / ericbarch.com
*/
#include <Servo.h>
// Pin used in this example
#define SERVO 9
#define X_GYRO 0
#define Y_GYRO 1
#define ADCresolution 4.89f // = 5000mV/1023counts: Arduino analog pins
resolution expressed in mV/count
#define Sensitivity 0.67f // [mV/dps] sensitivity of the sensor, took from
datasheet (4x output mode)
// Conversion coefficient, we do here because is a constant! so we'll not
do the calculation every loop
#define K ADCresolution/Sensitivity // the constant!
#define nrSamples 6 // Number of samples that we take for each measure
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
// Timing variables
unsigned long time, sampleTime = 12;
unsigned long printTime = 0, serialRefresh_time = 500;
float deltaT = (float)sampleTime*nrSamples/1000;
//Gyroscope variables
int roll_zeroVoltage, pitch_zeroVoltage;
int roll_rawADC[nrSamples], pitch_rawADC[nrSamples]; // store 3
values...just to avverage
float roll_rate, pitch_rate; //
float roll_angle = 0, pitch_angle = 0;
int c=0; // just a counter to count the samples
int pos; // variable to store the servo position