An accelerometer is a device that provides 3-axis control. With Arduino, this is connected using 5 pins. VCC and GND are for power supply. Three output pins X, Y, Z which provides analog input to the Arduino board.
Now our Arduino board is getting three analog inputs we have to calibrate these input values to perform the actions that we want.
This is possible with the help of error values. As the accelerometer provides analog reading which oscillates around a center value like 300 according to movement.
Let when Arduino has powered the value is xx. Now, this xx is the first value we get. All other next values are stored in another variable x. So the error value is given by
error_x= xx- x;
Note: while powering the Arduino the accelerometer should be in rest position that you want as all the next error value are dependent on that position.
When we move accelerometer in different directions this error value changes in +ve and -ve integer value according to the amount of rotation.
For all three axis they are given by
error_x = xx- x;
error_y = yy- y;
error_x = zz- z;
let us understand this with the help of four led program
//program
int xx;
int yy;
int zz;
int x;
int y;
int z;
int error_x;
int error_y;
int error_z;
int led1=4;
int led2=5;
int led3=6;
int led4=7;
int k=0;
void setup()
{
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
}
void loop()
{
x=analogRead(0);
y=analogRead(1);
z=analogRead(2);
if(k=0)
{
xx=analogRead(0);
yy=analogRead(1);
zz=analogRead(2);
k=1;
}
error_x = xx - x;
error_y = yy - y;
error_x = zz - z;
if(error_x>0 && error_y >0 && error_z >5)
{
digitalWrite(led1,HIGH);
}
else
if(error_x<0 && error_y >0 && error_z >5)
{
digitalWrite(led2,HIGH);
}
else
if(error_x>0 && error_y <0 && error_z >5)
{
digitalWrite(led3,HIGH);
}
else
if(error_x<0 && error_y <0 && error_z >5)
{
digitalWrite(led4,HIGH);
}
else
{
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
digitalWrite(led4,LOW);
}
}
The diagram above shows that which led will glow when the accelerometer is rotated in different directions. This is a 2D graph. error_z is assumed in the perpendicular direction.
Another method is to calibrate the accelerometer manually by taking all the values serially by serial monitor.
Now our Arduino board is getting three analog inputs we have to calibrate these input values to perform the actions that we want.
This is possible with the help of error values. As the accelerometer provides analog reading which oscillates around a center value like 300 according to movement.
Let when Arduino has powered the value is xx. Now, this xx is the first value we get. All other next values are stored in another variable x. So the error value is given by
error_x= xx- x;
Note: while powering the Arduino the accelerometer should be in rest position that you want as all the next error value are dependent on that position.
When we move accelerometer in different directions this error value changes in +ve and -ve integer value according to the amount of rotation.
For all three axis they are given by
error_x = xx- x;
error_y = yy- y;
error_x = zz- z;
let us understand this with the help of four led program
//program
int xx;
int yy;
int zz;
int x;
int y;
int z;
int error_x;
int error_y;
int error_z;
int led1=4;
int led2=5;
int led3=6;
int led4=7;
int k=0;
void setup()
{
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
}
void loop()
{
x=analogRead(0);
y=analogRead(1);
z=analogRead(2);
if(k=0)
{
xx=analogRead(0);
yy=analogRead(1);
zz=analogRead(2);
k=1;
}
error_x = xx - x;
error_y = yy - y;
error_x = zz - z;
if(error_x>0 && error_y >0 && error_z >5)
{
digitalWrite(led1,HIGH);
}
else
if(error_x<0 && error_y >0 && error_z >5)
{
digitalWrite(led2,HIGH);
}
else
if(error_x>0 && error_y <0 && error_z >5)
{
digitalWrite(led3,HIGH);
}
else
if(error_x<0 && error_y <0 && error_z >5)
{
digitalWrite(led4,HIGH);
}
else
{
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
digitalWrite(led4,LOW);
}
}
The diagram above shows that which led will glow when the accelerometer is rotated in different directions. This is a 2D graph. error_z is assumed in the perpendicular direction.
Another method is to calibrate the accelerometer manually by taking all the values serially by serial monitor.
No comments:
Post a Comment