20*04 LCD

Good day everyone, I am using a 20*04 LCD which is correctly wired to my Arduino, my problem is when it displays the information, it starts from line 1 skips line 2 and continues on line 3 before it goes back to line 2 and then line 4. Please help!

The data sheet for the driver explains the memory organization that leads to the described behavior. Look around page 10. Use the setCursor() function to control where text is displayed.

Okay, but how can I include setcursor when the text to be displayed is coming from the SMS sent to the GSM module

coming from the SMS sent to the GSM module

Sorry, I don't know what that means. Perhaps you can post a link for those modules and a schematic or your system. How is text being sent to the LCD?

aycoolt:
Okay, but how can I include setcursor when the text to be displayed is coming from the SMS sent to the GSM module

The command to set the cursor to line an column is a completely separate command that is sent before the message text is sent.

Your 4 line LCD is physically 2 two-line units stacked together. Each has it's own controller. The library takes care of the processing differences.

Paul

aycoolt:
Okay, but how can I include setcursor when the text to be displayed is coming from the SMS sent to the GSM module

You can easily break a received message up into pieces to display. The below assumes that you receive in a character array, not a String (capital S).

char sms[] = "This is message that was received and needs to be displayed!";

void setup()
{
  Serial.begin(250000);
  Serial.print("length of received message: "); Serial.println(strlen(sms));

  // 20 characters wide plus space for terminating '\0'
  char buffer[21];

  int offset = 0;
  while (offset < strlen(sms))
  {
    // clear the buffer
    memset(buffer, 0, sizeof(buffer));
    // copy part of sms
    memcpy(buffer, &sms[offset], 20);
    // print it
    Serial.print("'"); Serial.print(buffer); Serial.println("'");
    // for next part of sms
    offset += 20;
  }

  Serial.println("done");
}

void loop()
{
}

The only problem with this is that, in the last iteration, you might access memory that does not belong to the sms message. As it’s a ‘read’, it will not pose an issue; if it was a ‘write’, you would be overwriting something that is not part of the sms message and it will result in undefined behaviour somewhere in your code. A neater solution

void setup()
{
  Serial.begin(250000);
  Serial.print("length of received message: "); Serial.println(strlen(sms));

  // 20 characters wide plus space for terminating '\0'
  char buffer[21];

  int offset = 0;
  while (offset < strlen(sms) - 20)
  {
    // clear the buffer
    memset(buffer, 0, sizeof(buffer));
    // copy part of sms
    memcpy(buffer, &sms[offset], 20);
    // print it
    Serial.print("'"); Serial.print(buffer); Serial.println("'");
    // for next part of sms
    offset += 20;
  }
  memset(buffer, 0, sizeof(buffer));
  memcpy(buffer, &sms[offset], strlen(sms) - offset);
  Serial.print("'"); Serial.print(buffer); Serial.println("'");

  Serial.println("done");
}

Instead of the serial print statements, you can position the cursor on the desired row of the lcd (use a variable that indicates which row and increment that variable) and print to the lcd.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

We need to see your code, have you put in setup that you have a 20x4 display?

 lcd.begin(20, 4);

Have you got a single code just using the LCD to check the line by line commands?

Tom..... :slight_smile:

I want to try out your code @sterretje but other than adding the LCD library and array to your code, it should be complete right?

@paul, the message to be displayed will be a text message sent from a phone, so how do I include set cursor so it adequately breaks the messages into its precise lines on the LCD.

aycoolt:
@paul, the message to be displayed will be a text message sent from a phone, so how do I include set cursor so it adequately breaks the messages into its precise lines on the LCD.

You will HAVE to determine how to break the message into segments, Each segment is printed separately. Between printing each segment you have to set the LCD cursor, like my example from my oven control:

lcd.begin(LCDCOLSIZE, LCDROWSIZE); // initialize the lcd

lcd.setCursor(0, 0); // Splash screen
lcd.print(" JODECO DRYING OVEN ");
lcd.setCursor(0, 1);
lcd.print("SET TIM: TMP: "); // allow for a 4 digit time in munutes
lcd.setCursor(0, 2);
lcd.print("RUN TIM: TMP: ");

The first number in the parentheses is the position on the line, from 0 to 19. The second number is the line number on the 4 line display, line 0 to line 3. Where I print a text, you will print your message segment.

Paul

I have attached the code I’m using, it prints text sent from my phone to the Arduino on the LCD module.unfortunately the text aren’t arranged properly and line 3 comes before line 2. I don’t know how to add setcursor in the code since the message to be displayed isn’t going to be in the code. The message is a text sent via SMS from my mobile phone.

working_received.ino (1.65 KB)

Please add your code in your post, not as attachment. Not everybody is able to view ino files (e.g. when on a phone); your code is small enough. Please use code tags when posting here and first use menu -> tools -> autoformat in the IDE.

With regards to the code, what is it with all those

  do
  {
    while ( !SIM900.available() );
  } while ( '"' != SIM900.read() );

? ? ?

Simplest solution
Just read a character and write it to the display, read the next character and write it to the display; etc etc. Count how many you have written; when you reach a count of 20, use setCursor to move to the beginning of the next line and reset the counter.

Okay, I think I understand you @strretje, so the solution you proffered is what your codes does?

No, my code in reply #5 does not do that. With that, you first need to collect a full message.

Okay, so what modifications do I need to do to your code in #5 so messages received are broken and displayed orderly on the LCD?

Boss @ sterretje, please help me out, I'm not so great with the codes

Not the earlier described principle, just printing what is received and wrapping around at end of 20 chars.

void loop()
{
  static int row = 0;
  static int charCount = 0;

  if (Serial.available() > 0)
  {
    char ch = Serial.read();
    Serial.print(ch);
    if (ch == '\r')
    {
      row = charCount = 0;
      lcd.setCursor(0, 0);
    }
    else
    {
      lcd.write(ch);
      if (++charCount % 20 == 0)
      {
        row++;
        if(row == 4)
        {
          row = charCount = 0;
        }
        lcd.setCursor(0, row);
      }
    }
  }

}

Not perfect, leaves you something to puzzle.