Loading...
Pages: [1]   Go Down
Author Topic: *Solved- I think* LCD random character display but Serial output fine  (Read 930 times)
0 Members and 1 Guest are viewing this topic.
New Jersey
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

* Solved - after reading the basic Hello World tutorial again I added the 10K pot, instead of the pin 9 contrast control, and things seem fine.  I did wire it originally based on another diagram using pin 9 to feed the VO, but evidently that was not working consistently. I am going to go back and look that one up to see if I missed something.
*
Trying to figure out some strange behavior of my LCD.  Anyway the example code runs fine for a period of time but usually after a minute or so I start to see random characters on the LCD but the serial monitor shows the correct words.  Can someone else run this with a simple LCD set up and see if it maybe is bad hardware that I have.  This is pretty much my first project, obviously a pear down version to isolate the issue I'm having, so I don't have an extra LCD to try it on.

Code:
/*
 * Simple test program to demonstrate issue I am having with a
 * LCD display and working with char arrays.
 * After some random amount of cylces, usually before one minute has
 * elapsed the LCD display goes haywire, displaying
 * crazy characters and then it will come back to
 * normal.  Could it be hardware or wiring?  I am using
 * a breadboard, standard 16x2 LCD, wired like the example code
 * for the LCD, could one of the wires be intermittently bad or the
 * LCD itself?  I was thinking memory leak but the serial display
 * show the words normal, so it has to have something to do with the LCD.
 */

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int CONTRAST_PIN = 9;
const int BACKLIGHT_PIN = 7;
const int CONTRAST = 100;

void setup()
{
  Serial.begin(9600);   // opens serial port  
  // Switch on the backlight and LCD contrast levels
  pinMode(CONTRAST_PIN, OUTPUT);
  pinMode(BACKLIGHT_PIN, OUTPUT);

  digitalWrite(BACKLIGHT_PIN, HIGH);
  analogWrite (CONTRAST_PIN, CONTRAST);
    
  lcd.begin(16,2);               // initialize the lcd
  lcd.home ();                   // go home
  lcd.print("   LCD Test");  
  lcd.setCursor ( 0, 1 );        // go to the next line
  lcd.print (" Initializing  ");
}

char lcdLines[2][17];

boolean bSerial = true;
boolean bLcd = true;

void loop()
{
 
  char * strOnOff[] = {"On", "Off"} ;
  char * strRelay[] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"};
  int iRelayPos = 0;
  int iStatePos = 0;
  int iLen = 0;
  
  while(1)  // loop so variables don't get initialized each loop
  {
    // clear the two LCD storage lines
    strcpy(lcdLines[0],"");
    strcpy(lcdLines[1],"");
        
    add2Message("* Relay State *");
    add2Message(strRelay[iRelayPos]);
    add2Message("is");
    add2Message(strOnOff[iStatePos]);

    if (bSerial) {
      Serial.println(lcdLines[0]);
      Serial.println(lcdLines[1]);
    }

    if (bLcd) {
      lcd.clear();    
      iLen = int((16 - strlen(lcdLines[0])) / 2); // center the text
      lcd.setCursor(iLen,0);      
      lcd.print(lcdLines[0]);
      iLen = int((16 - strlen(lcdLines[1])) / 2); // center the text
      lcd.setCursor(iLen,1);      
      lcd.print(lcdLines[1]);
      lcd.noCursor();
    }
    
   // random stuff for testing
   iRelayPos = random(0,11);
   iStatePos = random(0,2);
   delay(2000);  
      
  }
  
} // End of loop()


/* Add the messages to either the first line or the second
   depending on if there is enough room on line one.
   A simple word wrap routine.
*/
void add2Message(const char *source) {
  int iLen;
  
  iLen = strlen(lcdLines[0]);
  iLen = (iLen + strlen(source));
  iLen = (iLen + 1);
  if  ((iLen <= 16) && (strlen(lcdLines[1]) == 0)) {
    if (strlen(lcdLines[0]) != 0) {strcat(lcdLines[0]," ");}  
    strcat(lcdLines[0],source);
  }
  else {
    if (strlen(lcdLines[1]) != 0) {strcat(lcdLines[1]," ");}  
    strcat(lcdLines[1],source);
  }    
}

« Last Edit: February 11, 2012, 10:10:15 pm by BassnHarp » Logged

Offline Offline
Edison Member
*
Karma: 3
Posts: 1712
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It is probably interference or a loose connection, do you have it on a breadboard?
Logged

Canada
Offline Offline
Sr. Member
****
Karma: 0
Posts: 319
Sometimes teaching, always learning,
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Glad you found the problem. I don't think a PWM will control the contrast satisfactory without a filter.
Just another point, put your variables in the setup() so you don't need to do a while(1) loop within the loop().
Logged


New Jersey
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Would putting them in the setup() make them global?  Or would I need to put them outside of the setup() like I did with the Boolean's?
Logged

Canada
Offline Offline
Sr. Member
****
Karma: 0
Posts: 319
Sometimes teaching, always learning,
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Read the code comments below. I tried to illustrate the point smiley
Code:
int GlobalVariable; //this will work in all funcions

void Setup(){
  int LocalSetupVariable = 200;  //this will only work in Setup
  GlobalVariable = 100;         //this works here because it is declared as a global variable
}

void loop(){
  int LocalLoopVariable = 300;  //this will only work in loop
 
  Serial.print(GlobalVariable + LocalLoopVarable); //this will print 400
  Serial.print(GlobalVariable + LocalSetupVarable); //this won't work. LocalSetupVariable is not declared in loop.

}
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey
ive got my code working and im able to see the output on the serial monitor but when i go to display this onto my lcd random characters show up instead of the characters.

im using arduino uno, arduino ethernet and 16x2 lcd

here is my code
Code:
// include the library code:
#include <LiquidCrystal.h>
#include <SPI.h>
#include <Ethernet.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

byte server[] = { 212,1,210,253 }; //ip Address of the server you will connect to

//The location to go to on the server
//make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
String location = "/output.php HTTP/1.0";

// if need to change the MAC address (Very Rare)
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x0A, 0xA0 };

////////////////////////////////////////////////////////////////////////

EthernetClient client;

char inString[190]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?

void setup()
{
  lcd.begin(16,2);
  Ethernet.begin(mac);
  Serial.begin(9600);
}

void loop()
{
  String pageValue = connectAndRead(); //connect to the server and read the output
  Serial.println(pageValue); //print out the findings.
  lcd.print(pageValue);
  delay(8000); //wait 8 seconds before connecting again
}

String connectAndRead()
{
  //connect to the server

  Serial.println("connecting...");

  //port 80 is typical of a www page
  if (client.connect(server, 80))
  {
    Serial.println("connected");
    client.print("GET ");
    client.println(location);
    client.println();

    //Connected - Read the page
    return readPage(); //go and read the output

  }
  else
  {
    return "connection failed";
  }

}

String readPage()
{
  //read the page, and capture & return everything between '<' and '>'
  stringPos = 0;
  memset( &inString, 0, 190 ); //clear inString memory

  while(true)
  {
    if (client.available())
    {
      char c = client.read();

      if (c == '<' )
      { //'<' is our begining character
        startRead = true; //Ready to start reading the part
      }
      else if(startRead)
      {

        if(c != '>')
        { //'>' is our ending character
          inString[stringPos] = c;
          stringPos ++;
        }
        else
        {
          //got what we need here! We can disconnect now
          startRead = false;
          client.stop();
          client.flush();
          Serial.println("disconnecting.");
          return inString;
        }
      }
    }
  }
}



please help me smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey...Ive got my code working and i can see the output on the serial monitor
but when i go to display the output to my 16x2 lcd random characters get displayed instead of the actual output
here is my code
Code:
// include the library code:
#include <LiquidCrystal.h>
#include <SPI.h>
#include <Ethernet.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

byte server[] = { 212,1,210,253 }; //ip Address of the server you will connect to

//The location to go to on the server
//make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
String location = "/output.php HTTP/1.0";

// if need to change the MAC address (Very Rare)
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x0A, 0xA0 };

////////////////////////////////////////////////////////////////////////

EthernetClient client;

char inString[190]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?

void setup()
{
  lcd.begin(16,2);
  Ethernet.begin(mac);
  Serial.begin(9600);
}

void loop()
{
  String pageValue = connectAndRead(); //connect to the server and read the output
  Serial.println(pageValue); //print out the findings.
  lcd.print(pageValue);
  delay(8000); //wait 8 seconds before connecting again
}

String connectAndRead()
{
  //connect to the server

  Serial.println("connecting...");

  //port 80 is typical of a www page
  if (client.connect(server, 80))
  {
    Serial.println("connected");
    client.print("GET ");
    client.println(location);
    client.println();

    //Connected - Read the page
    return readPage(); //go and read the output

  }
  else
  {
    return "connection failed";
  }

}

String readPage()
{
  //read the page, and capture & return everything between '<' and '>'
  stringPos = 0;
  memset( &inString, 0, 190 ); //clear inString memory

  while(true)
  {
    if (client.available())
    {
      char c = client.read();

      if (c == '<' )
      { //'<' is our begining character
        startRead = true; //Ready to start reading the part
      }
      else if(startRead)
      {

        if(c != '>')
        { //'>' is our ending character
          inString[stringPos] = c;
          stringPos ++;
        }
        else
        {
          //got what we need here! We can disconnect now
          startRead = false;
          client.stop();
          client.flush();
          Serial.println("disconnecting.");
          return inString;
        }
      }
    }
  }
}



do i have to covert these characters somehow and then display it?

thanks
Logged

'round the world...
Offline Offline
Edison Member
*
Karma: 20
Posts: 2308
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Are the pins in the initialization of the LCD correct?

Also, you should clear the LCD before writing to it again.

Have you tried something like this

Code:
// include the library code:
#include <LiquidCrystal.h>
#include <SPI.h>
#include <Ethernet.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

byte server[] = { 212,1,210,253 }; //ip Address of the server you will connect to

//The location to go to on the server
//make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
String location = "/output.php HTTP/1.0";

// if need to change the MAC address (Very Rare)
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x0A, 0xA0 };

////////////////////////////////////////////////////////////////////////

EthernetClient client;

char inString[190]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?

void setup()
{
  lcd.begin(16,2);
  Ethernet.begin(mac);
  Serial.begin(9600);
  lcd.clear();
  lcd.print("hello, world!!");
}

void loop()
{
}


« Last Edit: October 15, 2012, 04:26:45 am by bubulindo » Logged

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 311
Posts: 35478
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@komalkristina
Take a look at this thread:
http://arduino.cc/forum/index.php/topic,126114.0.html
Identical code. Strange, wouldn't you say?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 311
Posts: 35478
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is the third active thread in this section of the forum with the same code in it. OP, knock it off. One thread is all you need, and one user ID. More will NOT get you more help. What it will get is us pissed off for wasting our time.
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Shannon Member
*****
Karma: 218
Posts: 13896
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@komalkristina - stop cross-posting. Threads merged.
Logged


Pages: [1]   Go Up
Print
 
Jump to: