2

I am trying to measure a users 'hand steadiness' in order to control a video game character in UNITY3D. I am using a MinIMU-9 Gyro/Accelerometer/Compass linked with Arduino. I can measure pitch/roll etc in Arduino...and I have passed previous Arduino measurements into Unity via the serial command but I am l trying to do two things: 1. simplify & reduce noise in the data coming from the magnetometer reading before I pass it into Unity by taking an average of every 10 magnetometer readings. 2. Combining the three magnetometer values to get one overall variable or measurement that can be passed into unity.

This is the demo Sketch I have but I'm not sure how to script the addition of the x,y,z magnetometer values:

#include <Wire.h>
#include <LSM303.h>
LSM303 compass;
char report[80];
void setup()
{
  Serial.begin(9600);
  Wire.begin();
  compass.init();
  compass.enableDefault();
}
void loop()
{
  compass.read();
  snprintf(report, sizeof(report), "A: %6d %6d %6d    M: %6d %6d %6d",
    compass.a.x, compass.a.y, compass.a.z,
    compass.m.x, compass.m.y, compass.m.z);
  Serial.println(report);
  delay(100);
}

This is the type of 'averaging script' I would like to add in somewhere but I'm not sure where exactly to place it:

for(int i = 0; i < 10; i++){    //take ten readings in ~0.02 seconds
    reading[i] = compass.m.y;
    delay(2);
  }
  for(int i = 0; i < 10; i++){   //average the ten readings
    finalMagYreading += reading[i];
  }
   finalMagYreading
 /= 10;

Any help with this would be great.

Peter Bloomfield
  • 10,982
  • 9
  • 48
  • 87
joeyc
  • 21
  • 1

0 Answers0