Two dimensional char array, and if-statement

Hello!

I am having a problem with my code, it is for a radio-fax program. I store the “pixels” of the letter “G” in a two dimensional char array, and go through it with two for-statements to set the radio-pin High if there is a X and low if there is a O.

But the if-statement doesnt work, i cant compare ‘x’ to letter_g[row][col], and i dont understand why. Can anyone give me a helper?

void setup() 
  {
    pinMode(2, OUTPUT);
    Serial.begin(9600);
  }


void loop() 
  {
char *letter_g[][14] = {{"o","o","o","o","o","o","o","o","o","o","o","o","o","o"},
                        {"o","o","x","x","x","x","x","x","x","x","x","x","o","o"},
                        {"o","o","x","x","o","o","o","o","o","o","x","x","o","o"},
                        {"o","o","x","x","o","o","o","o","o","o","x","x","o","o"},
                        {"o","o","x","x","o","o","x","x","o","o","x","x","o","o"},
                        {"o","o","x","x","x","x","x","x","o","o","x","x","o","o"},
                        {"o","o","o","o","o","o","o","o","o","o","o","o","o","o"}}; 
  
  
  for(int row = 0; row < 7;row++)
    {
    for(int col = 0; col < 14;col++)
      {
      if(letter_g[row][col] == 'x')
        {
        digitalWrite(2, HIGH);
        delayMicroseconds(4045L);
        digitalWrite(2, LOW);
        Serial.print("x");
        }
      else
        {
        delayMicroseconds(4045L);  
        Serial.print("o");  
        }
      }
    }
  }

You’ve got an array of strings (char pointers); change it to an array of char

Thanx, that solved it!

char *letter_g[][14] = {{"o","o","o","o","o","o","o","o","o","o","o","o","o","o"},
                        {"o","o","x","x","x","x","x","x","x","x","x","x","o","o"},
                        {"o","o","x","x","o","o","o","o","o","o","x","x","o","o"},
                        {"o","o","x","x","o","o","o","o","o","o","x","x","o","o"},
                        {"o","o","x","x","o","o","x","x","o","o","x","x","o","o"},
                        {"o","o","x","x","x","x","x","x","o","o","x","x","o","o"},
                        {"o","o","o","o","o","o","o","o","o","o","o","o","o","o"}};

Wastes a lot of memory. Why not store the state of each pixel in a single bit of a variable ?

Besides the shocking memory usage, good luck keeping the clocks in step at both ends by just sending a raw bitstream like that. Or indeed knowing when something is being sent as opposed to an empty line. Or where the start of a line is....

Take a look at using a self clocking OTA protocol, such as Manchester Encoding.

I am have a program with a number of byte arrays, that store graphix pixels for a radio fax machine. But now i am running out of SRAM and need to move these to FLASH.

I have tried moving letter_a to flash, letter_g is still in SRAM. I get no error codes, but my program is unable to retrieve the letter_a variable. Is there a trick to using progmem that i have missed?

const PROGMEM byte  letter_a[][14] = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,1,1,1,1,1,1,1,1,1,0,0,0},
                        {0,0,0,0,0,1,1,0,0,1,1,1,0,0},
                        {0,0,0,0,0,1,1,0,0,0,0,1,0,0},
                        {0,0,0,0,0,1,1,0,0,1,1,1,0,0},
                        {0,0,1,1,1,1,1,1,1,1,1,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0}};     

byte letter_g[][14] =   {{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,1,1,1,1,1,1,1,1,1,1,0,0},
                        {0,0,1,1,0,0,0,0,0,0,1,1,0,0},
                        {0,0,1,1,0,0,0,0,0,0,1,1,0,0},
                        {0,0,1,1,0,0,1,1,0,0,1,1,0,0},
                        {0,0,1,1,1,1,1,1,0,0,1,1,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0}};

As I pointed out in another thread of yours you could save the pixel data in the bits of a variable rather than wasting 8 bits to hold a single digital value

uint16_t letter_a[] =   {0b0000000000000000}
                        {0b0000111111111000}
                        {0b0000000110011100}
                        {0b0000000110000100}
                        {0b0000000110011100}
                        {0b0000111111111000}
                        {0b0000000000000000};

The program is actually working, its for a low powered HF-beacon sending with the Hellschreiber mode. The only problem now is the memory usage, i cant fit the complete alfabet in SRAM.

Sorry for the poor etiquett, i will keep the discussion in the other thread!

More efficient memory storage is probably the key! I tried this(proposed in another thread), but im not sure how to recover the information from it.

uint16_t letter_a[] =   {0b0000000000000000}
                        {0b0000111111111000}
                        {0b0000000110011100}
                        {0b0000000110000100}
                        {0b0000000110011100}
                        {0b0000111111111000}
                        {0b0000000000000000};

The previos way i used to compare the value from the array doesnt work with this more efficient storage.

if(letter_g[row][col] == 1)
            {
            digitalWrite(2, HIGH);
            delayMicroseconds(speed);
            digitalWrite(2, LOW);
            }

How do i handle this new array? I cant use the X/Y locator way i did when it was a byte array...

I cant use the X/Y locator way

bitRead?

And make sure the array(s) stay in flash memory, not SRAM

heron2:
I am have a program with a number of byte arrays, that store graphix pixels for a radio fax machine. But now i am running out of SRAM and need to move these to FLASH.

I have tried moving letter_a to flash, letter_g is still in SRAM. I get no error codes, but my program is unable to retrieve the letter_a variable. Is there a trick to using progmem that i have missed?

const PROGMEM byte  letter_a[][14] = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,1,1,1,1,1,1,1,1,1,0,0,0},
                        {0,0,0,0,0,1,1,0,0,1,1,1,0,0},
                        {0,0,0,0,0,1,1,0,0,0,0,1,0,0},
                        {0,0,0,0,0,1,1,0,0,1,1,1,0,0},
                        {0,0,1,1,1,1,1,1,1,1,1,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0}};

byte letter_g[14] =  {{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,1,1,1,1,1,1,1,1,1,1,0,0},
                        {0,0,1,1,0,0,0,0,0,0,1,1,0,0},
                        {0,0,1,1,0,0,0,0,0,0,1,1,0,0},
                        {0,0,1,1,0,0,1,1,0,0,1,1,0,0},
                        {0,0,1,1,1,1,1,1,0,0,1,1,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0}};

If you want not to change the pixel data many times in the code, then you should use EEPROM memory for that purpose. EEPROM can store the configuration, and it is very easy to retrieve that information many times as you want.

You should do-

First program the Arduino with a code, that stores the pixels in the EEPROM. Note that the EEPROM write has limited uses, and read has unlimited uses. So don't save the pixel values code in a loop statement. If you do this, then the loop will write the EEPROM many times, resulting in a fault in the EEPROM memory.

Then in your main code, use the function EEPROM.read() to retrieve the pixel values.

.. Arnav

Is there a trick to using progmem that i have missed

Yes, you need to use the pgm_read_ instructions described in the reference.

It seems unlikely to me that you’d be changing the font frequently, so I’d avoid EEPROM.

bitRead worked nicely, i changed the format of the array a bit and managed to retrieve the value.

uint16_t letter_f[] =   {0b0000000000000000,
                        0b0000111111111000,
                        0b0000000110011100,
                        0b0000000110000100,
                        0b0000000110011100,
                        0b0000111111111000,
                        0b0000000000000000};  

Serial.println(bitRead(letter_f[5],3));

I tried this(proposed in another thread), but im not sure how to recover the information from it.

I will merge the threads to avoid duplication

Threads merged