Pages: [1]   Go Down
Author Topic: LCD rightmost character not refreshed properly  (Read 1019 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all

We have here a very simple program. First read pot, map value then display to LCD.

Check out this YouTube clip.
http://youtu.be/vY9wJGrQ93A

Program is running, we turn the pot and value is changing on display. Problem is somehow the rightmost character is not being refreshed properly. On the clip you will see when 1000 mA drops to 900 mA, there is a double "A" displayed. Something wrong with the code or our LCD?

BTFDev.

Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

unsigned int minAmpSet = 0;
unsigned int maxAmpSet = 3000;
unsigned int sensorValue;

void setup() {
  lcd.begin(16, 2);
}

void loop() {
  // read the input on analog pin 0:
  sensorValue = analogRead(A0);
  
  sensorValue = constrain(sensorValue, 0, 1023);
  sensorValue = map(sensorValue, 0, 1023, minAmpSet, maxAmpSet);
  
  lcd.setCursor(0, 0);
  lcd.print("Current");
  
  lcd.setCursor(0, 1);
  lcd.print(sensorValue);
  lcd.print("mA");
  delay(500);
}
Logged

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 114
Posts: 4979
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I had exactly this lastr week... the screen doesn't "refresh" as such, it simply writes what you send it each time, leaving previous characters in play. You're sending fewer characters to an area where there was a longer string in the previous write, so the last character is "still there" from the previous time.

So first when you send 6 characters it says:
1000mA

then you send 5 characters and so it says:
900mAA

since the "A" of "1000mA" is still on the screen.

I added a line to my code to write a "          " to clear out the appropriate part of the display between each write. Not sure if there's a more elegant way, but that worked for me...

HTH?
« Last Edit: June 10, 2012, 12:36:14 am by JimboZA » Logged

The Elders of the Internet know who I am
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 227
Posts: 14053
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

small remark about the code...

Quote
sensorValue = constrain(sensorValue, 0, 1023);
Not needed as analogRead() will allways within this range

Some LCD's have a clear function or clear line function which is faster than writing spaces.

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the input.

The more I read about this subject, I found more reason to believe something wrong with the LCD.

http://www.hacktronics.com/Tutorials/arduino-character-lcd-tutorial.html

Hacktronics posted their project, their usage of LCD and code, I don't see anything specific or additional white space needed. This is very strange.

BTFdev.
Logged

Dallas, TX USA
Offline Offline
Faraday Member
**
Karma: 70
Posts: 2766
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


The more I read about this subject, I found more reason to believe something wrong with the LCD.


Go back and read response #1.
It explains what is happening.
You are not printing the same amount of characters all the time.
Some times you print 6 and sometimes you print 5.
When you print 5 characters "900mA" after printing 6 "1000mA" there will be an
extra left character on the display from the previous printing of the 6 characters.
If you don't want "trash" to be left over when you update the display you must ensure
that you either clear the display (which will cause flicker) or completely overwrite all the previous
characters with the new characters.
You are not doing either of those, so you see "trash" left over on the display.
In your case you are seeing a left over "A" from when you write 6 characters and then
you start to only write 5 characters.

To avoid flicker you will need to add code to check for the value and print a <SPACE> when it starts
to fall below certain points.
i.e
Code:
* * *

if(sensorValue < 1000)
  lcd.print(' ');
if(sensorValue < 100)
 lcd.print(' ');
if(sensorValue < 10)
 lcd.print(' ');
lcd.print(sensorValue);

* * *

Then print the sensor value.
This will ensure that you always print 4 characters for the sensor value.
The leading digits will be earase. If you want you can change the <SPACE> to '0'
to get leading zeros.


--- bill
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh boy. Thank you for clearing things up.

We always thinking about screen refresh and never thought this is actually by design.

Thanks, BTFDev
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 76
Posts: 7307
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Read about sprintf if you want an elegant solution. I have a tutorial on this subject.

http://liudr.wordpress.com/2012/01/16/sprintf/
Logged


Dallas, TX USA
Offline Offline
Faraday Member
**
Karma: 70
Posts: 2766
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Rather than using sprintf() and having to worry about how large of a buffer to allocate,
and formatting strings eating up your SRAM,
here is a little set of macros and functions I wrote that will give you printf() support for your lcd.
It also automagically pushes the formatting strings into flash so they don't eat up precious SRAM.
It is contained in the header file lcdPrintf.h in the zip image attached.
(note: rather than printf() the name is lcdPrintf() )

See the instructions in the header file for how to use it.

Also, keep in mind that using the xxprintf() routines will eat up around 1.8k of code space.
So while the xxprintf() routines make output formating really easy, if the formatting is very simple and limited,
then using the Arduino supplied Print class functions can be substantially less code
even though using the Print class functions may require a bit of ugly code to handle some of the formatting.

--- bill

* lcdPrintf.h.zip (0.81 KB - downloaded 12 times.)
« Last Edit: June 11, 2012, 11:47:23 pm by bperrybap » Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 76
Posts: 7307
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Darn, iPad won't open zip files. I'll have to use my main computer to read your code. Right, formatting strings take up SRAM. I wonder if you have an SRAM buffer for formatting strings.
Logged


Pages: [1]   Go Up
Jump to: