1

I'm trying to make a led sign with a WS2812b ledstrip, using the FastLed library (by the way, this is a great library!), I'm already able to show a text (so far only one, which I leave saved in an array of characters at the beginning of the program), but I would like to be able to change this text using the serial port of the Arduino, and here is the problem:

When setting the text to be displayed in the array of LEDs, the function that I should use is:

ScrollingMsg.SetText ((unsigned char *) TxtDemo, sizeof (TxtDemo) - 1);}

Where TxtDemo is a variable type char array.

Until here all is right, as I commented I can show the text, but at the time of receiving the data from the serial port and save them in the char array TxtDemo, when I use the function sizeof(TxtDemo), the value I get remains with the original size (quite understandable) of this array.

For example: If the initial text was (saved in TxtDemo): "Hello world", the sizeof() function, returns "11", But when I pick up what I want to show on the serial port and store it in the variable TxtDemo, there's no way I can alter the size of that array. So ... what can I do? What do you recommend me to do?

0

1 Answer 1

4

Arrays in C are fixed. sizeof() will always give the amount of allocated memory for the array.

Instead, you should be using strlen() to get the number of characters up to (but not including) the terminating NULL character at the end of the string.

ScrollingMsg.SetText ((unsigned char *) TxtDemo, strlen(TxtDemo));

Make sure that you have enough space in your initial array to store the longest string you will want to send - it will not grow beyond your initial allocation. Also be sure to get the NULL character at the end of your valid text. It's too easy to forget you need to insert that after your last received character.

4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.