i want to disable the function of the accelerometer if I put an LED in A2 (analog output for Arduino Uno) which is in high condition... if it is low condition, the function of the accelerometer can function it back normally.. during high state i want the accelerometer to disable...
my problem is how to make a command to disable the function of the accelerometer... Is it possible to use an IF/ELSE statement here...
can anyone help me??? please...
this is the code for my accelerometer taken from the net...
const int groundpin = 18; // analog input pin 4 -- ground
const int powerpin = 19; // analog input pin 5 -- voltage
int xpin = A3; // x-axis of the accelerometer
int sensorValue = 0; // variable to store the value coming from the sensor
void setup()
{
// initialize the serial communications:
Serial.begin(9600);
pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
digitalWrite(groundpin, LOW);
digitalWrite(powerpin, HIGH);
}
void loop()
{
// read the value from the sensor:
sensorValue = analogRead(xpin);
// print the sensor values:
Serial.print(analogRead(xpin));
// print a tab between values:
Serial.print("\t");
Serial.println();
// delay before next reading:
delay(100);
}
Moderator edit: </mark> <mark>[code]</mark> <mark>
I'm not sure what you're trying to do. The LED has no way of turning off in your code so even if you programmed it to stop taking readings from the accelerometer it won't do anything. How were you planning to toggle the LED on and off?
Lets say you added a button or some other way to toggle the LED on/off then you could only take readings from the accelerometer when powerpin is LOW like this.
if (digitalRead(powerpin) == LOW)
{
Serial.print(analogRead(xpin);
}
I am using right now an RF module... If I press the button of the remote of the RF MODULE which is "ON", I want to disable the function of the accelerometer... when it is turn off, I want to go back in normal the function of the accelerometer... just make an on and off to the accelerometer...
Some Acceleromers have a Sleep (SL) pin, which I suppose you could activate.
As for 'turning' accelerometer off then surely you could just toggle a variable accell_active = 0|1 and then only read the inputs and use them if it's active.
Pseudo code
declare accell_active and set to 1 (on by default)
if remote control set accelerometer on then set accell_active to 1
if remote control set accelerometer off then set accell_active to 0
if accell_active is 1 then read the accelerometer pins and do any processing
if accell_active is 0 then don't read the accelerometer pins of do any processing