I've been fumbling with data types for a few hours on this, so I'm going to attempt to articulately request some help from the seasoned users and pros here:
This is my first attempt using ShiftOut(), and I'm using it with a 74HC595.
Where I'm getting stuck is the source of the binary value that I want to "shiftOut" into the 64HC595. It's a string of zeros and ones, but it's value is a string.
For example, "00111010".
What I'm struggling with is how to convert this to a data type that I can use in ShiftOut.
No surprise, ShiftOut objects to being fed a string of any kind.
String Upper = "00111010"; ///this string is actually derived from another process, which I won't include here
digitalWrite(LatchPin1,LOW);
shiftOut(DataPin1,ClockPin1,MSBFIRST,Upper);
digitalWrite(LatchPin1,HIGH);
error: cannot convert 'String' to 'uint8_t
I looked at toint(), but it's not an integer either, it's an 8-bit binary value that I want to use.
But if I use bitRead(), that seems to convert "00111010" into the binary value of the ascii characters, which is not what I want to do here...
0011000000110000001100010011000100110001001100000011000100110000
So, I feel like I'm chasing my tail, and I'm open to suggestions.
Before we get into how to do the conversion, it's important to ask: why is it a string? Can that be avoided? If it was an int from the beginning, no conversion would be needed. Put another way, instead of solving this problem, can the problem be avoided completely?
String Upper = "00111010"; ///this string is actually derived from another process, which I won't include here
byte v = strtol(Upper.c_str(), NULL, 2);
digitalWrite(LatchPin1,LOW);
shiftOut(DataPin1,ClockPin1,MSBFIRST,v);
digitalWrite(LatchPin1,HIGH);
maybe just ditch the shiftOut and do it from scratch
String Upper = "00111010";
digitalWrite(LatchPin1,LOW);
//in case MSB out first
for (uint8_t i = 0; i < Upper.length() ; i++) {
digitalWrite(DataPin1, Upper.charAt(i)&0x01);
digitalWrite(ClockPin1, HIGH);
digitalWrite(ClockPin1, LOW);
}
//-----------------
//in case LSB out first
for (uint8_t i = Upper.length(); i >0 ; i--) {
digitalWrite(DataPin1, Upper.charAt(i-1)&0x01);
digitalWrite(ClockPin1, HIGH);
digitalWrite(ClockPin1, LOW);
}
//-----------------
digitalWrite(LatchPin1,HIGH);
presumably you are receiving data as ASCII character and need to pass a binary argument.
0011 1010 is 0x3A. i don't know of any library functions to convert binary ASCII sequences to binary values, but there routine to handle hex an decimal, of course.
sscanf ("0x3a", "0x%2x" & val); translates it's first argument, a c-string to a binary value