Get data in Binary format from an array

Hi there,

it’s been a long time since i last programmed an arduino and maybe im forgetting some of the basics but i searched the forum and although i found a lot of related information, none was a direct solution to my problem, maybe some workarounds if theres not a straight simpler solution.

Here is my code:

 const unsigned char texto [] PROGMEM = {      
    B01110000,	//0
    B10001000,
    B10011000,
    B10101000,
    B11001000,
    B10001000,
    B01110000,
    6};
.
.
.
some other stuff
.
.
.
if(val=='0'){                         //ZERO 
    
    lc1.setRow(0,0,texto[0]);
    
    lc1.setRow(0,1,texto[1]);
    
    lc1.setRow(0,2,texto[2]);
    lc1.setRow(0,3,texto[3]);
    lc1.setRow(0,4,texto[4]);
    lc1.setRow(0,5,texto[5]);
    lc1.setRow(0,6,texto[6]);
    lc1.setRow(0,7,B00000000);
    
    Serial.println("ZERO");
  }
  
  if(val=='1'){                    //UNO

    
    for (int a=0;a<7;a++){
     val = texto[a];
       
        lc1.setRow(0,a,texto[a]);
         
        delay(20);
        Serial.println(val,BIN);
    }
    
  }

Thing is:

lc1.setRow(0,6,texto[6]); - works as expected, passing B01110000 as a parameter to the setRow func.

lc1.setRow(0,a,texto[a]); - (where a goes from 0 to 6) does not work because it is not binary formated it passes 112 decimal value 64+32+16.

i find this odd, am i missing something here? Whats the most simple way to get it in the B…format?
I thought that having texto[0] would be the same that texto[a] where a=0…can someone explain me the diference? whats makes one work and not the other?

Tanks in advance,
Paulo M.

lc1.setRow(0,a,texto[a]); - (where a goes from 0 to 6) does not work because it is not binary formated it passes 112 decimal value 64+32+16.

There are two "it"s in that statement that do not mean anything.

What is not "binary formatted"?

There is NO difference between

lc1.setRow(0,a,texto[6]);

and

byte a = 6;
lc1.setRow(0,a,texto[a]);

as far as the data passed to the setRow() method.

i find this odd, am i missing something here? Whats the most simple way to get it in the B........forma

To get what in the B......... format? All data is stored in binary. The "most simple way" to get data in binary is to do nothing. It's already in binary.

Thanks for replying,

PaulS: There are two "it"s in that statement that do not mean anything.

What is not "binary formatted"?

"it' was referring to the value of texto[0] that is in the B01110000 format as opposed to texto[a] given a=0 that is in decimal format

There is NO difference between Code: [Select] lc1.setRow(0,a,texto[6]);

and Code: [Select] byte a = 6; lc1.setRow(0,a,texto[a]);

Thats what i always thought but apparently its not the same, because the lc1.setRow function behaves differently when textoa is used or texto[0 to 6]. i cant see any other reason....

iThis code is to display numbers in a led matrix but it only works if i use texto[0 to 6], it doesn't display the numbers when i use texto[a].

In conclusion: lc1.setRow(0,6,texto[6]); --------->This Works lc1.setRow(0,6,B01110000);--------->This also Works a=6; lc1.setRow(0,6,texto[a]);--------->This does not Work, the led matrix only displays some garbage...variable a is not used anywhere else in the code and theres not much more code than this...im hitting my head in a wall here..

What do you think?

thanks.

"it' was referring to the value of texto[0] that is in the B01110000 format as opposed to texto[a] given a=0 that is in decimal format

Nonsense. ALL data is stored in binary format. When a contains 0, texto[0] and texto[a] refer to the SAME memory address. The values can not possibly be different.

i cant see any other reason....

Without seeing ALL of your code, neither can we. However, your interpretation of what you are seeing is wrong. I can guarantee that.

What do you think?

That you need to find the hint in the previous paragraph and act on it.

Posting a link to the library would be useful, too. It's hard to imagine that a library would include a function to print a character that [u]had[/u] to live in PROGMEM.

Did you mean:

if(val=='1'){                    //UNO 
    for (int a=0;a<7;a++){
     val = pgm_read_byte_near(texto[a]);     
        lc1.setRow(0,a,val);     
        delay(20);
        Serial.println(val,BIN);
    }

?

Dont get me wrong Paul, i completely agree with you in everything u said, i know you are correct and im just grateful you are trying to help, its just that i’ve spent all afternoon around this and i cant make the theory (what i learned and what u ve been saying, that they are all equivalent) stick to the reality that when i use texto[a] nothing works, so i just started to put everything i learned, and knew for a fact, in doubt. I’ll post the full code…maybe im missing something in the code.

As for the library and Progmem im using both for the first time, i think the idea is to be able to access data in a faster way but to be honest I m not sure and I also have to look at the library, i just thought that there could be something in the code that i was missing and some1 in the forum could spot easily.

I’ll try to study the ledcontrol library and do some more tests later, i must be doing something wrong.

Thanks.

Progetto.ino (14.2 KB)

aarg:

  if(val=='1'){                    //UNO 

for (int a=0;a<7;a++){
    val = pgm_read_byte_near(texto[a]);   
        lc1.setRow(0,a,val);   
        delay(20);
        Serial.println(val,BIN);
    }

HI aarg,

I dont know what that does (will study it later) but tried it and the output from println is:

1100
1100
0
1100
1100
1100
10010100

where i think it should be:

B01110000, //0
B10001000,
B10011000,
B10101000,
B11001000,
B10001000,
B01110000

There are more led turned on but still garbage.
I diddnt know about gm_read_byte_near function but im now pretty sure the problem will be related to the data being in progmem and/or this function.

Thanks!

Oops! Then try

if(val=='1'){                    //UNO
    for (int a=0;a<7;a++){
     val = pgm_read_byte_near(&texto[a]);     
        lc1.setRow(0,a,val);     
        delay(20);
        Serial.println(val,BIN);
    }

By the way, Serial.print() doesn’t produce output like “B00010010”, it produces a leading zero suppressed string like “10010”.

aarg:
Oops! Then try

if(val=='1'){                    //UNO

for (int a=0;a<7;a++){
    val = pgm_read_byte_near(&texto[a]);   
        lc1.setRow(0,a,val);   
        delay(20);
        Serial.println(val,BIN);
    }




By the way, Serial.print() doesn't produce output like "B00010010", it produces a leading zero suppressed string like "10010".

That made the ‘P’ character to light on the matrix… im still reading the reference docs over this on arduino but i must be honest and im finding it quite hard to grasp…still i tried this:

val = pgm_read_byte_near(texto +a);

wich apparently gives the same of

val = pgm_read_byte_near(&texto[a]);

thanks for the help i feel we are getting there

bragajam: That made the 'P' character to light on the matrix.... im still reading the reference docs over this on arduino but i must be honest and im finding it quite hard to grasp...still i tried this:

val = pgm_read_byte_near(texto +a);

wich apparently gives the same of

val = pgm_read_byte_near(&texto[a]);

thanks for the help i feel we are getting there

Yes, those are two different ways of saying the same thing. One possible problem, all your other numbers call setColumn, but this one calls setRow. Maybe, either that is incorrect, or you didn't adjust your font bit pattern correctly.

Ok, this is dumb from me but along the way i started to use val variable inside the if wich is tested by other ifs thats why it gives P character, because val variable is changed inside the 1st if and by some reason its catched by the if that looks for p character, i changed the name of the variable val inside the if and i can now see that the fetched data is right.

if(val=='1'){                    //UNO
    for (int a=0;a<7;a++){
        [b]x[/b] = pgm_read_byte_near(&texto[a]);     
        lc1.setRow(0,a,val);     
        delay(20);
        Serial.println(x,BIN);
    }
    
  }

It still does not display correctly in the matrix(if i enter 0 in the serial console it dipslays 0 in the matrix which is correct, if i enter 1 it should also display 0 because it points to the same variable but all i get is garbage) but in the Serial.println output i can see the data is correct.

Both SetColumns and setRows works because i changed the Bytes accordingly, i was just trying to make a text display from scratch because all available example codes on the internet are reversed ie their rows are my columns.
Thanks a lot for your great help.

    for (int a=0;a<7;a++){
        [b]x[/b] = pgm_read_byte_near(&texto[a]);     
        lc1.setRow(0,a,val);

I don’t suppose that those square brackets and b and /b are actually in your code. Reading and saving in x, and then showing val can’t be what you mean to do.

@bragajam, your welcome. Honestly, I think you wouldn't have made those mistakes if you had used more descriptive variable names than "a", "x" and "val".

PaulS:

    for (int a=0;a<7;a++){

x = pgm_read_byte_near(&texto[a]);   
        lc1.setRow(0,a,val);



I don't suppose that those square brackets and b and /b are actually in your code. Reading and saving in x, and then showing val can't be what you mean to do.

@Paul, no x was just to make clear where i did change the variable name…didnt realize it wouldn’t become bold…and changed the val to x in serial print But completely forgot to change it where it mattered, well this post will forever publicly embarrass me but i guess its well deserved…

Problem solved! Thanks a lot to u both. I’m now starting to understand how to work with progmem but i still have to read a lot more to completely understand it.

Best to you.

bragajam: well this post will forever publicly embarrass me

Not really, unless some idiot necro's it a year from now. :) There's enough posts here that it won't even last half the day on the front page.