Saturday, March 19, 2011

Input tests (Blink)

Input test: accelerometer (model adxl335)

Wiring: use 3 short wires to connect X, Y, and Z to A0, A1, and A2 respectively.
use 2 more wires (measured to fit) to connect GND (accelerometer) to GND (arduino) and VCC to 3.3V.

Coding:
//Set pins
const int X_PIN = 0;
const int Y_PIN = 1;
const int Z_PIN = 2;
const int NUM_AXES= 3;
const int PINS[NUM_AXES]={X_PIN,Y_PIN,Z_PIN};
//Input comes really fast, so set a buffer for averaging values
const int BUFFER_SIZE=16;

int buffer[NUM_AXES][BUFFER_SIZE];
int buffer_pos[NUM_AXES]={0};

void setup()
{
Serial.begin(9600);
}

int get_axis(const int axis)
{
//Give arduino time to switch pins
delay(1);
//Read values into the buffer
buffer[axis][buffer_pos[axis]]=analogRead(PINS[axis]);
buffer_pos[axis]=(buffer_pos[axis]+1)%BUFFER_SIZE;
long sum=0;
//Average buffer values
for(int i=0;i
{
sum+=buffer[axis][i];
return round(sum/BUFFER_SIZE);
}

int get_x(){return get_axis(0);}
int get_y(){return get_axis(1);}
int get_z(){return get_axis(2);}

void loop()
{
//Print values
Serial.print("X: ");
Serial.print(get_x());
Serial.print("Y: ");
Serial.print(get_y());
Serial.print("Z: ");
Serial.println(get_z());
}


Tweaking:
Values from x and y axes are between ~8 and ~800 while z is between ~200-~750
Using these values, I need to set up a bit of code to detect when the unit is rotated along one of its axes within tolerances. The output should only read once after detecting this change.

No comments:

Post a Comment