I'm very new to Arduino and really to microcontrollers period, I've done some limited PIC stuff. I'm working on a project that uses a Banner Photoelectric Sensor QS30LDQ, Accelerometer and LCD to display RPM's and Accelerometer information. This is beginning of the project and I've got to working ok but as my code runs I get weird anomalies on my first row of the LCD at weird times. The characters do change but they seem to be all the same on the line each time. I don't clear the LCD so they stay there. Here is a picture of the characters and my code. I'm not sure what is causing this. Any help would be appreciated and also any comments to improve my code would be accepted.
const unsigned long serialPeriod = 1000; // print to the serial console every second
unsigned long timeSerialDelay = 0;
volatile unsigned long numInterrupts = 0;
int xaxis = 0; //X output of the accelerometer
int yaxis = 1; // Y output of the accelerometer
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void setup()
{
// initialize serial communications:
// Serial.begin(9600);
lcd.clear();
lcd.begin(16, 2); // intialise the LCD
//Interrupt 0 is digital pin 2, so that is where the IR detector is connected
//Triggers on FALLING (change from HIGH to LOW)
attachInterrupt(0, countRPMs, RISING);
// initialize the pins connected to the accelerometer
// as inputs:
pinMode(xaxis,INPUT);
pinMode(yaxis,INPUT);
}
void loop()
{
if((millis() - timeSerialDelay) >= serialPeriod)
{
//detachInterrupt(0);
unsigned long RPM = (numInterrupts*60);
// Serial.print(RPM);
// Serial.println(" RPM");
numInterrupts = 0;
timeSerialDelay = millis();
//Print out result to lcd
lcd.home();
lcd.print("RPM=");
lcd.print(RPM);
//Restart the interrupt processing
//attachInterrupt(0, countRPMs, RISING);
}
}
void countRPMs()
{
//Serial.println("high");
//Serial.println(millis());
//Each rotation, this interrupt function is run twice, so take that into consideration for
//calculating RPM
//Update count
numInterrupts++;
//accelerometer stuff
// variables to read sensor voltage
int valx, valy;
valx = analogRead(xaxis); // read the value from the sensor
valy = analogRead(yaxis); // read the value from the sensor
// convert the pulse width into acceleration
// accelerationX and accelerationY are in milli-g's:
// earth's gravity is 1000 milli-g's, or 1g.
//accelerationX = ((pulseX / 10) - 500) * 8;
//accelerationY = ((pulseY / 10) - 500) * 8;
// print the acceleration
//Serial.print(valx);
// print a tab character:
//Serial.print("\t");
//Serial.print(valy);
//Serial.println();
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("X:");
lcd.setCursor(2, 1);
lcd.print(valx);
lcd.setCursor(7, 1);
lcd.print(" Y:");
lcd.setCursor(10, 1);
lcd.print(valy);
}