4

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.

Greenonline
3,1527 gold badges36 silver badges48 bronze badges
asked Jan 30, 2016 at 12:49
2
  • 1
    As a first step, pack your bits. Commented Jan 30, 2016 at 13:16
  • 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. Commented Feb 1, 2016 at 8:26

2 Answers 2

4

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);
 ...
}
answered Jan 30, 2016 at 13:50
2

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 0s.

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.

answered Jan 30, 2016 at 13:44
1
  • PROGMEM is an excellent idea. That saves storing anything in RAM. Commented Feb 1, 2016 at 8:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.