# Anyone willing to go over my Accelerometer coding?

I have MMA7260q 3axis accelerometer which can be setup from 1.5G all the way to 6G

now here is my code:

``````int posX= 2;
int negX= 4;
int posY= 7;
int negY= 8;
int posZ= 12;
int negZ= 13;
int X=0;
int Y=0;
int Z=0;
int Xin=0;
int Yin=1;
int Zin=2;

void setup()
{
pinMode(posX,OUTPUT); // sets each axis LEDs//
pinMode(posY,OUTPUT);
pinMode(posZ,OUTPUT);
pinMode(negX,OUTPUT);
pinMode(negY,OUTPUT);
pinMode(negZ,OUTPUT);
}

void loop()
{
posX=LOW;  // all lights off when data = 0//
posY=LOW;
posZ=LOW;
negX=LOW;
negY=LOW;
negZ=LOW;

do
{
negX=HIGH; //lights the X axis negative LED//
}while(X<0);

do
{
negY=HIGH;  //lights Y axis negative LED//
}while(Y<0);

do
{
negZ=HIGH; //lights Z axis negative LED//
}while (Z<0);

do
{
posX=HIGH; //lights X axis positive LED//
}while (X>0);

do
{
posY=HIGH; //lights Y axis positive LED//
}while (Y>0);

do
{
posZ=HIGH; //lights Z axis positive LED//
}while (Z>0);

}
``````

as you can see i’m compensating for the 1.86V when z axis reacts to gravity. im pretty sure im not doing the compensation right so any input would be welcome.

The analogRead function returns integers in the range 0 to 1023. They are not floating-point positive and negative values as your code expects. Adding 1.86 to Z, for example, makes no sense until you convert the integer reading 0 to 1023 to the appropriate units.

that's what i was suspecting , where can i find out what the values are in arduino. by this i mean what's the sampling factor of the A/D. where can i find this info. or should i just trial and error by reading the values?

With the default reference (5V) then 0 means 0V and 1023 means 5V.

thank you very much , that helps a lot. what about negative voltage values?

You cannot apply negative voltage values to the microcontroller; you will damage it.

Besides, your accelerometer does not output negative values. It outputs a half-way voltage (about 2.5V) to mean 0g, lower values for negative g's and higher values for positive g's.

sorry my brain stopped working for a minute i was reading the data sheet wrong instead of Gs i read volts my bad i apologize. its midnight here in Montreal.

just to reiterate my mini project: 6 LEDs that light up depending on the tilt of the accelerometer, in other words if i tilt the accelerometer to the left the LEFT LED will light up , if i tilt it up the front LED lights up and so on.

ok so i have connected everything correctly (i went over my connections at least 5 times lol), i have put my AREF to external 3.3V , and i got approximate values for the analogRead command). Now I checked the output of the accelerometer and it varies as i move it/tap it so i know the accelerometer is working fine. BTW im using MMA7260q accelerometerhttp://pdf1.alldatasheet.co.kr/datasheet-pdf/view/103487/MOTOROLA/MMA7260Q.html

Now here is my code:

``````int posX= 2;
int negX= 4;
int posY= 7;
int negY= 8;
int posZ= 12;
int negZ= 13;
int X=0;
int Y=0;
int Z=0;
int Xin=0;
int Yin=1;
int Zin=2;

void setup()
{
analogReference(EXTERNAL);
pinMode(posX,OUTPUT); // sets each axis LEDs//
pinMode(posY,OUTPUT);
pinMode(posZ,OUTPUT);
pinMode(negX,OUTPUT);
pinMode(negY,OUTPUT);
pinMode(negZ,OUTPUT);
}

void loop()
{

digitalWrite (posX,LOW);  // all lights off when data = 0//
digitalWrite (posY,LOW);
digitalWrite (posZ,LOW);
digitalWrite (negX,LOW);
digitalWrite (negY,LOW);
digitalWrite (negZ,LOW);

do
{
digitalWrite (negX,HIGH); //lights the X axis negative LED//

}while(X<500);
delay(1000);

do
{
digitalWrite (negY,HIGH);  //lights Y axis negative LED//

}while(Y<450);
delay(1000);

do
{
digitalWrite (negZ,HIGH); //lights Z axis negative LED//

}while (Z<800);
delay(1000);

do
{
digitalWrite (posX,HIGH); //lights X axis positive LED//

}while (X>500);
delay(1000);

do
{
digitalWrite (posY,HIGH); //lights Y axis positive LED//

}while (Y>450);
delay(1000);

do
{
digitalWrite (posZ,HIGH); //lights Z axis positive LED//

}while (Z>800);
delay(1000);

}
``````

im not sure what i’m doing wrong but out of 6 LEDs i have 5 that are permanently lit and no amount of tapping/moving /shaking will turn them off. My suspicion is that my delays/ timing is off or the values are too close to call. Can anyone tell me what i’m doing wrong? Is it my functions or is my program stuck in an infinate loop?

I don’t understand your use of do-while loops and delays. Why are loops even necessary here? I think you just want a simple if statement:

``````if (X<500) {
digitalWrite(negX,HIGH);
} else {
digitalWrite(negX,LOW);
}

if (X>500) {
digitalWrite(posX,HIGH);
} else {
digitalWrite(posX,LOW);
}
``````

my reasoning for the do loops is that i thought it would be better suited for controlling the leds. as for the delay i read that it is better to give one so the ADC can settle in between readings. I will try your method right now thanks RC

IT WORKS!!! thanks a bunch RC.