Archive for November, 2007

Acclerometer + Arduino = Aii

3 November, 2007

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(green,OUTPUT);
pinMode(blue,OUTPUT);
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
beginSerial(9600);
}

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

Serial.print("x:"); // Print the raw values
Serial.print(xval);
Serial.print(", y:");
Serial.print(yval);
Serial.print(", z:");
Serial.print(zval);
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
analogWrite(red,rval);
gval = yval - 260;
analogWrite(green,gval);
bval = zval - 260;
analogWrite(blue,bval);

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!