Hi All,
I'm building an 8-bit computer and want to use an arduino for the display output. I have found a fantastic library for driving multiplexed 7 segment LED displays which is working well. My idea is to place the Arduino behind a bus transceiver, so when the output is enabled, the binary data from the 8 bit bus will go into the arduino, which will convert it to an int, then display it on the 7 segment LED displays.
I'm using an arduino nano for this. The LED display is already up and running with no issue, so ignore that for the time being. My issue is the conversion of the binary data to decimal.
Pins 2-9 are plugged in to the bus, which I am reading like so:
byte bcdInputPins[] {2, 3, 4, 5, 6, 7, 8, 9};
then I have a method to read the data:
int readInput(){
Serial.println("--------------");
String rawbinaryString;for (int i = 0; i <= sizeof(bcdInputPins) -1; i++) {
rawbinaryString += String(digitalRead(bcdInputPins[i]));
}//reverse string
Serial.println("Raw binary string = " + rawbinaryString);char s[8];
rawbinaryString.toCharArray(s, 8);int result = readBinaryString(s);
Serial.println("int result from string: " +String(result));return 1;
}
this is giving me the following output, which is correct
Raw binary string = 00100000
I'm passing this result to a method I found on this forum, to convert it to an int:
int readBinaryString(char* s) {
int value = 0;
for (int i=0; i< strlen(s)+1; i++) // for every character in the string strlen(s) returns the length of a char array
{
value *= 2; // double the result so far
if (s[i] == '1') value++; //add 1 if needed
}
return value;
}
The problem here, is that method is returning 32. I know that is actually the correct value, but I need it to return 4. I know that it's the leading 0's that are causing the issue, as 4 in binary is 100.
Is there a smarter way to do this?
To clarify a bit further, my bus has to be a fixed width of 8, so on my bus I have the following convention:
0 = 00000000
1 = 10000000
2 = 01000000
3 = 11000000
4 = 00100000
5 = 10100000
and so on....
is there a tidier way of converting this to an int?