16x2 LCD Display 1 line only! Help!

Hi everyone, I really need your help. I can observed that my Lcd is working normally when I used Hello world code generated by arduino itself. But after I type my code in It become only viewable for 1 line while the second line I can't seem to see or write anything. I'm not sure what is wrong.... Hope someone can guide me to fix this problem.
I'm using Arduino UNO, LCD and Electrical conductivity circuit embedded system from atlas scientific.

Here's my code:

#include <stdlib.h>
#include <string.h>
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#define rxpin 2
#define txpin 3

SoftwareSerial myserial(rxpin,txpin);
LiquidCrystal lcd(12,11,7,6,5,4);

String inputstring ="";
String sensorstring ="";

boolean input_stringcomplete = false;
boolean sensor_stringcomplete = false;

byte arduino_only=0; // 0: computer send code 1 : manual send code
byte i;

char *tds;
char EC[50];

unsigned int s;
int tds_int;

void setup(){
  Serial.begin(38400);
  myserial.begin(38400);
  inputstring.reserve(5);
  sensorstring.reserve(30); 
  lcd.begin(0,0);
  lcd.print("TDS:");
}

void serialEvent(){
  char inchar = (char)Serial.read();
  inputstring += inchar;
  if (inchar =='\r')
  {input_stringcomplete = true;}
}

void loop(){
  
  if(input_stringcomplete){
    myserial.print(inputstring);
    inputstring ="";
    input_stringcomplete = false;
  }
  
  while (myserial.available()){
    char inchar = (char)myserial.read();
    sensorstring +=inchar;
    if(inchar == '\r')
    {sensor_stringcomplete = true;}
  }
 if(sensor_stringcomplete){
   Serial.print(sensorstring);
   lcd.print(sensorstring);
   sensorstring.toCharArray(EC , s);
   
   Serial.print("TDS:");
   Serial.println(tds);
   
   lcd.setCursor(0,1);
   lcd.print(EC);
   sensorstring="";
   sensor_stringcomplete = false;
 }
}

At my lcd , it only show the first line : TDS:2?
then the rest is empty plus I can also see a weird japanese letter after my data.

lcd.begin(0,0); should say how many columns and rows your LCD has.

Hello World example uses lcd.begin(16, 2);

It looks like the library is ignoring your lcd.begin(0,0) statement and using the default which is for a 16x1 display.

This is an unfortunate choice for a default value since there aren't many 1-line displays out there, 16x1 or otherwise. The vast majority of 16x1 displays are configured as 8x2 and must be initialized as such.

Don

Thanks Riva and Floresta! It save me a lot of time and brain power. Appreciate it alot!!!!!
By the way, have you came across this problem (see the attached photo below):

There's a weird japanese character appearing behind and after a while the pattern of ( 0 follow by the pattern will repeat on the first line) till it reach the end of the column. The external circuit is continuously reading data thus, it is transmitting data every 1 seconds, but the weird part is that everytime new data comes in , the original data on the lcd should be overwritten as I set the cursor at respective position before I transmit the data.

20140626_090628[1].jpg

I think your problem is with special charakter you're trying to use - char[50]. Look here for an idea how to declare and print special characters :http://forum.arduino.cc/index.php/topic,19002.0.html

The lcd library does not handle end of line processing.
It passes every character it receives to the LCD.
The LCD does not process end of line characters.
So if you send the LCD library a carriage return or a newline it gets passed directly
to the LCD and the LCD will use those character codes to print a user defined character.
is hex 0xd or decimal 13
(newline) is hex 0xa or decimal 10

The first 8 characters 0-7 are the user defined characters,
which are repeated for 8-15
maps to special character 5
maps to special character 2

If you attempt to print a string with either a or a in it
and don't define the matching custom character, you will see
garbage on the LCD display.
--- bill

Thanks Waski and bill for replying.

@Waski alright I will take a look later and try it out on my own.

@bill I fully understand that right now. But since it sending out a string of message with a carriage return at it's back how is it possible to remove away the special character when displaying the string of message or display it out properly. I would prefer to remove it away, and at the same time I've been trying to play around with the code to see whether it would remove away the undefined character such as print the unknown character after 2 line so it would be outside of my lcd.
Example : printf("%s\n\n",n)

Here's the new edited code:

if(sensor_stringcomplete){
   //Serial.print(sensorstring);
   lcd.setCursor(3,0);
   lcd.print(sensorstring);
   lcd.setCursor(9,0);  
   lcd.print("uS/cm______");
   lcd.setCursor(15,0);
   lcd.print("\n");
   
 //  Serial.print("EC:");
  // Serial.println("TDS:");
   
   Serial.println(sensorstring);  //so you can see the captured string 
   /*char carray[sensorstring.length() + 1]; //determine size of the array
   sensorstring.toCharArray(carray, sizeof(carray)); //put readStringinto an array
   int n = atoi(carray); //convert the array into an Integer  */
  //line 78&79 is the same as the comment out
   char* carray = &sensorstring[ 0 ]; //copy pointer to string
   int n = atoi(carray); //convert the string into an Integer 
   Serial.println(n);
   lcd.setCursor(0,1);
   lcd.print(n);
   
   if(z<=n)
   {
     digitalWrite(led,HIGH);
     delay(1000);
     digitalWrite(led,LOW);
     delay(1000);
   }
   
   delay(2000);
   sensorstring="";
   sensor_stringcomplete = false;
 }
}

You don't need this:

   lcd.print("\n");

You only need this line:

   lcd.setCursor(0,1);

luckystar94:
Thanks Waski and bill for replying.

@Waski alright I will take a look later and try it out on my own.

@bill I fully understand that right now. But since it sending out a string of message with a carriage return at it's back how is it possible to remove away the special character when displaying the string of message or display it out properly. I would prefer to remove it away, and at the same time I've been trying to play around with the code to see whether it would remove away the undefined character such as print the unknown character after 2 line so it would be outside of my lcd.
Example : printf("%s\n\n",n)

I don't think you are quite grasping the issue, given the latest example code is also
sending a newline to the LCD.
Neither the LiquidCrystal library nor the LCD support end of line processing.
So when you send a or character \r or \n
the LCD will not do any end of line processing:
no line wrapping, no advancing of lines, and no scrolling of lines.
The LCD will simply use those code values to
display characters from the user defined characters.

What you need to do is stop putting the carriage return into your string before
you call the lcd.print() function to print it.

It is your code that is putting the carriage return into the string.
This code puts the carriage return into the string:

void serialEvent(){
  char inchar = (char)Serial.read();
  inputstring += inchar;
  if (inchar =='\r')
  {input_stringcomplete = true;}
}

That code puts the carriage return in the string since it adds the character
before it checks to see what it is.
You can change that code to not put the carriage return in the string.
hint: check first, then only add the character when it isn't a \r

--- bill

Thanks Bill , After a long period of research , I'm now working on that , I'm using strtok or what we call a delimiter to remove the carriage return and it also can split the string.

luckystar94:
Thanks Bill , After a long period of research , I'm now working on that , I'm using strtok or what we call a delimiter to remove the carriage return and it also can split the string.

That seems like overkill for something like this.
All you need to do is throw away the carriage return when you see it rather than
stuff it into the string.

--- bill

Hi there, I see that this is an older post but it just helped me sort out a problem I had.
Thanks for the info, I had a carriage return printing to the lcd too.
I found and used printString.trim(); to remove it before sending it to the lcd.
Maybe this will help others too.
Frank

Maybe not.

You are approaching it from the wrong end! :astonished:

Just do not use lcd.println() and do not add CR or LF in the first place to any string you wish to display. The string does not build itself; you are the one who decides what to put in it.

That's true but in my case I get the line from the serial port and need the cr for another part of my program. Thanks again.

frankthetech:
That's true but in my case I get the line from the serial port and need the cr for another part of my program. Thanks again.

In my 30+ years of C programming, I've done lots of stuff using strings and serial ports and I've never had a case where I needed a or to be stored in the string for use by some other portion of code even if it was received in the serial data stream and used to delimit an end of line .

Typically the or newline is just a delimiter in the data stream and not needed for anything else as what is important is the string of characters that preceded the end of line termination.

newlines/CR are often a PITA to deal with when they end up being stored in the string, so often they are simply dropped when building up the string.

--- bill

OK I'll bite, yes the cr is used to delimit the input stream. So I print the line of text to the LCD and then write the line to an sd card file. Without the cr would the file write to the sd card not just be one long line of text?
But your right I could just print the line to the lcd when the cr is found.
Then do the sd card file write. I'll try it and see what heppens.

Or treat the line ending as just the delimiter of text/string input by stripping off the line endings as you detect them rather than blindly inserting strings of text that include the line endings.
(Some library input functions already do this for you)

Then when outputting the string, add any line ending that are desired/needed.
(some library output functions already do this for you)

The reason to do it this way is that handling line endings can get messy and the needed output line ending may not match the line ending used on the input.

Unix got it right by keeping it simple and always using newlines (linefeeds) to signify the end of line.
But CPM, DOS, and Windows use both and while Apple (even though now its os is unix based) tends to use just for a text line delimiter.

So the best thing to do is process the input into a null terminated string by removing the line endings and then when the string is sent to some output device or file, make sure any appropriate line endings are appended if the i/o library doesn't automatically do it.

For example, in your case, if you are reading from a serial link you might get or or possibly some other character, it depends on the serial device on the other end. For full flexibility, you'd have to handle all the possibilities.

Then as you have seen, you don't want any line endings going to the LCD.
And then when putting the string as a line of text in a file, the line endings in the file could be
one of 3:
for the MAC text files
for the DOS/Windows text files
for the *nix text files.

And this is why you often can't take a raw input stream and forward directly it to another output stream/device or file.

Similar issues come up when parsing strings. Putting the line endings in the string can make parsing messy and complicate since in reality, line endings are usually not part of the string.

Many things get simpler, if you view line endings as simply delimiters between strings in an input stream or output stream rather than being part of the actual string.

--- bill

Thanks again for the help! I did re-code the section that builds the line as you suggested.
I did add a new-line char the the part of the my code that builds a file write out of many lines of text,
as i need to wait for certain information that does not appear till about 5 lines of input before I can start the file on the sd card.