Thursday, 5 April 2012

I Have Turned Myself Around

So, for whatever reason I needed to get a small accelerometer working off of an Arduino Nano (and wasn't allowed to use a Wii Motion+), the setup needed to be small, so I grabbed one of these MMA7361 accelerometers of the net (Don't know if that is who I bought it from but that is the one I bought) and wired it up.

RED 3v3 & AREF to 3v3, ORANGE A1 to X, GREEN A2 to Y, YELLOW A3 to Z, BLUE D2 to SL (SLEEP), BLACK to GND.

The code was borrowed off of various forums and tweaked for my specification the best being the one shown below (tweaked slightly) taken from the Arduino forum here.

Needles to say it works, it is just a matter of working out what numbers I need etc, it is good fun waving things around like a lunatic while staring at a screen late at night, I recommend it. The image was made in Fritzing and tweaked in Photoshop CS5 as they didn't have the exact accelerometer in the library.

//ACCEL

int x_avg[10];
int y_avg[10];
int z_avg[10];

int x_axis = 0;
int y_axis = 0;
int z_axis = 0;

int slp_pin = 2;
int i;

void setup()
{
Serial.begin(9600);
analogReference(EXTERNAL);
pinMode(2, OUTPUT);
}

void loop()
{
digitalWrite(slp_pin, HIGH);

for (i=0; i<10; i=i+1)
{
x_avg[i] = analogRead(1);
y_avg[i] = analogRead(2);
z_avg[i] = analogRead(3);
}

for (i=0; i<10; i=i+1)
{
if (i == 0)
{
x_axis = x_avg[i];
y_axis = y_avg[i];
z_axis = z_avg[i];
}
else
{
x_axis = x_axis + x_avg[i];
y_axis = y_axis + y_avg[i];
z_axis = z_axis + z_avg[i];
}
}

Serial.print("x-axis");
Serial.print("\t");
Serial.println(x_axis/10);

Serial.print("y-axis");
Serial.print("\t");
Serial.println(y_axis/10);

Serial.print("z-axis");
Serial.print("\t");
Serial.println(z_axis/10);

Serial.println("");
delay(1000);
}

No comments:

Post a Comment