LCD Weird Characters

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);    
 }

I think you need to put lcd.clear() after the lcd.begin().

Pete

Yeah I think your right but I only start seeing the weird characters after the code has run for about a minute

     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);

In an ISR? No! LCD's are pretty slow to print data. And get rid of those Serial.print()s, too. Don't just comment them out. They have no place in an ISR, either. Fast! That's what an ISR needs to be. The analogRead()s are even questionable.

Thanks for the reply I had to google what ISR was, remember i'm a noob :roll_eyes: I'll remove the serial stuff and may need to figure out a better way to code this project. As far as the analogread() my accelerometer is analog based accelerometer so I'm not sure how else I could do it. Should I purchase a digital accelerometer? Will having the serial stuff in there commented out cause the strange characters?

PaulS:

     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);



In an ISR? No! LCD's are pretty slow to print data. And get rid of those Serial.print()s, too. Don't just comment them out. They have no place in an ISR, either. Fast! That's what an ISR needs to be. The analogRead()s are even questionable.

As far as the analogread() my accelerometer is analog based accelerometer so I'm not sure how else I could do it. Should I purchase a digital accelerometer?

No. But, why do you need to read the accelerometer every time the encoder triggers the ISR?

Because I'm trying to build a dynamic balancer, if I take an accelerometer reading at the time the RPM is read it gives me a reference point to find the imbalance. I think I could instead of output it to lcd every time I could either average the readings and then ouput RPM and accelerometer readings every second or so??

Try adding a delay between analog read's. The A/D converter is probably getting confused.