My code lags the arduino !?

Hello everybody ! i got my arduino mega last week and i wanted to make it interface with the game LiveForSpeed, so i coded everything tried it the first time without writing data to the lcd module and everything was fine, the led rev meter worked perfectly. But when i try to output data to the lcd module (speed, gear etc) everything starts to lag nothing is synchro with the game.
Here is my code for the whole thing (i know its messy and not efficient at all but its just a start ^^") :

#include <Messenger.h>
#include <LCD4Bit_mod.h>
#include <stdio.h>

LCD4Bit_mod lcd = LCD4Bit_mod(2);

Messenger message = Messenger(); 

char data[16];
int pins[13]={23,25,27,29,31,33,37,41,43,50,44,38,49};
int dat;
float percent;
int speed1;
int gear;
int i;

// Define messenger function
void messageCompleted() {
  // This loop will echo each element of the message separately
  while ( message.available() ) {
    dat = message.readInt();
    speed1 = (float)message.readInt();
    sprintf(data,"%i %s        ",speed1,"km/h");
    gear = message.readInt();
    percent = dat / 7470.0;
    rev_meter((double)13.0 * percent);
    lcd.cursorTo(2,0);
    lcd.printIn(data);
  }
  
  
}

void setup() {
  int i;
  lcd.init();
  lcd.clear();
  lcd.printIn("LFS Interface");
  lcd.cursorTo(2,4);
  lcd.printIn("Arduino MEGA");
  for(i=0;i<13;i++)
  {
  pinMode(pins[i],OUTPUT);
  digitalWrite(pins[i],HIGH);
  delay(153);
  digitalWrite(pins[i],LOW);
  }
  lcd.clear();
  lcd.printIn("Current Speed :");
 // Initiate Serial Communication
  Serial.begin(115200);//115200 
  message.attach(messageCompleted); 
}

void loop() {
  
  // The following line is the most effective way of 
  // feeding the serial data to Messenger
  while ( Serial.available() ) message.process( Serial.read() );
 }



void rev_meter(int level)
{
  switch (level)
  {
    case 0:
    digitalWrite(pins[0],LOW);
    digitalWrite(pins[1],LOW);
    digitalWrite(pins[2],LOW);
    digitalWrite(pins[3],LOW);
    digitalWrite(pins[4],LOW);
    digitalWrite(pins[5],LOW);
    digitalWrite(pins[6],LOW);
    digitalWrite(pins[7],LOW);
    digitalWrite(pins[8],LOW);
    digitalWrite(pins[9],LOW);
    digitalWrite(pins[10],LOW);
    digitalWrite(pins[11],LOW);
    digitalWrite(pins[12],LOW);
    break;
    case 1:
    digitalWrite(pins[0],HIGH);
    digitalWrite(pins[1],LOW);
    digitalWrite(pins[2],LOW);
    digitalWrite(pins[3],LOW);
    digitalWrite(pins[4],LOW);
    digitalWrite(pins[5],LOW);
    digitalWrite(pins[6],LOW);
    digitalWrite(pins[7],LOW);
    digitalWrite(pins[8],LOW);
    digitalWrite(pins[9],LOW);
    digitalWrite(pins[10],LOW);
    digitalWrite(pins[11],LOW);
    digitalWrite(pins[12],LOW);
    break;
    case 2:
    digitalWrite(pins[0],HIGH);
    digitalWrite(pins[1],HIGH);
    digitalWrite(pins[2],LOW);
    digitalWrite(pins[3],LOW);
    digitalWrite(pins[4],LOW);
    digitalWrite(pins[5],LOW);
    digitalWrite(pins[6],LOW);
    digitalWrite(pins[7],LOW);
    digitalWrite(pins[8],LOW);
    digitalWrite(pins[9],LOW);
    digitalWrite(pins[10],LOW);
    digitalWrite(pins[11],LOW);
    digitalWrite(pins[12],LOW);
    break;
    case 3:
    digitalWrite(pins[0],HIGH);
    digitalWrite(pins[1],HIGH);
    digitalWrite(pins[2],HIGH);
    digitalWrite(pins[3],LOW);
    digitalWrite(pins[4],LOW);
    digitalWrite(pins[5],LOW);
    digitalWrite(pins[6],LOW);
    digitalWrite(pins[7],LOW);
    digitalWrite(pins[8],LOW);
    digitalWrite(pins[9],LOW);
    digitalWrite(pins[10],LOW);
    digitalWrite(pins[11],LOW);
    digitalWrite(pins[12],LOW);
    break;
    case 4:
    digitalWrite(pins[0],HIGH);
    digitalWrite(pins[1],HIGH);
    digitalWrite(pins[2],HIGH);
    digitalWrite(pins[3],HIGH);
    digitalWrite(pins[4],LOW);
    digitalWrite(pins[5],LOW);
    digitalWrite(pins[6],LOW);
    digitalWrite(pins[7],LOW);
    digitalWrite(pins[8],LOW);
    digitalWrite(pins[9],LOW);
    digitalWrite(pins[10],LOW);
    digitalWrite(pins[11],LOW);
    digitalWrite(pins[12],LOW);
    break;
    case 5:
    digitalWrite(pins[0],HIGH);
    digitalWrite(pins[1],HIGH);
    digitalWrite(pins[2],HIGH);
    digitalWrite(pins[3],HIGH);
    digitalWrite(pins[4],HIGH);
    digitalWrite(pins[5],LOW);
    digitalWrite(pins[6],LOW);
    digitalWrite(pins[7],LOW);
    digitalWrite(pins[8],LOW);
    digitalWrite(pins[9],LOW);
    digitalWrite(pins[10],LOW);
    digitalWrite(pins[11],LOW);
    digitalWrite(pins[12],LOW);
    break;
    case 6:
    digitalWrite(pins[0],HIGH);
    digitalWrite(pins[1],HIGH);
    digitalWrite(pins[2],HIGH);
    digitalWrite(pins[3],HIGH);
    digitalWrite(pins[4],HIGH);
    digitalWrite(pins[5],HIGH);
    digitalWrite(pins[6],LOW);
    digitalWrite(pins[7],LOW);
    digitalWrite(pins[8],LOW);
    digitalWrite(pins[9],LOW);
    digitalWrite(pins[10],LOW);
    digitalWrite(pins[11],LOW);
    digitalWrite(pins[12],LOW);
    break;
    case 7:
    digitalWrite(pins[0],HIGH);
    digitalWrite(pins[1],HIGH);
    digitalWrite(pins[2],HIGH);
    digitalWrite(pins[3],HIGH);
    digitalWrite(pins[4],HIGH);
    digitalWrite(pins[5],HIGH);
    digitalWrite(pins[6],HIGH);
    digitalWrite(pins[7],LOW);
    digitalWrite(pins[8],LOW);
    digitalWrite(pins[9],LOW);
    digitalWrite(pins[10],LOW);
    digitalWrite(pins[11],LOW);
    digitalWrite(pins[12],LOW);
    break;
    case 8:
    digitalWrite(pins[0],HIGH);
    digitalWrite(pins[1],HIGH);
    digitalWrite(pins[2],HIGH);
    digitalWrite(pins[3],HIGH);
    digitalWrite(pins[4],HIGH);
    digitalWrite(pins[5],HIGH);
    digitalWrite(pins[6],HIGH);
    digitalWrite(pins[7],HIGH);
    digitalWrite(pins[8],LOW);
    digitalWrite(pins[9],LOW);
    digitalWrite(pins[10],LOW);
    digitalWrite(pins[11],LOW);
    digitalWrite(pins[12],LOW);
    break;
    case 9:
    digitalWrite(pins[0],HIGH);
    digitalWrite(pins[1],HIGH);
    digitalWrite(pins[2],HIGH);
    digitalWrite(pins[3],HIGH);
    digitalWrite(pins[4],HIGH);
    digitalWrite(pins[5],HIGH);
    digitalWrite(pins[6],HIGH);
    digitalWrite(pins[7],HIGH);
    digitalWrite(pins[8],HIGH);
    digitalWrite(pins[9],LOW);
    digitalWrite(pins[10],LOW);
    digitalWrite(pins[11],LOW);
    digitalWrite(pins[12],LOW);
    break;
    case 10:
    digitalWrite(pins[0],HIGH);
    digitalWrite(pins[1],HIGH);
    digitalWrite(pins[2],HIGH);
    digitalWrite(pins[3],HIGH);
    digitalWrite(pins[4],HIGH);
    digitalWrite(pins[5],HIGH);
    digitalWrite(pins[6],HIGH);
    digitalWrite(pins[7],HIGH);
    digitalWrite(pins[8],HIGH);
    digitalWrite(pins[9],HIGH);
    digitalWrite(pins[10],LOW);
    digitalWrite(pins[11],LOW);
    digitalWrite(pins[12],LOW);
    break;
    case 11:
    digitalWrite(pins[0],HIGH);
    digitalWrite(pins[1],HIGH);
    digitalWrite(pins[2],HIGH);
    digitalWrite(pins[3],HIGH);
    digitalWrite(pins[4],HIGH);
    digitalWrite(pins[5],HIGH);
    digitalWrite(pins[6],HIGH);
    digitalWrite(pins[7],HIGH);
    digitalWrite(pins[8],HIGH);
    digitalWrite(pins[9],HIGH);
    digitalWrite(pins[10],HIGH);
    digitalWrite(pins[11],LOW);
    digitalWrite(pins[12],LOW);
    break;
    case 12:
    digitalWrite(pins[0],HIGH);
    digitalWrite(pins[1],HIGH);
    digitalWrite(pins[2],HIGH);
    digitalWrite(pins[3],HIGH);
    digitalWrite(pins[4],HIGH);
    digitalWrite(pins[5],HIGH);
    digitalWrite(pins[6],HIGH);
    digitalWrite(pins[7],HIGH);
    digitalWrite(pins[8],HIGH);
    digitalWrite(pins[9],HIGH);
    digitalWrite(pins[10],HIGH);
    digitalWrite(pins[11],HIGH);
    digitalWrite(pins[12],LOW);
    break;
    case 13:
    digitalWrite(pins[0],HIGH);
    digitalWrite(pins[1],HIGH);
    digitalWrite(pins[2],HIGH);
    digitalWrite(pins[3],HIGH);
    digitalWrite(pins[4],HIGH);
    digitalWrite(pins[5],HIGH);
    digitalWrite(pins[6],HIGH);
    digitalWrite(pins[7],HIGH);
    digitalWrite(pins[8],HIGH);
    digitalWrite(pins[9],HIGH);
    digitalWrite(pins[10],HIGH);
    digitalWrite(pins[11],HIGH);
    digitalWrite(pins[12],HIGH);
    break;
  }
}

for the picture of my assembly, i’ll try to post it as soon as i get my hands back on my camera ^^"

thank you all for your help in advance :slight_smile:

Cheers.

Lose the 4bit library, its outdated and slow. The included LiquidCrystal library is much faster and is officially supported. It won't be lightening fast with the new library (LCDs are generally slow) but it will be an order of magnitude faster that the old 4bit library.

Hi pluggy and thanks for your reply :slight_smile: the problem is that my lcd is soldered to the lcd keypad shield and its already on 4bits -___-

the problem is that my lcd is soldered to the lcd keypad shield and its already on 4bits -___-

It says it will work in either 4 or 8 bit mode. http://www.arduino.cc/en/Reference/LiquidCrystal

Lefty

Hi Lefty and thanks for your response ! you were right i used the library that already came with the arduino and now its working perfect ;) thanks a million :) and thank all of you out there :) Have a nice day ^^