Acclerometer + Arduino = Aii

See what I did there…

I will have to list the Nintendo Wii as my inspiration for this project.

I’ve been working on my Arduino + accelerometer for quite some time now, but I’ve finished the code. The main features I wanted to has was to be able to send, via the serial connection, to the PC the ACTUAL values in g. So I’d know how many gs the module was pulling. I also wanted the RGB LED I’d mounted on the shield to change colours as the board was moved.

However I ran into a few problems namely, as gs are quite large units the results would be in floating point numbers. Which cannot be sent by the Arduino command “Serial.print”. I tried many work arounds, but I eventually gave up, and settled for the raw data value being sent.

And here’s the code running on the Arduino:

/* Aii - Accelerometer Shield */
/* Interfaces an accelerometer */
/* and an RBGB LED to an arduino */
/* board. TODO: use arrays? */
/* (CC) TheFallenIndustries 2007 */

int red = 3; // Outputs
int green = 6;
int blue = 5;
int fs = 4;

int x = 0; // Inputs
int y = 1;
int z = 2;
int button = 2;

int xval = 0; // Accelerometer variables
int yval = 0;
int zval = 0;

float xfloat = 0; // Values of g
float yfloat = 0;
float zfloat = 0;

int rval = 0; // PWM out variables
int gval = 0;
int bval = 0;

void setup()
pinMode(red,OUTPUT); // Set the RGB PWMs as outputs
pinMode(button,INPUT); // Set button as an input
digitalWrite(green,LOW); // Turn off the ever-on green LED
digitalWrite(fs,HIGH); // Fullscale setting 1=6g 0=2g

void loop()
xval = analogRead(x); // Read the raw values
yval = analogRead(y);
zval = analogRead(z);

Serial.print("x:"); // Print the raw values
Serial.print(", y:");
Serial.print(", z:");
Serial.print(10, BYTE);

xfloat = xval; // set float to gravity in axis
xfloat = (xfloat - 441)/ 63;
yfloat = yval;
yfloat = (yfloat - 441)/ 63;
zfloat = zval;
zfloat = (zfloat - 441)/ 63;

rval = xval - 260; // set RGB LED PWMs
gval = yval - 260;
bval = zval - 260;

delay(500); // Pause of 1/4 a second

Some changes I haven’t got round to making to the code:
Using arrays to store variables.
Using a function to convert the raw value to a value of g, and to store it as an array
General house keeping

But it worked for me in that video so all is good!


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: