Reading Serial and Writing to LCD Simultaneously

Hi Arduino masters. I'm having an issue with reading serial data and writing to a parallel Character LCD screen at the same time. LCD has that Hitachi HD44780 controller. I'm using ATMEGA2560. I can read serial data, or I can write to the LCD, just not at the same time. If I write to the LCD I have to use delay(500); or similar. Without the delay the information doesn't show on the LCD screen. Problem is reading from the serial requires the delay to not be there. If you use the delay, serial data is rarely read if that. I tried things like for loops with incrementing numbers and the LCD would write data if the number was between a certain range, etc. However, with no delay, the LCD flickers significantly. Its a 4 line display, the top line would be darker and the following lines would be progressively dimmer/flickering.

profs77:
If I write to the LCD I have to use delay(500); or similar.

Half a second delay? Wow. Anyway, just rework your display routines to not use delay, but to keep track of the time and send things after a suitable interval.

I just now figured it out, hopefully everything continues to work as I enable the other parts of the program. I was trying the timing method you mentioned in many ways but couldn't get it to work well. Finally I found this page which mentioned about built in timers.

http://www.engblaze.com/microcontroller-tutorial-avr-and-arduino-timer-interrupts/

First I used Timer 0 or Timer 1, it worked out ok, however, PWM didn't seem to work well or analogWrite(); I think what happened was PWM stuff uses the timing in Timer1. Since I am using ATMEGA2560, there multiple timers built in so I got it to work with Timer 3.

Perhaps its a bug in the code? Maybe its writing the information but when its done it continues to write blanks quickly overwritng the info before you can see it?

winner10920:
Perhaps its a bug in the code? Maybe its writing the information but when its done it continues to write blanks quickly overwritng the info before you can see it?

I thought of this, so I set flags in the code. So that it would write to the LCD only one time, but a lot of crazy stuff happened, it slowed down the processing bad enough to where it was too slow to read serial. It ended up working out with the built in timers though, see above.

You are asking us to troubleshoot a problem that is probably related to your code, yet you have not provided us with your code nor have you even outlined what you are attempting.

The title of your post is "Reading Serial and Writing to LCD Simultaneously" but I hope you realize that this is impossible. The processor can only do one thing at a time so most likely you are (1) reading some sort of sensor, (2) sending the sensor data to the Arduino via a serial link, and then (3) displaying this data on the LCD. These are being done sequentially, not simultaneously. You have these steps in a loop, and each trip around the loop happens in a short amount of time, so everything appears to be simultaneous.

The problem is that the LCD is a very slow device whose display does not need to be refreshed. The only time you have to write to the display is when the data changes. So, you may be able to fix things up by having your routine compare the current data to the previous data and only write to the display when the information changes. You will still run into 'blocking' (where time delays keep the processor from doing anything else) due to the LCD routines but, depending on how fast your data changes, this may not be a problem.

Don

it is a very popular problem for those who have to refresh an LCD very often.
if the data you want to show don't change often over time, you can tell the program to update the screen only if the old data differs from the newer one.

code expample:

void loop()
{
  /* read your value here */
  if(current_val!=old_val)
  {
    /* update the screen here */
    old_val = current_val;
  }
}