LCD causing 4 second delay?

I have my LCD hooked up to my Arduino MEGA and its having a ton of delay? im making a wireless controller with 2 joysticks (which i stabalized) and a potentiometer to control the range of the joysticks. i hooked up the LCD and used these libraries for it

#include <LiquidCrystal.h> //LCD library: https://arduino-info.wikispaces.com/LCD-Blue-I2C
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//LCD library: http://www.sunfounder.com/wiki/index.php?title=File:LiquidCrystal_I2C.zip

it works but there is at least a 4 SECOND delay, on the code itself, there are no delays…what is the problem? if i comment out all the lcd code, the code runs smoothly in the monitor, when i add the lcd code back in…its soooo slow. i move my joystick up, 4 seconds later, the value of the stick comes in.

Whats causing the massive delay?

Whats causing the massive delay?

How the Hell do I know? Post your code. Use code tags.

How is your display wired to the Arduino? Is it a display with I2C backpack or a "plain" display which needs more than 4 wires to be connected to the pins of the Arduino?

-> in your includes section of the sketch there is at least one library too much: - if you have a I2C backpacked lcd display the first line is meaningless - if you got a plain lcd display, the I2C library is meaningless

masterfo:
I have my LCD hooked up to my Arduino MEGA and its having a ton of delay? im making a wireless controller with 2 joysticks (which i stabalized) and a potentiometer to control the range of the joysticks. i hooked up the LCD and used these libraries for it

#include <LiquidCrystal.h> //LCD library: https://arduino-info.wikispaces.com/LCD-Blue-I2C
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//LCD library: http://www.sunfounder.com/wiki/index.php?title=File:LiquidCrystal_I2C.zip

The above small code fragment is indicating 3 or more different LCD libraries.
Here are the possibilities:

  • The bundled <LiquidCrystal.h> - which could mean using Arduino pins vs i2c
  • (from the https link: fm’s <LiquidCrystal.h> - which could mean using Arduino pins vs i2c
  • fm’s LiquidCrystal_I2C
  • sunfounders LiquidCrystal_I2C
  • any number of other libraries called “LiquidCrystal_I2C”

If you want help, you must provide enough information for some one else to fully understand all the h/w and s/w you using. Think about all things another person would need to replicate your exact environment.
That is what is needed.

Whats causing the massive delay?

It is likely to be something wrong ins some piece of s/w, but without know anything about either the h/w or s/w being used, it is impossible to even guess.

— bill

Sorry guys i completely forgot the code lol
here it is:

#include <SPI.h>                            //this librarys let microcontrollers communicate with each other quickly over short distances(MISO-send data to master from slave)(MOSI-send data from master to peripherals)(SCK-serial clock,clock pulses which synchronize data transmission generated by the master) 
#include <RF24.h>
#include <nRF24L01.h>                       //UNO:  MOSI(pin11)MISO(pin12)SCK(pin13)(CE, CSN any pin)    
#include <printf.h>                         //MEGA: MOSI(pin51)MISO(pin50)SCK(pin52)(CE 9, CSN 53, or any pin)
#include <RF24_config.h>

//#include <LiquidCrystal.h>                  //LCD library: https://arduino-info.wikispaces.com/LCD-Blue-I2C
#include <Wire.h>                         
#include <LiquidCrystal_I2C.h>              //LCD library: http://www.sunfounder.com/wiki/index.php?title=File:LiquidCrystal_I2C.zip
LiquidCrystal_I2C lcd(0x27,20,4);           //setting display to 0x27 address; 20 columns, 4 rows;  UNO:SDA(A4)SCL(A5); MEGA:SDA(pin20)SCL(pin21)
                   
#define rightStick_y    A2          //my right joystick (y axis)
#define motorSpeed_Pot  A5          //my speed potentiometer that i will use to constrain the range of the joystick                    
int lifting_switch   = 44;          //the switch that will activate "RACING MODE" in the code            

void setup(){
  Serial.begin(115200);
  Serial.println("Starting");  
  lcd.init();                                         
  lcd.backlight();                                   
  pinMode(lifting_switch, INPUT);            
  pinMode(lifting_switch, HIGH);                             
}

//Dont mind these, these r just here so that i can use a function i made called smoothing
#define amountToAverage 20                           
#define x amountToAverage 
#define numOfSticks 6                                  
const int smoothingValue[numOfSticks] = {x,x,x,x,x,x}; 
int readings[numOfSticks][x]={};                       
int readIndex[numOfSticks] = {0,0,0,0,0,0};        
int total[numOfSticks] = {0,0,0,0,0,0};                       
int average[numOfSticks] = {0,0,0,0,0,0};       


void loop(){ 
  int right_y = analogRead(rightStick_y);
  right_y = smoothing(right_y,3);                                             //constantly read the first 20 values of the joystick and average them to smooth them out, basically average every 20 readings
  int lifting_sw = digitalRead(lifting_switch);      
  Serial.print("Buttons:");Serial.print(lifting_sw);Serial.print(" ");
                                                 
  right_y = calibrateJoystick(right_y, 6,500, 997, true);                     //stabalize the joystick and turn the 0-512-1023 values into 0-90-180 values 
  int defaultStick = calib_neutral(right_y, 90, 90);                          //ignore this, defaultStick will just be the alues of the right stick y axis, my variable "right_y" will change depending on the neutral position to run my brushless motor
  right_y = calib_neutral(right_y, 90, 90);                               //ignore this as well, for this example code, nothing changed from the "calibrateJoystick" function
  int speedPot = analogRead(motorSpeed_Pot);
  speedPot = isreverse(speedPot,true);                                      //my potentiometer was reversed so i unreversed it                                          
  speedPot = smoothing(speedPot,4);                                         //smooth out the values and make it stable
  speedPot = map(speedPot,0,966, 0,1023);                                  //map the speed pot to 0 to 1023
  speedPot = constrain(speedPot, 0,1023);                                   //constrain the values to 0 to 1023 (it sometimes reads 1024)
  Serial.print("CalibratedSpeedPot: ");
  Serial.print(speedPot);  
  Serial.print("           ");
  Serial.print("right Stick:  ");
  Serial.println(right_y);  

  lcd.clear();                                                  //clear the LCD and print the motor speed % on the 2nd ROW AT ALL TIMES               
  lcd.setCursor (0, 1);                          
  lcd.print(" Motor Speed: ");                
  int motorPercentage = map(speedPot,0,1023,0,100); 
  lcd.setCursor (16, 1);   
  lcd.print(" ");                     
  lcd.setCursor (14, 1);   
  lcd.print(motorPercentage);              
  lcd.setCursor (18, 1);     
  lcd.print("%");
 
  if (lifting_sw==0)
  {
          //Print "Racing Mode" in 1st row of Display
          //Print "Motor Speed: " and then the motor speed  percentage     
   lcd.setCursor (0, 0);                        
   lcd.print("     Racing MODE    ");          
   if (defaultStick > 90){
     int forwardStick = map(defaultStick, 90,180,0,100);
     lcd.setCursor (0, 3);                           
     lcd.print("    FORWARD");              
     lcd.setCursor (13, 3);                           
     lcd.print(forwardStick);
     lcd.setCursor (16, 3);                           
     lcd.print("%");
   }     
  }  
}

so what is causing the delay? there were a lot of user defined functions i made to calibrate the joysticsk and potentiometers because the readings were VERY unstable. Anyways, when i have the lcd code, there is a MASSIVE delay on the lcd screen and the serial monitor. when i take off the lcd code, there is no delay on the monitor

How about showing us all the code you are using?
The code you supplied does not compile.
It is missing several functions:

smoothing()
calibrateJoystick()
calib_neutral()
isreverse()

In terms of what the problem is, it impossible to say without having the code or knowing which specific arduino board and lcd module you are using.

But from looking at the code you have supplied:

  • you are clearing the screen every time through the loop.
    That will cause flicker.
  • You are using A5 which shouldn’t be a problem on mega but on Uno it is the I2C SCL pin.

If your i2c lines don’t have external pullups on them then this can cause very strange issues.
But on newer Arduino Mega boards the pullups are built in to the Arduino board. (but not on an UNO).

You need to fill us in on the real code you are using and what specific Arduino board are you using.

If you want to run a test of your i2c lines and your i2c module, you could install my hd44780 library package and run the included I2C expander backpack LCD diagnostic tool (I2CexpDiag)
hd44780 can be installed from the IDE library manger and you can read more about it here:

The i/o class you will want to use is hd44780_I2Cexp

— bill

Sorry guys the actual code is like 300-400+ lines so like i didnt think anyone wanted to go thru that mess lol, so i just trimmed it down and found the function that was causing the trouble. The function i made called “smoothing()” that was causing the delay. it takes in 10 readings ( or whatever i set the reading to be) and then gets the average, how do i fix the code to enable no delay?

#include <SPI.h>                            //this librarys let microcontrollers communicate with each other quickly over short distances(MISO-send data to master from slave)(MOSI-send data from master to peripherals)(SCK-serial clock,clock pulses which synchronize data transmission generated by the master) 
#include <RF24.h>
#include <nRF24L01.h>                       //UNO:  MOSI(pin11)MISO(pin12)SCK(pin13)(CE, CSN any pin)    
#include <printf.h>                         //MEGA: MOSI(pin51)MISO(pin50)SCK(pin52)(CE 9, CSN 53, or any pin)
#include <RF24_config.h>

//#include <LiquidCrystal.h>                  //LCD library: https://arduino-info.wikispaces.com/LCD-Blue-I2C
#include <Wire.h>                         
#include <LiquidCrystal_I2C.h>              //LCD library: http://www.sunfounder.com/wiki/index.php?title=File:LiquidCrystal_I2C.zip
LiquidCrystal_I2C lcd(0x27,20,4);           //setting display to 0x27 address; 20 columns, 4 rows;  UNO:SDA(A4)SCL(A5); MEGA:SDA(pin20)SCL(pin21)
                   
#define rightStick_y    A2          //my right joystick (y axis)
#define motorSpeed_Pot  A5          //my speed potentiometer that i will use to constrain the range of the joystick                    
int lifting_switch   = 44;          //the switch that will activate "RACING MODE" in the code            

void setup(){
  Serial.begin(115200);
  Serial.println("Starting");  
  lcd.init();                                         
  lcd.backlight();                                   
  pinMode(lifting_switch, INPUT);            
  pinMode(lifting_switch, HIGH);                             
}

//These are for the smoothing function:
#define amountToAverage 10                          
#define x amountToAverage 
#define numOfSticks 6                                  
const int smoothingValue[numOfSticks] = {x,x,x,x,x,x}; 
int readings[numOfSticks][x]={};                       
int readIndex[numOfSticks] = {0,0,0,0,0,0};        
int total[numOfSticks] = {0,0,0,0,0,0};                       
int average[numOfSticks] = {0,0,0,0,0,0}; 

int smoothing(int stick, int index){
  total[index] = total[index] - readings[index][readIndex[index]];        //Subtract the last reading
  readings[index][readIndex[index]] = stick;              // read from the sensor, 
  total[index] = total[index] + readings[index][readIndex[index]];        // add the reading to the total:
  readIndex[index] = readIndex[index] + 1;                  // go to the next position in the array:
  if (readIndex[index] >= smoothingValue[index]) {          // if we are reaching the end of the array...
    readIndex[index] = 0;                            // ...go to the the beginning:
  }
  average[index]= total[index] / smoothingValue[index];           // calculate the average:
  return(average[index]);
}

     

void loop(){                        
  int lifting_sw = digitalRead(lifting_switch);      
  Serial.print("Buttons:");Serial.print(lifting_sw);Serial.print(" ");                                               
  
  int right_y = analogRead(rightStick_y);  
  right_y = smoothing(right_y,0);                       //turns out this is causing the delay, i guess its because reading and averaging every 10 readings is making it slow? how do i fix it?
  Serial.print("right Stick:  ");
  Serial.println(right_y);          

  if (lifting_sw==0)
  {    
   lcd.setCursor (0, 0);                        
   lcd.print("     Racing MODE    ");          
   if (right_y > 93){                                       //if the joystick is up, do this:
     int RightFORWARD = map(right_y, 90,180,0,100);
     lcd.setCursor (0, 3);                           
     lcd.print("    FORWARD   ");              
     lcd.setCursor (14, 3);                           
     lcd.print(RightFORWARD);
     lcd.setCursor (17, 3);                           
     lcd.print("%");
   }    
   else if (right_y < 87){                                   //if the joystick is down, do this:
     int RightFORWARD = map(right_y, 0,90,100,0);
     lcd.setCursor (0, 3);                           
     lcd.print("    BACKWARD  ");              
     lcd.setCursor (14, 3);                           
     lcd.print(RightFORWARD);
     lcd.setCursor (17, 3);                           
     lcd.print("%");
   }  
   else{                                   //if the joy is in the middle, print this
    
     lcd.setCursor (0, 3);                           
     lcd.print("       NOTHING      ");  
   }    
  }   
}