# String to Binary/Byte for 595 Shift Register

Good afternoon folks. I have scrubbed google and I just can't seem to find the answer I am looking for.

I am using a 595 shift register to eluminate LED's (original huh?) . I am basically building a string that I can pass using shiftout. The string looks like this: "B01001000". In this example, I want to power the LEDS on pin 2 and 5.

My programming question is: How can I convert this String to a byte so that I can pass it using shiftout?

Like this: (shiftOut(dataPin, clockPin, MSBFIRST, B01001000); (which will illuminate LEDs on pin 2 and 5)

Thanks!

Here is my string building...

``````int latchPin = 8;
int clockPin = 12;
int dataPin = 11;

String byte_string;
int number_array[2] = {2,5}; //Shift register pin numbers to set to 1

void setup() {
Serial.begin(9600); //For debugging

pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);

}

void loop() {

byte_string = "B";

//Loop 8 times to capture 1/0 value for register pins
for(int x = 1;x<=8;x++){
if(checkExist(x,number_array)){
byte_string = byte_string + "1";
}else{
byte_string = byte_string + "0";
}
}

//Display the dynamic string
Serial.println(byte_string);
delay(10000);
}

//Check if any of the array numbers exist in the 0 thru 8 loop
bool checkExist(int iteration, int array[]){
bool result;

for(int j=0;j<=sizeof(array);j++){
if(iteration == array[j]){
result = true;
break;
}else{
result = false;
}
}
return result;
}
``````

if you have a var that indicates the arraySize of the array holding the numbers (this can vary over time) you could create a byte that holds the value to be shiftedOut().

something like

``````void doLeds()
{
uint8_t leds = 0;
for (int i = 0; i < arraySize; i++)
{
leds |=  1 << array[i];
}
shiftOut(dataPin, clockPin, MSBFIRST, leds);
}
``````

I tried this method but it doesnt seem to work for my application. I have a String "B01001000" that is exactly what I want to shiftout. I just cant seem to get the same into byte form.

shiftOut(dataPin,clockPin,MSBFIRST,B01001000);

the code
leds |= 1 << array
uses a bitmask to set the bits. Normally bits are count [76543210] but I see in your code that you count them [12345678]. My mistake
try this
`* *void doLeds() {   uint8_t leds = 0;   for (int i = 0; i < arraySize; i++)   {     leds |=  1 << array[i];   }   Serial.println(leds, BIN);  // for debug purpose   shiftOut(dataPin, clockPin, LSBFIRST, leds);  // changed MSBFIRST => LSBFIRST to change direction }* *`
or
`* *void doLeds() {   uint8_t leds = 0;   for (int i = 0; i < arraySize; i++)   {     leds |=  1 << (8-array[i]);  // change n => 8-n   }   Serial.println(leds, BIN);  // for debug purpose   shiftOut(dataPin, clockPin, MSBFIRST, leds); }* *`

Wow....Thats amazing. Thanks for the help man. This worked perfectly for what I was trying to do. What exactly is the |= operation? (Forgive me. I am a vb and java programmer attemting C++)

Thanks again.

Aaron

x |= y is a shorthand note for x = x | y

The | operator is a bitwise or
The << operator is a bitshift

similar C++ has
x += 3 => x = x + 3
y %= 5 => y = y % 5

z++ => z = z + 1
z-- => z = z -1

These are just shorthand notes for the programmer, but thanks to it the following is complete legal C

a = ++x + x++;

The use of bitwise operators are important when doing IO and setregisters etc. See also - Arduino Reference - Arduino Reference for doing lowlevel digitalWrite()

Thanks for the lesson and great links! You have been extremely helpful.

If you are trying to set & clear individual bits, you can do it like this also: