Alternative ways to display text

Why arduboy.print takes up so much SRAM memory? I would love to use text in one of the games I make, but it’s a shame to waste the arduino nano’s memory, which has even less space. Explain to me what this is connected with and what are the alternative ways to call text on the screen? P. S .: I found only method “the use of a dictionary of words” , but I did not understand how this method works.

arduboy.print() shouldn’t take up too much RAM. Are you using the F() macro for printed strings?

See the section “The F() macro” at the end of this page:

@MLXXXp

You are awesome! I was not aware of the existence of such a simple solution. Thanks!
One more question: the article states that you can also use PROGMEM. Unfortunately, I get random symbols when using it. I try like this:

const static char long_str [] PROGMEM = "Hi, I would like to tell you a bit about myself. \ n";

arduboy.setCursor (0, 0);
arduboy.print (long_str);

Am I using PROGMEM correctly?

You have to tell print() that the string is in PROGMEM. You can do this by casting the variable as __FlashStringHelper*

arduboy.print ((__FlashStringHelper*)long_str);

I believe @Pharap has worked on ways to simplify this type of thing. Perhaps he will “pipe in” here.

3 Likes

Their is some good documentation here >

4 Likes

Indeed I have. It’s not a massive improvement,
but I’ve written a header that provides two helper functions

The demo should speak for itself, but here’s an adaptation of the code posted earlier:

const char long_str[] PROGMEM = "Hi, I would like to tell you a bit about myself. \ n";

arduboy.setCursor(0, 0);
arduboy.print(asFlashStringHelper(long_str));

The link @filmote posted also contains a link to a gist that provides an example of how to use the readFlashStringPointer function:

Really you should cast it to const __FlashStringHelper *.
Casting away constness is a bad idea because it’s one step away from undefined behaviour (i.e. trying to modify the object).

In this case it’s not going to cause a problem,
but it’s not something anyone should get into the habit of.
Nobody should ever have to cast away const.
If they do, someone else has already screwed up somewhere.

2 Likes

Guys, thank you all for the answers. It was really helpful! Perhaps this question will be beyond the scope of the topic, but how to implement the gradual appearance of the text? I think I need to create a marker that will move through the array of text and display each letter, but I don’t know how to implement this.

#include <Arduboy2.h>

Arduboy2 arduboy;

void setup()
{
	arduboy.begin();
}

void loop()
{
	if(!arduboy.nextFrame())
		return;

	arduboy.pollButtons();

	arduboy.clear();

	// Will be true four times per second at 60 FPS
	if(arduboy.everyXFrames(15))
		// Updates the text cursor
		updateTextCursor();
	
	// Draws the text
	drawText();

	arduboy.display();
}

const char text[] PROGMEM = "The text to be printed slowly";

size_t textIndex = 0;

void updateTextCursor()
{
	// Reads the next character
	char c = pgm_read_byte(&text[textIndex]);
	
	// If the next character isn't the null terminator
	if(c != '\0')
		// Increases the index
		++textIndex;
}

void drawText()
{
	// Writes all characters from the string up to
	// and excluding the character at 'textIndex'
	for(size_t index = 0; index < textIndex; ++index)
		arduboy.write(pgm_read_byte(&text[index]));
}

// This code is available under CC0 1.0
2 Likes

If you want to know about the pgm_read_byte() function and other specifics about accessing program memory, this article provides good information:

1 Like

Oh, thanks for the finished code and the link to a great article! For me, arrays are still difficult, but I understand that without them it will be even more difficult. Thanks for the new information =)

1 Like