Totally confused: String handling

Hi,

I have bought the Adafruit 32x16 LED display and I can run the scrolltext example without any problems. However, the examples uses a fixed text. But I would like to be able to display text that is dynamically calculated on the display.

Here is the example with the fixed scrolltext that works fine:

// scrolltext demo for Adafruit RGBmatrixPanel library.
// Demonstrates double-buffered animation on our 16x32 RGB LED matrix:
// http://www.adafruit.com/products/420

// Written by Limor Fried/Ladyada & Phil Burgess/PaintYourDragon
// for Adafruit Industries.
// BSD license, all text above must be included in any redistribution.

#include <Adafruit_GFX.h> // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library

// Similar to F(), but for PROGMEM string pointers rather than literals
#define F2(progmem_ptr) (const __FlashStringHelper *)progmem_ptr

#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
#define LAT A3
#define OE 9
#define A A0
#define B A1
#define C A2
// Last parameter = ‘true’ enables double-buffering, for flicker-free,
// buttery smooth animation. Note that NOTHING WILL SHOW ON THE DISPLAY
// until the first call to swapBuffers(). This is normal.
RGBmatrixPanel matrix(A, B, C, CLK, LAT, OE, true);
// Double-buffered mode consumes nearly all the RAM available on the
// Arduino Uno – only a handful of free bytes remain. Even the
// following string needs to go in PROGMEM:

const char str PROGMEM = “Adafruit 16x32 RGB LED Matrix”;
int textX = matrix.width();

int textMin;
int hue = 0;

char test[50];

void setup() {
matrix.begin();
matrix.setTextWrap(false); // Allow text to run off right edge
matrix.setTextSize(2);

//strcpy(test, “This ia a test”);
textMin = sizeof(str) * -12;
}

void loop() {
byte i;

// Clear background
matrix.fillScreen(0);

// Draw big scrolly text on top
matrix.setTextColor(matrix.ColorHSV(hue, 255, 255, true));
matrix.setCursor(textX, 1);
matrix.print(F2(str));
//matrix.print(test);

// Move text left (w/wrap), increase hue
if((–textX) < textMin) textX = matrix.width();
hue += 7;
if(hue >= 1536) hue -= 1536;

// Update display
matrix.swapBuffers(false);

}

I have a prepared the statement strcpy(test, “This ia a test”); for handling dynamic text. But as soon as I remove the comment lines before this statement and send the code to the Arduino, everything seems to be screwed up. The display no longer shows the scrolltext and I see random pixels on the display.

From the compile output, I think I should have enough space:

Sketch uses 10,040 bytes (31%) of program storage space. Maximum is 32,256 bytes.
Global variables use 377 bytes (18%) of dynamic memory, leaving 1,671 bytes for local variables. Maximum is 2,048 bytes.

Does anybody know why the strcpy causes these problems? I have also tried using String instead of char, but I ran into the same problem.

Best regards
whyper

I think this is your problem:

// Double-buffered mode consumes nearly all the RAM available on the
// Arduino Uno -- only a handful of free bytes remain.  Even the

test consumes 50 bytes

Why is it a Problem that test consumes 50 bytes? What else can I do if I want to retrieve and store dynamic Text e.g. via a serial connection?

whyper:
Why is it a Problem that test consumes 50 bytes? What else can I do if I want to retrieve and store dynamic Text e.g. via a serial connection?

//RGBmatrixPanel matrix(A, B, C, CLK, LAT, OE, true);
RGBmatrixPanel matrix(A, B, C, CLK, LAT, OE, false);

Change the last parameter as above to use single buffering. It won't look quite as pretty, but it will give you back about 800 bytes of RAM (32 * 16 * 12 / 8, where 12=12 bits per pixel and 8=bits per byte = 768 bytes).

Or buy a Mega.

Or modify the library to use indexed colours instead. But that's a big job. Actually, it would be cool if Adafruit did that. Maybe if you suggested it?