string identifier

hey there, i hope its ok to ask :)

im working on a project which requires a screen, so im using an ssd1306 and the adafruit gfx lib, im sending text from max to the teensy and all works great. the things is, that i would like to send different strings to the screen and control them independently so im guessing i need to put a header infront of each string and unpack it in the arduino side and send it to the right place. im pretty new to the arduino so im not sure whats the best way to do this, i looked at the examples and saw "startsWith" . could this be used for something like this?

could anyone advise me on how to do so?

thanks!

Hi welcome to the forum

There are examples coming with the library and on the internet. What have you looked at and tried?

hey there, thanks for the super fast reply, i actually didnt find string specific examples but i was probably not looking very good either, could you maybe refer me to some?

edit, i looked at the arduino strings examples but could not find any string identifying examples except of startWith which im not sure is the way to go?

thanks

This is coming with the library as setup documentation and this is on github and a small Google on “SSD1306 arduino example” returns tons of ideas such as that one... plenty to start from

thanks again! perhaps i was not clear, i already figured out how to use the screen and display text and graphics, my problem is much more basic, i would like to use Serial.readString(); and read different strigs and send them to different places in the code, thats why i think i need a header and im not sure how to go about this?

To read strings from serial have a look at Serial Input Basics
You acquire a message and then decide where to display

thanks for the link! a very useful tutorial! it actually clarifies many other problems i had, tho im afraid it still does not answer my question, i can successfully send text from max to the arduino using Serial.readString(); and display it, however i cant figure out how to interpret a prefix, for example if i send something like /line1 "hello world" i want it to be sent to somewhere based on the /line1 prefix, and if i send /line2 "goodbye world" i want it to be sent somewhere else based on the /line2 prefix

thanks again and i really hope im not asking extremely obvious quesitons

i can successfully send text from max to the arduino using Serial.readString();

I don’t know who is Max but he is probably not sending anything with Serial.readString().. Max would be better off using Serial.print() for sending text :)

You need to write a parser... once you have received a message, use functions from the standard libraries (stdlib.h and string.h ) to analyse your message for example using strncmp() or strstr() to look for /line at the beginning of the message and atoi() to extract the X from /lineX

=> I would not use the String class nor the readString() function due to timeouts. Better be in charge of timing and memory usage

thanks! thats a great start! max is max/msp, what im doing is sending text converted to integer to the serial port and reading it in the arduino :)

ok, i actually got it working combining Serial.read and Serial.readString sending the letters "a" and "b" as a prefix to the string and then sending it to the right place like so:

while (Serial.available() > 0) {

id = Serial.read(); data = Serial.readString();

if(id == 97){ line1 = data; }

if(id == 98){ line2 = data; }

i have the Serial.setTimeout set to 1ms and all is working great. you have mentioned before that readString is problematic, why so? and how would one look into alternatives? (altho everything works fine now but i guess youll never know)

If you can deal with 1ms wait because data will pause long enough then you might be fine with timing

1ms is a LOT of lost time in a 16Mhz microprocessor perspective - enough time to execute 16,000 clock cycle instructions or receiving more than 11000 characters at 115200 bauds...

The other challenge is that this function uses the String class which - depending on how you use it - risks to fragment your Arduino SRAM and at some point you might run out of memory and program will crash if you don’t track memory allocation

So use the tutorial I pointed above and the cString construct and associated libraries is safer / faster

Now if you run this for not too long and a crash is not an issue - then that can work out

J-M-L: 1ms is a LOT of lost time in a 16Mhz microprocessor perspective - enough time to execute 16,000 clock cycle instructions or receiving more than 11000 characters at 115200 bauds...

Wow, that would be 11 000 000 characters in a second. You should check your arithmetics. ;)

Oooooppps got a bit carried away :)

115200 bits per second 10 bits per character 11520 characters per second Only 11 per millisecond

francis7979: readString is problematic, why so? and how would one look into alternatives?

It's the String class in general that is not good on a microcontroller because it uses dynamic allocation in an environment with no memory management so it can lead to memory fragmentation.

The alternative is to use C-style strings which are null terminated char arrays.

francis7979:
thanks for the link! a very useful tutorial! it actually clarifies many other problems i had, tho im afraid it still does not answer my question,

Read that link again. It contains a complete example that you can use. If you use the example with the start- and endmarker, you can send e.g.

<1,hello world>

Where 1 indicates the line number.

There is also an example to split the received text on the comma, so you have the line number and the text to display.

===

Forget that String (capital S) exists on an Arduino. Heavy use of it might result in unexpected behaviour at run time.