# how to covnert data from 8 pins to one number

I am reading data from 8 digital pins 0 or 1

so, how can I convert or put together 0 or 1 from 8 pins into one byte, number (0 to 255) or HEX

thanks

Look at bitSet() and bitWrite()

Try something like this

``````byte pinArray[] = {13,12,9,8,6,5,4,3}; // 8 random pins, say MSB is the leftmost, i.e. pin 13
byte newBit;
byte dataByte;
byte x;

void setup(){
for (x=0; x<8; x=x+1){
pinMode (pinArray[x], INPUT_PULLUP); // pin  high unless pulled low with a switch or something

}
Serial.begin(9600);
}
void loop(){
dataByte = 0;
for (x=0; x<8; x=x+1){
dataByte = dataByte | (newBit<<x); //shift the bit into position and put in its place in the data byte
}
Serial.print(dataByte, HEX);
delay (1000); // keep from flooding the PC
}
``````

Try something like this

``````byte pinArray[] = {13,12,9,8,6,5,4,3}; // 8 random pins, say MSB is the leftmost, i.e. pin 13
``````

byte newBit;
byte dataByte;
byte x;

void setup(){
for (x=0; x<8; x=x+1){
pinMode (pinArray, INPUT_PULLUP); // pin  high unless pulled low with a switch or something

}
Serial.begin(9600);
}
void loop(){
dataByte = 0;
for (x=0; x<8; x=x+1){
dataByte = dataByte | (newBit<<x); //shift the bit into position and put in its place in the data byte
}
Serial.print(dataByte, HEX);
delay (1000); // keep from flooding the PC
}

That code abuses the digitalRead() API.
It is assuming that digitalRead() returns a 1 or a 0. But that isn’t how digitalRead() is defined.
According the documentation: digitalRead() - Arduino Reference

digitalRead() returns HIGH or LOW. Not 1 or zero.
While Arduino core implementations do seem to have chosen using 1 for HIGH and zero for LOW, it is not specified nor required to be that way.
So to assume HIGH is 1 and LOW is zero abuses the API by using specific implementation specific information that is not part of the API specification nor required to work that way.

It would be better to do something like this:

``````void loop(){
dataByte = 0;
for (x=0; x<8; x=x+1){
if(digitalRead(pinArray[x]) == HIGH) // test pin for HIGH
dataByte = dataByte | (1<<x); //shift the bit into position and OR into its place in the data byte
}
Serial.print(dataByte, HEX);
delay (1000); // keep from flooding the PC
}
``````

It also avoids doing shifting and ORing of the zero bits.

— bill

If byte newBit = digitalRead(); is incorrect, then is this one correct: bool newBit = digitalRead();?

I think they're both implicit narrowing casts, aren't they?

GolamMostafa:
If byte newBit = digitalRead(); is incorrect, then is this one correct: bool newBit = digitalRead();?

Being pedantic, neither is actually correct.
The documentation is silent on the actual value or type of the digitalRead() return value.
It merely states that the return value is HIGH or LOW. It does not specify a type nor a value for each symbol.
It is abusing the API to assume HIGH is 1 / true or LOW is zero / false.

From its origin, Arduino has been very sloppy in its definition and documentation of return values for its API functions.
IMO, it was too much simplification.

What all this means is that is that in order to have maximum portability and avoid any abuse of the API, which could cause potential issues down the road, you must use the API return values as they are documented and not assume any specific information or implementation specific knowledge that is outside the documentation.
For digitalRead() it means you can not ever do a direct assignment of the return value from digitalRead() to a variable or use it in an expression; i.e. you must test it then do something based on it being HIGH or LOW.

This is what I was saying in post #3.

All that said, the code that CrossRoads posted will run on the Arduino.cc cores like the AVR core since in those cores HIGH and LOW are simple #defines of 1 for HIGH and zero for LOW.
All the other cores I've seen so far have also chosen to use #defines for HIGH / LOW with 1 for HIGH and zero for LOW so while the code is abusing the API, it will "work" with today's cores.

But where things could break is if all of a sudden HIGH and LOW become something other than simple defines of 1 and zero.
Once HIGH and LOW have an actual type, it could cause code that does assignments or uses them in expressions to break.
This is one example of why it is best not to abuse an API.

--- bill