AaronLiddiment / LEDText

FastLED Flexible Text Message Class requires LEDMatrix Class
89 stars 32 forks source link

Dynamic score display #6

Closed benlooi closed 7 years ago

benlooi commented 7 years ago

Hello again, I am trying to display a score in numbers with one decimal point on my matrix. In the example, the scrolling text uses const unsigned char array. I am wondering if the SetText function require unsigned char and how do I convert a string to unsigned char in Arduino. Here is my sample code:

cLEDMatrix<WIDTH, HEIGHT, MATRIX_TYPE> leds;

cLEDText Score;

float the_score; unsigned char SCORE[]= {}; void setup () { the_score=0; Score.SetFont(MatriseFontData); Score.Init(&leds, leds.Width(), Score.FontHeight() + 1, 0, 0); Score.setText((unsigned char *)SCORE, sizeof(SCORE)-1);

}

void loop () { //increase the score every cycle the_score+=0.25; //assign the_score to SCORE array ---this is where I am unsure SCORE[0] = String(the_score,1); if (Score.UpdateText() == -1) //so the displayed text Score will keep changing every refresh cycle Score.SetText((unsigned char *)SCORE, sizeof(SCORE) - 1); else FastLED.show(); delay(10); }

AaronLiddiment commented 7 years ago

You do not allocate any memory for the score character array which will lead to memory corruption!

Replace: unsigned char SCORE[]= {}; With: String SCORE = "";

And change your SetText's to this: Score.SetText(SCORE, SCORE.length());

And Finally change: SCORE[0] = String(the_score, 1); To: SCORE = String(the_score, 1);

benlooi commented 7 years ago

Thanks for the tip! Great work, by the way on this. I hope to understand it more to contribute to this project. I am making a 39 x 18 matrix with ws2811 string leds. Using a China-power supply with 5v/40A that comes with 3 DC outs. I managed to power all by allocating each DC output to about 250 leds. May need to put in one more power supply to power the Arduino Mega and another set of ws2812bs strips.

benlooi commented 7 years ago

Hi. I am getting an error when I do Score.SetText(SCORE,SCORE.length()); : no matching function for call to 'cLEDText::SetText(String&, unsigned int)'

Any idea why?

benlooi commented 7 years ago

Alright, did some more digging into SetText. It requires unsigned char, so String as a parameter wouldn't work. So the solution is to cast the String to unsigned char. Here's what I did that seems to work. //get the string SCORE= String(the_score,1); //make a new variable that is an unsigned char unsigned char displayScore[10]; //push SCORE string to the char array SCORE.toCharArray(displayScore,10); //finally for SetText...dunno what -1 does, just following the example Score.SetText(displayScore, sizeof(displayScore) -1);

This got rid of the error.

AaronLiddiment commented 7 years ago

Well done :) To be honest I have never used 'String', I would normally use sprintf to format data into unsigned char arrays. But I thought it would be easier as you had already started using it. I did mod one of the examples with String and it compiled with no errors for me but this could be down to which Arduino version you have installed. But you do learn more when you have to work to find a solution ;) BTW here is a sprintf example: unsigned char displayScore[10]; float Score = 2.75; sprintf(displayScore, "%9.1f", Score); // " 2.8" or sprintf(displayScore, "%09.1f", Score); // "0000002.8"

A char array is usually terminated with a 0x00 so the actual length of the data is usually it's size minus 1.