Hi folks. I'm trying to set up a device that will display "UP" "DOWN" "LEFT" "RIGHT" on an LCD display, using an MMA7361 accelerometer as the input sensor. I've got it all set up, and the initial code written, but I can seem to get a stable output from the accelerometer. I'm using 2 analog inputs as the x and y inputs (not using the z axis) and digital outs to the LCD. When the code is initially executed the accelerometer is sitting flat, and it takes baseline readings for x and y. While the program is running however, the x and y values are jumping al over the place, even while the sensor sits still. I'm not sure if the problem lies in the accelerometer, or if there is some sort of interference being introduced somewhere within the controller. Here's the code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int xIn=A4;
int yIn=A3;
int xbl;
int ybl;
int sl=1;
void setup(){
lcd.begin(20, 4);
Serial.begin(9600);
pinMode(xIn, INPUT);
pinMode(yIn, INPUT);
pinMode(A5, INPUT);
pinMode(sl, OUTPUT);
digitalWrite(sl, HIGH);
xbl = analogRead(xIn);
delay(500);
ybl = analogRead(yIn);
delay(500);
}
void loop(){
int xPos=analogRead(xIn);
delay(500);
int yPos=analogRead(yIn);
delay(500);
int offset=65;
if (xPos>xbl+offset) {
lcd.clear();
lcd.setCursor(7,1);
lcd.write("LEFT");
delay(500);
}
if (xPos<xbl-offset) {
lcd.clear();
lcd.setCursor(7,1);
lcd.write("RIGHT");
delay(500);
}
if (yPos>ybl+offset) {
lcd.clear();
lcd.setCursor(7,1);
lcd.write("DOWN");
delay(500);
}
if (yPos<ybl-offset) {
lcd.clear();
lcd.setCursor(8,1);
lcd.write("UP");
delay(500);
}
//Displays readings to Serial Monitor
Serial.print("xbl ");
Serial.print(xbl);
Serial.print(" X: ");
Serial.print(xPos);
//delay(1000);
Serial.print(" ybl ");
Serial.print(ybl);
Serial.print(" Y: ");
Serial.println(yPos);
//delay(1000);
}
Has anybody ever come across an issue like this. I'm pretty new to Arduino, so if I left any info out just let me know. Thanks.