Working with 16 bit numbers

Doing some learning here and I am fiddling with a 2 digit 14 segment LED and trying to drive with with just the I/O pins. Like what was done in this post: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1265669651 with the bits of a value to set the state of the segment.

Don't have any fancy chips yet to make things easy, but there are just enough pins to do what I want at the moment. My question pertains to this particular piece of code which the poster used to set the bits of a byte for the control of each LED segment. I was wondering in what ways can I set a word/int directly with binary source code like that without having to just convert it to a decimal and setting the array to that? Is there an library with like a BH BL function or something built in I can use or would it be easier to just use two arrays, one for the 'high' and 'low' bits of the 14 bits I need for the control?

byte numbers[10] =
{
  B11000000, // 0
  B11111001, // 1
  B10100100, // 2
  B10110000, // 3
  B10011001, // 4
  B10010010, // 5
  B10000010, // 6
  B11111000, // 7
  B10000000, // 8
  B10010000  // 9
};

Jon,

You could use hexadecimal constants (base 16) to do this. Each hex numeral represents four bits, so your numbers would have four hex numerals, but the first digit would only use the lower two bits.

With a little practice, you can convert four bits to a hex numeral in your head. Here's how they go:

Binary  Hex
0000    0
0001    1
0010    2
0011    3
0100    4
0101    5
0110    6
0111    7
1000    8
1001    9
1010    A
1011    B
1100    C
1101    D
1110    E
1111    F

So, if your 14-bit pattern were 10100110011101, you would break it into a two-bit chunk, then three four-bit chunks:

10 1001 1001 1101 then translate each chunk into a hex numeral:

2 9 9 D

Then, your array would look like this:

int numbers[10] =
{
  0x299d,
  ...
};

(of course, the "..." would be replaced by the other 9 values in your array).

Regards,

-Mike

Ah, excellent. Well 36 numbers in this case since it will be 0-9 and A-Z for the alphanumeric display. Forgot to make that clearer, I bet there is already an include out there already but practice is good. Thank you.

Jon,

Google '14-segment led font' and you'll find some nice fonts, including lower case and symbols. I didn't see any that were already encoded into binary or hex, but copying them will keep you from having to draw them out.

Regards,

-Mike

in what ways can I set a word/int directly with binary source code like that without having to just convert it to a decimal

Recent versions of the compiler used by Arduino allow you to

int x = 0b110101001101;  // leading zeros unneeded!
long y = 0b110000000000000000000000101;

("recent" means anything that has even been incorporated into any arduino release, I think. But it's not "standard.")

Also cool. I thought about trying that but didn't see the syntax highlighting pop up when I entered it so thought it just was not allowed. Perhaps I should have experimented :stuck_out_tongue: I am using release 19 at the moment naturally.