Need Help with code:

Everything runs fine a few times through but eventually the screen will start spewing random charactors and not return to normal, basically I have a switch between Pin 2 and ground, the interrupt detects a fall in voltage to “swap screens”

#include <LiquidCrystal.h>
//SET UP DISPLAY//
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 6);
int backLight = 13;
int ledPin = 13; // LED is attached to digital pin 13
int x = 15; // variable to be updated by the interrupt
int lastxvalue = 15;
//DEFINE CHAR
byte fullbar[8] = {
B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
};
byte blank[8] = {
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
};
//variables to keep track of the timing of recent interrupts
unsigned long button_time = 0;  
unsigned long last_button_time = 0; 

void setup() {                
  //enable interrupt 0 which uses pin 2
  //jump to the increment function on falling edge
  digitalWrite(2, HIGH);
  attachInterrupt(0, increment, FALLING);
  Serial.begin(9600);  //turn on serial communication
  pinMode(backLight, HIGH);
  digitalWrite(backLight, HIGH);
  lcd.begin(16,2);              // columns, rows.  use 16,2 for a 16x2 LCD, etc.
  lcd.clear();
  startUpRoutine();
}

void loop() {
  loadmenu();
}

// Interrupt service routine for interrupt 0
void increment() {
  button_time = millis();
  //check to see if increment() was called in the last 250 milliseconds
  if (button_time - last_button_time > 1200)
  {
    lastxvalue = x;
    lcd.clear();
    if(x!=20)
    {
      x++;
    }
    else
    {
      x=15;
    }
    last_button_time = button_time;
  }
}

void loadmenu()
{
    if(x==15)
    {
      clearfunction();
      lcd.setCursor(0,0);
      lcd.write("RPM:            ");
      //Test some functionality
      lcd.setCursor(0,1);
      for(int i=0; i<=7; i++)
      {
      lcd.write((uint8_t)0);
      }
      
    }
    if(x==16)
    { 
      clearfunction();
      lcd.setCursor(0,0);
      lcd.write("Engine Temp:");
      lcd.setCursor(0,1);
      for(int i=0; i<=8; i++)
      {
        lcd.write((uint8_t)0);
      }
    }
    if(x==17)
    {
      clearfunction();
      lcd.setCursor(0,0);
      lcd.write("Fuel Level:");
      lcd.setCursor(0,1);
      for(int i=0; i<=10; i++)
      {
        lcd.write((uint8_t)0);
      }
    }
    if(x==18)
    {
      clearfunction();
      lcd.setCursor(0,0);
      lcd.write("Boost:");
      lcd.setCursor(0,1);
      lcd.write("17.8 Psi");
    }
   if(x==19)
    {
      clearfunction();
      lcd.setCursor(0,0);
      lcd.write("EGT:");
      lcd.setCursor(0,1);
      lcd.write("246 Degrees");
    }
    if(x==20)
    {
      clearfunction();
      lcd.setCursor(0,0);
      lcd.write("Speed: 134km/hr");
      lcd.setCursor(0,1);
      for(int i=0; i<=11; i++)
      {
        lcd.write((uint8_t)0);
      }
    }
    lastxvalue = x;
}

void startUpRoutine()
{
  lcd.write("//MPOWER");
  lcd.createChar(0, fullbar);
  lcd.createChar(1, blank);
  lcd.setCursor(0,1);
  lcd.write("Kai Rasporich");
  delay(1500);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.write("Loading:");
  delay(500);
  lcd.setCursor(8,0);
  lcd.write("Library");
  delay(500);
  lcd.clear();
  lcd.write("Loading:");
  lcd.setCursor(8,0);
  lcd.write("Data");
  lcd.setCursor(0,1);
  for(int i=0; i<=10; i++)
  {
  lcd.write((uint8_t)0);
  delay(85);
  }
  lcd.setCursor(8,0);
  lcd.write("Sync");
  lcd.setCursor(8,0);
  delay(500);
  lcd.setCursor(8,0);
  lcd.write("Gauge");
  lcd.setCursor(11,1);
  for(int i=0; i<=7; i++)
  {
  lcd.write((uint8_t)0);
  delay(85);
  }
  
  digitalWrite(backLight, LOW);
  lcd.clear();
  delay(40);
  digitalWrite(backLight, HIGH);
}

void clearfunction(){
  if(lastxvalue!=x)
  {
    lcd.clear();
  }
}

You should NOT call lcd.clear() in an interrupt routine. It takes far too much time and will cause garbage if the interrupt happens in the middle of a call to the LCD library.

Your variables 'button_time', 'last_button_time', 'x' and 'lastxvalue' should be declared 'volatile' because you change then in the interrupt routine.

It is generally safer to check for button input in loop() rather than using an interrupt.

Great advice thank you!! Works like a charm now. Another quick question, how much input voltage can the analog pins handle? I am looking to read in car data that could be as high as 12v. As well do you have any ideas as to how to provide power supply to the arduino via a 12V power source?

5V max on the input pins. Above that, you will damage the pins.

Try one of the 12V USB adapters that provide a 5V supply phone charging.

http://www.amazon.com/MobileSpec-MS02802-12V-Power-Adapter/dp/B004SSEEF2

Can find these all over the place, hardware stores even.

Thats a good idea.. I will see if I can find something I can wire discretely into the car. Can I send voltage into the Vin pin on arduino or do I need to use USB? I was hoping to mount the usb for editing code once its installed.

Need to use USB. Or the 5v header pin if you put a 1N4001 diode from 5V (anode) to Vin (cathode) to protect the regulator (so any spikes go back to Vin).

You will be downloading code while in the car?

Hey I am using a usb cigarette lighter, wired to a switch, works well in the car! and easy to transfer cable to laptop to tweak code.. I have changed the code entirely though.. now reads Boost, Air/Fuel Ratio, and hopefully RPM soon.