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
}
About these ads
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: