Hey,
Im using an adxl335 accelerometer, im using an old version of arduino which does not have a 3.3V pin so im using an external 3V supply to power my sensor.The problem is that when i run the program to light up an led when a tilt is detected, the led lights up, but the accelerometer readings gets boosted whenever the led lights up and gets lowered whenever the led goes low.I get the output:
accelerations are x, y, z : 396 391 483//when in stable position and led is ON
accelerations are x, y, z : 396 391 483
accelerations are x, y, z : 396 391 483
accelerations are x, y, z : 396 391 483
accelerations are x, y, z : 351 348 430//it gets lowered when the led turns off while it is still in stable mode
accelerations are x, y, z : 351 348 430
accelerations are x, y, z : 351 348 430
Please help:
int x,y,z;
void setup()
{
Serial.begin(9600); // sets the serial port to 9600
pinMode(7,OUTPUT);
pinMode(6,OUTPUT);
pinMode(5,OUTPUT);
pinMode(4,OUTPUT);
pinMode(3,OUTPUT);
}
void loop()
{
x = analogRead(0); // read analog input pin 0
y = analogRead(1); // read analog input pin 1
z = analogRead(2); // read analog input pin 1
Serial.print("accelerations are x, y, z: ");
Serial.print(x, DEC); // print the acceleration in the X axis
Serial.print(" "); // prints a space between the numbers
Serial.print(y, DEC); // print the acceleration in the Y axis
Serial.print(" "); // prints a space between the numbers
Serial.println(z, DEC); // print the acceleration in the Z axis
if( y > 290 && z < 340)//up
{
digitalWrite(7,HIGH);
}
else digitalWrite(7,LOW);
if( x<270 && y<270 && z<340)//down
{
digitalWrite(6,HIGH);
}
else digitalWrite(6,LOW);
if ( x>260 && x<286 && y<290 && y>240 && z<350 && z>315)//centre
{
digitalWrite(5,HIGH);
}
else digitalWrite(5,LOW);
if(x>298 && y>282 && z>310)//right
{
digitalWrite(4,HIGH);
}
else digitalWrite(4,LOW);
if(x<235 && y<275 && z<318)//left
{
digitalWrite(3,HIGH);
}
else digitalWrite(3,LOW);
delay(600); // wait 100ms for next reading
}