LCD Display script getting stuck somewhere.

Let me start off by saying that I just started with arduino approximately 4 days ago, so please bear in mind some things might go over my head.

Well I embarked on the enthusiastic journey to link my computer and my arduino and transfer data from from one to the other. Figured I’d start with a project that would display RSS feed headlines from world news every couple minutes by way of a Python parsing script that sent the data via pyserial through the COM port to the Arduino. That worked well enough and I can readily read the RSS headlines on my 16x2 character LCD display.

The issue is how I am handling the characters. Here is the code I quickly bashed together for the display:

char incomingByte = 0;   // for incoming serial data
int startstring = 0;   // recognition of beginning of new string of data
int charcount = 0;    // counts char length for screen clear
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
         Serial.begin(9600);     
         lcd.begin(16,2);
         lcd.setCursor(0,0);
}

void loop() {

         // send data only when you receive data:
         if (Serial.available() > 0) {
           
                 incomingByte = Serial.read();
                 if ((incomingByte == '~') && (startstring == 1)){                   
                   startstring = 0;
                   delay(1000);
                   lcd.clear();
                   charcount = 0;
                   lcd.setCursor(0,0);
                 }
                 if (startstring == 1){
                   if (charcount <= 30){
                     lcd.print(incomingByte);
                     charcount = charcount++;
                     }
                   }
                   if (charcount == 31){
                     lcd.clear();
                     lcd.setCursor(1,0);
                     lcd.print(incomingByte);
                     charcount = 1;
                   } 
                   
                 if (incomingByte == '~'){
                   
                   startstring = 1;
                 }
         }
                 
                 delay(200);
             }

Note I am using LiquidCrystal440 for the LCD to fix issues with text wrapping on my 16x2 LCD. It essentially waits till it receives a ~, notifying it to start printing out data, then another ~, telling it to stop printing out data. It is supposed to output 31 or 32 characters (2 lines of 16 characters each), then clear the screen, reset the cursor to 0,0 and continue writing. However, after the 2nd screen clear, it seems to jam up after about 2 or 3 characters, for a total of 65 or 66 characters. I think I am hitting the limit of the LCD or Arduino memory, but am unsure of how to reset that mid program. Some assistance would be very much appreciated.

                     charcount = charcount++;

It’s time to learn what the ++ operator does.

charcount++;

is equivalent to
charcoutn = charcount + 1;

So, your code is equivalent to:

                     charcount = charcount = charcount + 1;

which look plain silly, doesn’t it?

It essentially waits till it receives a ~, notifying it to start printing out data, then another ~, telling it to stop printing out data.

No, that is not true. There is only one test for the character being ~, and that is a compound test. You want to use nested ifs.

if(incomingByte == '~')
{
   if(startstring == 1)
   {
      // Do something
   }
   else
   {
      // Do something else
   }
}

}

Thanks for the clarification on the coding. C++ is not my forte, so I need to go back and clean it up.

As for the problem, I believe it is the serial receive buffer on the Arduino filling up to 64 bytes instantly, then dropping the rest of the data, causing it to never receive the ending ‘~’ and getting stuck waiting for it.
I obviously need to go back and fix the rate at which my python script sends data to the arduino.

As for the problem, I believe it is the serial receive buffer on the Arduino filling up to 64 bytes instantly, then dropping the rest of the data, causing it to never receive the ending '~' and getting stuck waiting for it.

It couldn't have anything to do with the fact that you only check for it as the beginning character?

PaulS:

As for the problem, I believe it is the serial receive buffer on the Arduino filling up to 64 bytes instantly, then dropping the rest of the data, causing it to never receive the ending '~' and getting stuck waiting for it.

It couldn't have anything to do with the fact that you only check for it as the beginning character?

I check for it twice. The beginning ~ is checked by the statement at the end

if (incomingByte == '~'){
                   
                   startstring = 1;
                 }

The end ~ is checked by the statement at the beginning

if ((incomingByte == '~') && (startstring == 1)){                   
                   startstring = 0;
                   delay(1000);
                   lcd.clear();
                   charcount = 0;
                   lcd.setCursor(0,0);

The problem was nothing to do with Arduino (well short of having only a 64 byte buffer). The data was sent by python as a string, resulting in anywhere from 1 to thousands of characters being crammed into the serial faster than the Arduino could handle them. Once the buffer filled up to 64 chars, the rest of the message was dropped. I changed the python coding to cut the string into 1 word segments and send them one at a time with a delay between sending, so the Arduino can keep up with the rate. Everything works properly now.

I check for it twice. The beginning ~ is checked by the statement at the end

Oops. Sorry. Missed that. When I went back and looked at your code, though, I way why.

Try using Tools + Auto Format, and see if that doesn't make your code easier to read. The random indents are hard to match up. Also, I prefer each { on a new line, like I showed in my first response. The start and end of a block are clearer, to me, when the curly braces line up.

The problem was nothing to do with Arduino (well short of having only a 64 byte buffer). The data was sent by python as a string, resulting in anywhere from 1 to thousands of characters being crammed into the serial faster than the Arduino could handle them. Once the buffer filled up to 64 chars, the rest of the message was dropped. I changed the python coding to cut the string into 1 word segments and send them one at a time with a delay between sending, so the Arduino can keep up with the rate. Everything works properly now.

I don't really understand this. The Arduino can read data from the serial buffer orders of magnitude faster than the serial hardware can fill it. Though printing to the LCD slows things down, as do the calls to delay(). The delay() function is verbotten in a function that needs to read serial data faster than it arrives.

But, I'm happy that you have found a solution that works for you.