With the code from this site Arduino Playground - Gyro it really doesn't work. I have it hooked up to a servo and the servo barely rotates, and when it does it only rotates one way. Of course thats with the code just copied directly from the site (which i know I shouldn't do). I did get it working my self when I wrote the code my self (which was exactly the same), I was still copying the code for means of understanding it. But I still had a problem. the servo would rotate in one direction more the other, which is annoying. I narrowed it down to the drift. Although the drift doesn't rotate the servo while the gyro is stationary. I suspect its adding "weight" to one direction when I turn the Gyro, so in the end when I turn it around an back again the servo wouldn't rotate back to same place as before.
Heres the code from the site with the Servo implemented:
/* Keep track of gyro angle over time
* Connect Gyro to Analog Pin 0
*
* Sketch by eric barch / ericbarch.com
* v. 0.1 - simple serial output
*
*/
#define GYROY A0
#include <Servo.h>
Servo myServo;
int gyroPin = A0; //Gyro is connected to analog pin 0
float gyroVoltage = 5; //Gyro is running at 5V
float gyroZeroVoltage = 1.35; //Gyro is zeroed at 2.5V
float gyroSensitivity = .002; //Our example gyro is 7mV/deg/sec
float rotationThreshold = 1.0; //Minimum deg/sec to keep track of - helps with gyro drifting
float currentAngle = 180 / 2; //Keep track of our current angle
void setup() {
myServo.attach(11);
Serial.begin (9600);
}
void loop() {
//This line converts the 0-1023 signal to 0-5V
float gyroRate = (analogRead(GYROY) * gyroVoltage) / 1023;
Serial.println(analogRead(GYROY));
//This line finds the voltage offset from sitting still
gyroRate -= gyroZeroVoltage;
//This line divides the voltage we found by the gyro's sensitivity
gyroRate /= gyroSensitivity;
//Ignore the gyro if our angular velocity does not meet our threshold
if (gyroRate >= rotationThreshold || gyroRate <= -rotationThreshold) {
//This line divides the value by 100 since we are running in a 10ms loop (1000ms/10ms)
gyroRate /= 100;
currentAngle += gyroRate;
}
//Keep our angle between 0-359 degrees
if (currentAngle < 0)
currentAngle += 180;
else if (currentAngle > 179)
currentAngle -= 180;
myServo.write(currentAngle);
//DEBUG
Serial.println(gyroRate);
delay(10);
}
and heres my code that I wrote that mimiks the first one
#define GYROX A0
#define GYROY A1
//#define VREF A2
#include <Servo.h>
int x, y; // X and Y in-plane sensing
float voltage = 5.0;
float zeroVoltage = 1.35;
float gyroSensitivity = .002;
float rotationThreshold = 1.0;
float currentAngle = 180 / 2;
float previousAngle = 0;
Servo myServo;
void setup()
{
myServo.attach(11);
Serial.begin(9600); // sets the serial port to 9600
}
void loop()
{
float gyroRate = (analogRead(GYROY) * voltage) / 1023.0;
gyroRate -= zeroVoltage;
gyroRate /= gyroSensitivity;
if(gyroRate >= rotationThreshold || gyroRate <= -rotationThreshold)
{
gyroRate /= 100;
currentAngle += gyroRate;
}
if (currentAngle < 0)
{
currentAngle += 180;
}
else if (currentAngle > 180)
{
currentAngle -= 180;
}
myServo.write(currentAngle);
Serial.println(currentAngle);
//delay(10);
}