Arduino + Gyro Sensor
This is basic code to take input from a gyro sensor and export it through the serial window. There is also a function in there that takes a baseline average of 10 samples to establish what the “zero rotation” voltage is.
Code after the break:
float turn = 0;
int baselineSamples = 10;
float baselineValue = 0;
void setup()
{
delay(1000);
Serial.begin(9600);
pinMode(A0, INPUT);
baseline();
}
void loop()
{
turn = baselineValue - analogRead(A0);
Serial.println(turn);
delay(100);
}
// This Function Calibrates the Gyro
void baseline()
{
float samples[baselineSamples + 1];
for(int i = 0; i < baselineSamples; i++)
{
samples[i] = analogRead(A0);
delay(10);
}
for(int i = 0; i < baselineSamples; i++) //Now we add up all the values
{
baselineValue = baselineValue + samples[i];
}
baselineValue = baselineValue/baselineSamples; //Finally we divide to get an average
}



No trackbacks yet.