I'm working on a program to turn relays on/off (for christmas lights ect.) and I am using bit math to make it simple. I have the example below working. Each "menu selection" will cycle through the bytes and turn on/off the relays according to the bits. (Because there can be up to 32 cycles per menu, the first bit is 1 to show that it is not a null entry.)
I have this working for 6 relays, and want to expand up to 16 relays. However, something like B11010100111101010 doesn't work (probably because it's not a byte..).
Is there a solution? How can I make an integer in binary format and still use the bitmath?
byte RelaySequence[menuSelections][32] = { //up to 32 cycles per menu
{ //menu selection 1
B1100000,
B1000000 }
,
{ //menu selection 2
B1110000,
B1011000,
B1001100,
B1000110,
B1000011,
B1100001 }
}
You just need to select the size that gives you the right number of bits (for your application) if you want to manipulate it as a number:
(unsigned) byte or uint8_t - 8 bits
(unsigned) int or uint16_t - 16 bits
(unsigned) long or uint32_t - 32 bits
Of course you can still use your array of bytes and combine them into int/long just before you need to use them if that makes more sense form a programming point of view.
Also note that the numbers Bnnnnnnnn are defined in the Arduino header file (not really sure why). Standard C/C++ allows you to define binary numbers by 0bnnn..nn (simlar to hex numbers being 0xnnnn). This definintion does not depend on any header file and allows arbitrary length bit strings. You still need to respect that the bits have to fit in your variable, though.