# 8 bit binary to turn 8 outputs on or off

Hi

I was wondering if it is possible to have an 8 bit binary integer turn switch 8 outputs. ie 11001010 would turn outputs 11,12,15,17 high and 13,14,16,18 low.

The reason behind this is I want to have an option to select any number of Days Monday through Sunday and then store that selection in one EEPROM location and this would allow for that to happen. Then the EEPROM would be read and each each day stored in its own boolean ie boolean Monday = 1. Then this would allow certain events only to opeprate on days that the boolean value is 1.

I have read this http://www.electroschematics.com/9809/arduino-8-bit-binary-led/ which turns on LEDs based on a 0 to 255 integer. However being a bit of a newbee I am having trouble converting this code to what I need.

I was wondering if it is possible to have an 8 bit binary integer turn switch 8 outputs. ie 11001010 would turn outputs 11,12,15,17 high and 13,14,16,18 low.

Yes, quite possible.

One way would be to use this method

``````const byte ledPins[] = {11, 12, 13, 14, 15, 16, 17, 18};

void setup ()
{
for (int pin = 0; pin < 8; pin++)
{
pinMode (ledPins[pin], OUTPUT);
}
byte aNumber = 0b11001010;
showLeds(aNumber);
}

void loop ()
{
}

void showLeds(byte aNumber)
{
for (int bit = 7; bit >= 0; bit--)
{
}
}
``````

I dont know How you planning to use it.

Above thing can be done using 8: 1 digital multiplexer & 16 pin DIP switch.\

For building program you mind need 4 selector pin & 1 read pin, Depend on the status of DIP switch you calculate the the Days.

Here below code can give 255 days.

Selector switch should be in parallel.out put dip switch connected to mux IC in order.

``````static int Device_ID = 1;
int SO_enable = 5;
int S1_enable = 4;
int S2_enable = 3;

int array1[8][3] = {
{
0, 0, 0
}
, {
0, 0, 1
}
, {
0, 1, 0
}
, {
0, 1, 1
}
, {
1, 0, 0
}
, {
1, 0, 1
}
, {
1, 1, 0
}
, {
1, 1, 1
}
};
static int Status_Out[8];
{
for (int row = 0; row < 9; row++)
{
digitalWrite(SO_enable, array1[row][0]);
digitalWrite(S1_enable, array1[row][1]);
digitalWrite(S2_enable, array1[row][2]);
Device_ID = 1 * Status_Out[0] + 2 * Status_Out[1] + 4 * Status_Out[2] + 8 * Status_Out[3] + 16 * Status_Out[4] + 32 * Status_Out[5] + 64 * Status_Out[6] + 128 * Status_Out[7];
Serial.print("Device_ID"); Serial.print(row); Serial.print(":\t"); Serial.println(Device_ID);

}
}

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
delay(1000);

}
``````

thank you UKHeliBob. bitRead was the function I was after :) However may I ask what does the prefix 0b do and does it differ from B.

Sorry for the late reply I dont have much spare time atm.

Cheers Grant

A good tutorial on bit math: http://playground.arduino.cc/Code/BitMath

You can store day of the week in 3 bits. 3 bits can be 0 to 7. That leaves 5 bits in a byte, enough to store day of the month, 0 to 31.

With C bit fields you can even name single or grouped bits.

grantastley: thank you UKHeliBob. bitRead was the function I was after :) However may I ask what does the prefix 0b do and does it differ from B.

Sorry for the late reply I dont have much spare time atm.

Cheers Grant

The 0b prefix denotes a binary number in C and can be used for any size of variable so a value like 0b1111111100000000 is valid for an integer for instance The Arduino environment has predefined values for bytes in binary format such as B10101010 etc but only the 256 byte values are defined in this way so B1111111100000000 is not valid, for instance.