String to const char array for dot matrix display problem

I'm creating a scrolling youtube subs & views display for my son.
I'm using a mAX7219 32 x 8 display, and then using the MD_PAROLA library.
If I just pass a straightforward text to the library, it scrolls and displays fine.
However I need to combine some text and the subscriber/view count from youtube api and although when I use Serial.print it shows the combined text fine, on the display it shows as random junk.
The displayText needs a const char array as the text input field, which is why I end up creating a string to begin with and then convert it to a char array.
The 2 Serial.println commands both show me the exact same text.
I suspect that I'm not understanding some fundamental principle relating to the variables and how they are passed?

void loop() {
const char *yt0,*yt1,*yt2;
String subsString;
if (myDisplay.displayAnimate()) {
if (i == 0){
  	if(api.getChannelStatistics(CHANNEL_ID)) {
	Serial.println("\n---------Stats-------------");
		Serial.print("Subscriber #: ");
		Serial.println(api.channelStats.subscriberCount);
		Serial.print("      View #: ");
		Serial.println(api.channelStats.viewCount);
 		Serial.println("----------------------------");
    
    

subsString = "Subs:       " + String(api.channelStats.subscriberCount);

yt1 = subsString.c_str();

Serial.println(subsString.c_str());
Serial.println(yt1);
    
}

switch(cnt){
    case 0:
    
      myDisplay.displayText(yt1, PA_RIGHT, 50, 5000, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
      cnt =1;
    break;
    case 1:
      myDisplay.displayText("Views:        138762", PA_RIGHT, 50, 5000, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
      cnt =0;
    break;
}
myDisplay.displayReset();
}

}
}

A String (capital S) object consists of two parts. Part one contains internal information used by the String object, part two contains the actual text. Using c_str(), you get (a pointer to) the actual text.

The print routines differentiate what to do based on the type of the variable (String or character pointer or integer or float or ...). The print routine that handles the String more than likely uses the c_str() under the hood (not checked) and hence you will not see a difference.

I'm not familiar with the MD_Parola library, but looking at the example for scrolling text it appears the library does not maintain its own char array, but instead uses the pointer that you pass it to access the original data location. If this is the case, then when the loop ends, the String ceases to exist and gets overwritten, resulting in random data being displayed. Try copying the String to a global char array, or better yet just use the char array and drop the entire use of String ( you can use itoa() or similar to convert an integer to a char array).

1 Like

Hi - Thanks for the fast answer. As I suspected, some fundamental 'under the hood' stuff that I failed to understand.

I've done some googling and gone for the below line to create the text i need, directly as a char array, and it seems to work.

sprintf(yt0, "Subs: %d", api.channelStats.subscriberCount);

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