I've wired up a dot matrix, and I display characters on the screen by using something like the example code below.
The Char_B
variable is a global variable in a library used by the Arduino, and displayPixels()
is called from inside the library.
bool Char_B[8][8] = {
{0,1,1,1,1,0,0,0},
{0,1,0,0,0,1,0,0},
{0,1,0,0,0,1,0,0},
{0,1,1,1,1,0,0,0},
{0,1,0,0,0,1,0,0},
{0,1,0,0,0,1,0,0},
{0,1,0,0,0,1,0,0},
{0,1,1,1,1,0,0,0}
};
displayPixels(1000, Char_B);
However, each time I declare a new character like the array above, I use a relatively massive amount of memory. Is there any way to reduce memory usage? Keep in mind I am willing to change the structure of the array if necessary, since I'll need well over 100 characters loaded onto the Arduino to write text.
2 Answers 2
The data structure will take 64 bytes as sizeof bool is 1 byte not a bit. The first option is to pack each line (or column) in a byte (or bytes). The prefix 0b is for binary constant value (in gcc).
uint8_t Char_B[8] = {
0b01111000,
...
0b01111000
};
The second option is to move the data structure to program memory.
const uint8_t Char_B[8] PROGMEM = { ... };
This will require reading each line with a special function.
for (int line = 0; line < sizeof(Char_B); line++) {
uint8_t pixels = (uint8_t) pgm_read_byte(Char_B + line);
...
}
Use a single byte to store a row of data. As a bool still takes a single byte.
Optionally you could flip the axis, and store column data, instead of row data in the array. Since letters like i
use fewer columns that letters like B
. Even in your example you can see you have 3 columns with only 0
s.
A final suggestion would be to use PROGMEM
and use the pgm_read_byte_near
function. That way the data will remain in the flash memory, and will not take any space in the program-memory.
-
PROGMEM is an excellent idea. That saves storing anything in RAM.2016年02月01日 08:28:01 +00:00Commented Feb 1, 2016 at 8:28
I use a relatively massive amount of memory.
- 64 bytes, perhaps? You don't need 64 bytes to store 8 lots of 8 bits. You would need 8 bytes.