Go Down

Topic: Problems implenting code on 20x4 LCD but code works on 16x2 (Read 2621 times) previous topic - next topic

kenneth87

Good day,

I have a problem implementing a similar code that is working on a 16x2 LCD to a 20x4 LCD.

They are both different branded LCDs

16x2 is a freetronics 16x2 LCD
&
20x4 is a Optrex 20x4 LCD

I'm sure the connection for my 20x4 is correct because i got it to display simple println stuff and every single pixel is okay.

this is the code i used:

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>
#include <TextFinder.h>
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
//LiquidCrystal lcd(8,9,4,5,6,7);  //16x2 board
LiquidCrystal lcd(12,11,7,6,5,4);  //20x4 board

byte mac[] = { 0xA2, 0xB1, 0x9A, 0xD2, 0xB3, 0xC5 };
byte ip[] = {192,168,1,25}; //check ip
byte server[] = {128,242,240,20}; // Twitter
char serverName[] = "api.twitter.com";  // twitter URL
char tweet[140];

EthernetClient client;

TextFinder  finder( client ); 

void setup()
{
  lcd.begin(20,4);
  //lcd.begin(16,2);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.write("Initializing");
  lcd.setCursor(0,1);
  lcd.print("Twitter2LCD...");
  delay(3000);
  Ethernet.begin(mac,ip);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.write("Making HTTP");
  lcd.setCursor(0,1);
  lcd.write("request...");
  delay(2000);
}


also my 20x4 LCD is connected this way.

LCD pin 14(DB 7) ----> Arduino digital pin 4
LCD pin 13(DB 6) ----> Arduino digital pin 5
LCD pin 12(DB 5) ----> Arduino digital pin 6
LCD pin 11(DB 4) ----> Arduino digital pin 7

right now, it only displays "Initialising Twitter2LCD" and not "Making HTTP request".

there's more in void(loop) but it's pointless to post it cus the 16X2 goes all the way through the whole program but not the 20X4.

Appreciate the help!

cheers!

dxw00d

Are you using the Arduino Ethernet shield? I think that uses pins 11 and 12.

kenneth87

i'm using a Freetronics EtherTen board. it has a similar configuration as the Uno+ethernet shield only on one board.

dxw00d

According to the Freetronics website, that board also uses pins 11 and 12 for communication with the Ethernet and SD card parts. That would explain why the LCD works before the Ethernet.begin(), but not after.

kenneth87

goodness. i didn't realise that! thanks for your help mate. i'll update when i resolder tomorrow!

Cheers!

kenneth87

Good day,

now my code goes to the Making HTTP connection state then it shows gibberish that fills the whole screen..

Code: [Select]
void loop()
{
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.write("Attempting to");
    lcd.setCursor(0,1);
    lcd.write("connect...");
    delay(2000);
{
  if (client.connect(serverName, 80)) {
    TextFinder  finder( client,2 );
    client.println("GET http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=kennethbeatle HTTP/1.0");  // twitter rss for
    client.println();
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.write("Connected");
    lcd.setCursor(0,1);
    lcd.write("to server!");
    delay(2000);
  }
  else {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.write("Could not");
    lcd.setCursor(0,1);
    lcd.write("find server...");
    delay(2000);


I used LiquidCrystal lcd(9,8,7,6,5,4); //pin 4(RS on LCD) to pin 9 on arduino and pin 6(enable) to pin 8 on arduino.

appreciate your help!

floresta

Quote
now my code goes to the Making HTTP connection state then it shows gibberish that fills the whole screen..

I am not a proficient C programmer but it appears to me that there is some code missing from your loop().  From what I can see it appears that that the stuff that you have shown should really be in setup().

One thing you should try to avoid is the use of lcd.clear() in a loop.


Don

kenneth87

hey floresta,

i'm quite sure the code is right because this same code runs perfect on my other 16x2 which is made to connect to my arduino board seamlessly.

Could you run through the connections for the 20x4 LCD for me?

I've used the two different grounds on the arduino under the power headers. one from the pot, one directly from LCD pin 16. will that affect anything?

Far-seeker


One thing you should try to avoid is the use of lcd.clear() in a loop.[/color]


Right it will clear the LCD every time the main loop is run, something that isn't necessary unless the output has changed.  This can be avoided with a flag value and some conditional logic like the following:

Code: [Select]
if (clearLCDFlag == true) lcd.clear();

kenneth87

good day guys,

Sorry don't mean to be stubborn but i still think there's something wrong with the connection?

i'll use pictures to explain my point.

The first three pics shows the code working on a Freetronics EtherTen board and Freetronics LCD shield.







next the 20x4 LCD.







noticed how the words get cut off and is misaligned? they are all meant to be on the first two lines of the LCD.

i suspect it's a connection problem but i have no idea what to correct.

any help?

floresta

Quote
Could you run through the connections for the 20x4 LCD for me?

It can't be the connections because you are getting legible information on the display.

It could be an 'out of spec' display.  There have been a few recent cases where the standard LiquidCrystal library does not work with these displays and the LiquidCrystal1.0 library does.  This library was written for the 40x4 displays but it also works with the smaller displays.  Start here --> http://code.google.com/p/liquidcrystal440/ and use the Downloads link to get the latest version.


Don

floresta

Quote
Right it will clear the LCD every time the main loop is run, something that isn't necessary unless the output has changed.

You really should avoid using lcd.clear() even if the output has changed.  You are usually better off just overwriting the old information with the new.

Don

komalkristina

hey i have a similar problem but i can't display on 16X2 LCD this is my code
can u see where i went wrong please.. i only want to display the tweet on my LCD
Thanks

#include <SPI.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x0A, 0xA0 };
IPAddress ip(152,62,89,135);

EthernetClient client;

const unsigned long requestInterval = 100000;  // delay between requests

char serverName[] = "api.twitter.com";  // twitter URL

boolean requested;                   // whether you've made a request since connecting
unsigned long lastAttemptTime = 0;            // last time you connected to the server, in milliseconds

String currentLine = "";            // string to hold the text from server
String tweet = "";                  // string to hold the tweet
boolean readingTweet = false;       // if you're currently reading the tweet

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
{
  lcd.begin(16, 2);
  currentLine.reserve(256);
  tweet.reserve(150);

  Serial.begin(9600);

  Serial.println("Attempting to get an IP address using DHCP:");

  if (!Ethernet.begin(mac))
  {
   
    Serial.println("failed to get an IP address using DHCP, trying manually");

    Ethernet.begin(mac, ip);
  }

  Serial.print("My address:");

  Serial.println(Ethernet.localIP());

  connectToServer();
}



void loop()
{
  if (client.connected())
  {
    if (client.available())
    {
     
      char inChar = client.read();

      currentLine += inChar;

      if (inChar == '\n')
      {
        currentLine = "";
      }
      if ( currentLine.endsWith("<text>"))
      {
        readingTweet = true;
        tweet = "";
      }
 
      if (readingTweet)
      {
        if (inChar != '<')
        {
          tweet += inChar;
        }
        else
        {

          readingTweet = false;
          lcd.print(tweet);
          Serial.println(tweet);   
       
          client.stop();
        }
      }
    }   
  }
  else if (millis() - lastAttemptTime > requestInterval)
  {
    connectToServer();
  }
}

void connectToServer()
{
  Serial.println("connecting to server...");
 
  if (client.connect(serverName, 80))
  {

    Serial.println("making HTTP request...");

    client.println("GET /1/statuses/user_timeline.xml?screen_name=KomalMaisuria&count=1 HTTP/1.1");
    client.println("HOST: api.twitter.com");
    client.println();
  }

  lastAttemptTime = millis();
}

floresta

@komalkristina
You really should start a new thread for this since you don't appear to be using a 20x4 display.

Here are some things to include in your new thread:

Did your display work correctly for something simple like the 'Hello world' sketch?
What do you expect to appear on your display and what do you actually see on the display?

Also - Highlight the portion of your post that is program code and then use the 'Code' button, which looks like --> #.


Don





Go Up