Hey guys,
I wrote a code that I thought would recieve data from an accelerometer, and if that data's value was either greater than 500 or less than -500, would cause an LED to light up... Only thing is... The code is only displaying the serial data to the serial monitor, not lighting the LED when the values are correct. Here's the code, any help would be greatly appreciated...
int xPin = 2;
int yPin = 3;
int ledPin = 4;
void setup(){
Serial.begin(9600);
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop(){
int pulseX, pulseY;
int accelerationX, accelerationY;
pulseX = pulseIn(xPin, HIGH);
pulseY = pulseIn(yPin, HIGH);
accelerationX = ((pulseX / 10) - 500) * 8;
accelerationY = ((pulseY / 10) - 500) * 8;
Serial.print(accelerationX);
Serial.print("\t");
Serial.print(accelerationY);
Serial.println();
delay(100);
if (xPin >= 500){
digitalWrite(ledPin, HIGH);
}
if (xPin <= -500){
digitalWrite(ledPin, HIGH);
}
if (yPin >= 500){
digitalWrite(ledPin, HIGH);
}
if (yPin <= -500){
digitalWrite(ledPin, HIGH);
}
}
The only other thing I could ask is, now that it is working, the LED is constantly on after the value reaches <> 500, -500. How would I go about turning it off until the next value reaches <>500, -500?