Printing individual characters

So I have this

const char txtDefault[27] PROGMEM = "Welcome to Arduboy FX      ";

  for( int i = 0; i < 4; i++){
    Serial.print(i);
    Serial.print(": ");
    Serial.println( txtDefault[i] );
    arduboy.print( txtDefault[i] );
  }

And for some BORKED UP REASON that is DRIVING ME UP THE WALL, this is what it returns:
image

Ok, how is it even flipping between two different values every time it runs???

Also, I coudln’t even copy these results, it refuses to paste anything that is in the 1 or 3 values, it’s like a special “end of file” character or something???

Serial.println( txtDefault[0] ) works fine and returns “W” and so forth, what the heck am I doing wrong here??

What I’m going for here is a typewriter effect.

Your text is in program memory (progmem) so you can’t access it as if it were in RAM.

Change:
txtDefault[i]
to:
(char)pgm_read_byte(&txtDefault[i])

The details are here:
https://www.nongnu.org/avr-libc/user-manual/pgmspace.html

6 Likes

Or just remove the PROGMEM directive if you are only writing a little program and have heaps of RAM

1 Like

Thank you friends!

Actually throwing circle version of my particles, that you helped shrink so I can have more particles :slight_smile:

You can never have enough particles.

You can when A: you have n-1 with n= the number that crashes the hardware and B: when you can’t see behind the particles

I am guessing you hit A well before B ?

Nah actually B! ARDUBOY RULES!

So, @MLXXXp et al does that mean the only reason txtDefault[0] returned anything was because the compiler figured it out?

Well, it’s got to return something… it computed the address in progmem, but used that as an offset into data memory, so was just using a byte of effectively uninitialized memory for the value. The compiler has to generate different instructions to fetch program memory than to fetch data memory, which is what the pgm_read_byte macro facilitates.

2 Likes

Yeah but setting it manually went and grabbed it correctly, so I guess the compiler figures out it needs the PGM read bite? I’m just confused on how it actually works if it’s hard coded.

By the way this is the greatest welcome to Arduboy FX app that there ever was created thanks to everyone!

1 Like

I suspect that since you’ve declared txtDefault as const, when you write txtDefault[0], the compiler just replaces that directly with the value, since that’s faster than doing the extra memory read. If txtDefault wasn’t const, it couldn’t assume that the memory wasn’t going to be changed.

2 Likes

@bateske, I’m assuming your tweet about no warning for using = instead of == was related to what you’re working on here, so I’ll answer here.

In the Arduino IDE select
File > Preferences
Set Compiler warnings: to All
Click on OK at the bottom right.

You’ll then get a warning for it:
warning: suggest parentheses around assignment used as truth value

3 Likes