String handling error for Digital Display

Hi All,

I'm trying to create a Digital Display for my MIDI Controller, and I am successfully receiving via a virtual serial port.
The issues I'm having are that I cannot seem to isolate and process an incoming serial message, so that it prints to the correct area of the 2 x 16 Digital Display.
Everything works as it should, until I send new data from the transmitter, and it appears twice on the DD screen, in the first instance it is printing into the correct area, but it then simultaneously prints to the first line / first column of the DD.

Has anyone any ideas as to how I might improve the code to eliminate the problems?


#include <LiquidCrystal.h>
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
#include <SoftwareSerial.h>
#define rxPin 14  // THIS IS THE ACTIVE VIRTUAL PORT
#define txPin 15
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);

String STRINGNAME;
String CUSTSTANstring;
String MODEstring;
String MAPPAGEstring;

unsigned long OSpreviousMillis;
int OSDelay = 100;

void setup() {
  lcd.begin(16, 2);
  mySerial.begin(9600);
  mySerial.setTimeout(10);
}

void loop() {
  unsigned long OScurrentMillis = millis();
  if (mySerial.available() > 0) {
   String STR = mySerial.readString();
     STRINGNAME = STR;


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

    if (STR != "1" or STR != "2" or STR != "3" or STR != "4"
        or STR != "5" or STR != "6" or STR != "7" or STR != "8" or STR != "9"
        or STR != ":" or STR != "CUSTOM  :       " or STR != "STANDARD:       ") {

       MAPPAGEstring = STR;
    }

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


    if (STRINGNAME == "1" or STRINGNAME == "2" or STRINGNAME == "3" or STRINGNAME == "4"
        or STRINGNAME == "5" or STRINGNAME == "6" or STRINGNAME == "7" or STRINGNAME == "8" or STRINGNAME == "9" or STRINGNAME == ":") {

      if (STRINGNAME == ":") {
        STRINGNAME = "10";
      }

      MODEstring = STRINGNAME;
    }

    if (STRINGNAME == "CUSTOM  :       ") {
      CUSTSTANstring = STRINGNAME;
    }
    if (STRINGNAME == "STANDARD:       ") {

      CUSTSTANstring = STRINGNAME;
    }
  }

  if (OScurrentMillis - OSpreviousMillis >= OSDelay)  // if enough time has elapsed
  {
    lcd.setCursor(0, 0);
    lcd.print(MAPPAGEstring);
    lcd.setCursor(0, 1);
    lcd.print(CUSTSTANstring);
    lcd.setCursor(9, 1);
    lcd.print("MODE   ");
    lcd.setCursor(14, 1);
    lcd.print(MODEstring);
    lcd.noCursor();  
    OSpreviousMillis = OScurrentMillis;
  }
}

Please post a photo showing that.

Hi PaulRB

Here is the display in its normal state:

Here is the display after the Standard / Custom toggle switch has been selected:

And here is after the Mode dial has been turned:

Cheers

If the same text is showing on both lines, then these two Strings must contain the same text.

MAPPAGEstring is set here:

So I guess you expect that condition won't be true if STR is "STANDARD: " and so MAPPAGEstring should not get updated?

Is it possible that there are more or fewer spaces after the colon than you were expecting?

Maybe try printing what you received from the software serial port to the Serial Monitor. Put some delimiters around the string so you can see how many spaces there are at the end of the text.

I've taken the Print to Serial Monitor statements out of the code above to try to keep it to the source code, but have previously tried your suggestions, and also have tried the "delimiter" idea, and nothing seems awry.
I'm stumped as to why it's behaving this way!

please show the code how you "tried" the delimeters and what was on the display with that code.

The pictures above show that @PaulRB is right and the number of spaces in the resulting string does not correspond to the number of spaces in the conditions and therefore the conditions do not work. To test this you need delimited code.

Generally. your code is very poor at protecting against input errors. Count on the exact number of spaces is a very bad idea.

It would be good to see the outputs you saw, we might spot something you didn't.

You could also print the length of the String received. There might be "invisible" non-printable characters in the string that mean it does not exactly equal the String you are checking against, and that would not be visible on serial monitor.

Clearly there is something wrong with your code and you posted on the forum in the hope that more eyes on the problem would reveal something. But we can only see what you show us.

Hi B707,

Thank you for your input.
Regards "Generally. your code is very poor at protecting against input errors. Count on the exact number of spaces is a very bad idea." Have you a better way to identify the incoming text? Working with Strings is very new to me, and although clunky, generally this type of logic works for me.

Paul

Do not test the Strings by equality, check the occurrence of one string in another, say with indexOf() method.
At least this should be used on long lines. On strings one or two characters long, you can use equality.

Thank you ... I'll look into this and let you know how I get along

Thank you PaulRB,
I'll take onboard the suggestions to analyse the input stream a different way.

As it goes, I received an OLED Display this morning, and that doesn't show any of the irregularities of the 2 x 16 Digital Display, In fact it's working perfectly, so it seems to be a moot point!

But thank you all, for your insights.

Cheers

My guess would be that while making the code changes for the OLED, you accidentally fixed the previous problem. Such things do happen!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.