Problems with Serial Monitor and LCD screen

Hello everyone,
I try to make a code. In this code, the user type some text in the Serial monitor, the arduino shows the typed text on a LCD display, a piezo element gives three tones and the arduino have to scroll the display to show the whole text.

Here is the code I have made:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
boolean pieper = false;
String alpha;
int aantal = 0;

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // initialize the serial communications:
  Serial.begin(9600);
  pinMode(8, OUTPUT);
}

void loop() {
  // when characters arrive over the serial port...
  if (Serial.available()) {
    // clear the screen
    lcd.clear();
    // read all the available characters
    while (Serial.available() > 0) {
      // display each character to the LCD
      lcd.write(Serial.read());
      pieper = true;
      aantal = Serial.available();
      Serial.println("Aantal: "+aantal);
    }
  }

  if(pieper == true) {
    for(int i=0; i<3; i+=1) {
      tone(8, 1420, 500);
      delay(500);
      tone(8, 2021, 500);
      delay(500);
      tone(8, 2950, 500);
      delay(500);
      noTone(8);
      delay(1000);      
    }
    pieper = false;
  }


  
}

The problem is that I don’t know how to say to the Arduino he have to scroll automaticaly to the left, showing ALL the text. I have found the scrollDisplayLeft command, but how can I program that the arduino scrolls automaticaly to the left, so that all the text is displayed (the length of the text is not equal all the time)?

Regards,
Daan70

The problem is that I don't know how to

The problem is that you don't know how to read a packet.

Look at that sentence above. You can see that it expresses a complete thought (is a complete packet) because it ends with a period. That period is an end of packet marker.

You MUST read and store the data when it arrives, until the end of packet marker arrives. Then, you'll have an idea how much data there is to display, and you'll know whether to statically show it or to use scrolling.

Right now, you haven't a clue how much data there is to display.

Simple serial capture with an end of packet marker. You can change the comma marker to a period for more natural keyboard typing in the serial monitor.

//zoomkat 3-5-12 simple delimited ',' string  
//from serial port input (via serial monitor)
//and print result out serial port

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,
  
  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      //do stuff
      Serial.println(readString); //prints string to serial port out
      readString=""; //clears variable for new input      
     }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

Thank you both for your suggests!

I have tried to make a code, which combine the code from myself, Zoomcat and the information of PaulS; it worked. I have now the sketch I would make.

I have one more question: if you type a text, and the display scrolls, the arduino sets the same text after a while automatically at the LCD (after a few characters on the LCD). Is there a way to disable this?

Thanks in regard, Daan70

I have one more question

about some code you haven't posted.

if you type a text, and the display scrolls, the arduino sets the same text after a while automatically at the LCD

Psst. That's the definition of scrolling. What do YOU want?

I think I’ve translated it bad!

The code is the same as the first code I have made.
The problem is:

  • The arduino scrolls to the left (that’s what I want)

But after a while, the Arduino sets the same code from the beginning after some seconds. I have programmed that he have to scroll, but I want the Arduino to set the same text ONLY after I cleared the display, and not after a while.

Maybe a setting in the LiquidCrystal library. Is there a way to disable this?

Hello everyone,
This is my code:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
boolean pieper = false;
String readString;
String lcdDisplay;
int aantal = 0;
int aantalScrolls = 0;
int aantalCopy = 0;

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // initialize the serial communications:
  Serial.begin(9600);
  pinMode(8, OUTPUT);
}

void loop() {
  // when characters arrive over the serial port...
  if (Serial.available()) {
    // clear the screen
    lcd.clear();
    lcdDisplay = "";
    aantalScrolls = 0;
    // read all the available characters
    while (Serial.available() > 0) {
      char c = Serial.read(); aantal+=1;
    
      readString += c; //makes the string readString
    
    }
    
    // display each character to the LCD
      lcdDisplay += readString;
      readString = "";
      lcd.print(lcdDisplay);
      pieper = true;
  }

  

  if(pieper == true) {
    for(int i=0; i<3; i+=1) {
      tone(8, 1420, 500);
      delay(500);
      tone(8, 2021, 500);
      delay(500);
      tone(8, 2950, 500);
      delay(500);
      noTone(8);
      delay(1000);      
    }
    pieper = false;
    aantalCopy = aantal;
  }

  aantal = 0;
  
  if(aantalCopy > 16 && aantalScrolls < 3) {
    for(int i=0; i<aantalCopy; i+=1) {
    lcd.scrollDisplayLeft();
    delay(500);
  }
  aantalScrolls += 1;
  lcd.clear();
  lcd.print(lcdDisplay);
  delay(2000);
 }
  else if(aantalCopy > 16 && aantalScrolls == 3) {
    lcd.clear();
  }
  
  
}

I don’t know how to explain the problem properly, but I have made a simple image:

If I typ the sentence: ‘This is an example that I have made for you!’, I have the following steps…

  • The LCD displays the text, as you can see in ‘NUMBER 1’ of the appendix.
  • Then, the LCD begins to scroll the text to the left. After a while, the text is displayed fully and then I get my problem. The LCD paste the full text behind the last character, as you can see in ‘NUMBER 2’ of the appendix.

If the sentency I have typed is longer than an x. number of characters, the LCD split the text and set the second part of that splitted text to the next line.

I hope it’s clear to you all with the appendix and the code. If you have questions, please ask them!

Regards,
Me