I'm wanting to use a ADXL335 analog accelerometer as a simple 2 axis level to detect movement. I do not care what angle it is at when moved (other than +- range padding). I only care that it HAS moved via in this example with an LED. The cleaned up code below does what I want other than (and this is the issue)... it depends on me placing the ADXL335 at the same exact starting level each and every time. This will be placed in new and varying locations so I think I need a way to sample the current pins, average them, pad range values, set them as Mins Maxes, then compare to the current pins.
This is my very first post. I have looked around but not everything meshes with my device or has range padding.
Even helping me with proper keywords and links would be appreciated.
// Adafruit ADXL335 (5v w/AREF) + Uno R3 (5v) + 1 LED
const int xPin = A0; // x-axis of the ADXL335
const int yPin = A1; // y-axis of the ADXL335
const int ledPin = 13; // LED w/resistor
long xy_min = 505; // From me watching the Serial Monitor then - Padding
long xy_max = 520; // From me watching the Serial Monitor then + Padding
long x = 0; // clear values to be set below I guess
long y = 0; // clear values to be set below I guess
void setup() {
pinMode(ledPin, OUTPUT); // Set LED pin 13 as output
}
void loop() {
analogReference(EXTERNAL); // ADXL335's 3.3v pin connected to Uno's AREF pin
x = analogRead(xPin); // Current X value
y = analogRead(yPin); // Current y value
if (y <= xy_max && y >= xy_min && x >= xy_min && x <= xy_max) { // compare
digitalWrite(ledPin, HIGH); // Turn LED on
} else {
digitalWrite(ledPin, LOW); // Turn LED off
}
}