generating a byte from multiple IOs

hi all,

what i am trying to do, is i have 8 inputs, i want to generate a hex byte from the states of all 8 inputs. i cant use any of the PORTs one is only 6 pins long because of the crystal, one crosses over the serial, and the last one (PORTC across the analog pins) doesn't seem to be reading the last two pins. so i want to manually read each pin and build a binary number that i can convert to hex and send down the serial.

is that possible? am i missing something?

thanks everyone!

Certainly…

MyByte =
digitalRead( Bit0Pin ) |
(digitalRead( Bit1Pin ) << 1 ) |
(digitalRead( Bit2Pin ) << 2 ) |
(digitalRead( Bit3Pin ) << 3 ) // …

wow thanks very much for the fast reply!

i just tried your example, obviously extending it for all 8 inputs, i then did a Serial.print(MyByte,BIN); just to see if it matches up and i am getting 5 digits instead of 8 any ideas?

Post your entire Sketch. Please use "code tags".

ok this is a warning my code is a mess because its just test at the moment

int adPin1 =  2;
int adPin2 =  3;
int adPin3 =  4;
int adPin4 =  5;
int adPin5 =  6;
int adPin6 =  7;
int adPin7 =  8;
int adPin8 =  9;
int readPin =  13;
int daPin0 =  14;
int daPin1 =  15;
int daPin2 =  16;
int daPin3 =  17;
int daPin4 =  18;
int daPin5 =  19;
int daPin6 =  10;
int daPin7 =  11;

int dataValue;
byte MyByte;

void setup()   {                
  Serial.begin(9600);
  Serial.println("online");
  pinMode(readPin, OUTPUT);
  pinMode(adPin1, OUTPUT);
  pinMode(adPin2, OUTPUT);
  pinMode(adPin3, OUTPUT);
  pinMode(adPin4, OUTPUT);
  pinMode(adPin5, OUTPUT);
  pinMode(adPin6, OUTPUT);
  pinMode(adPin7, OUTPUT);
  pinMode(adPin8, OUTPUT);
  pinMode(daPin0, INPUT);
  pinMode(daPin1, INPUT);
  pinMode(daPin2, INPUT);
  pinMode(daPin3, INPUT);
  pinMode(daPin4, INPUT);
  pinMode(daPin5, INPUT);
  pinMode(daPin6, INPUT);
  pinMode(daPin7, INPUT);
  
  digitalWrite(readPin, HIGH);  
  DDRC = B00000000;
  
  ClearAddress();
// digitalWrite(adPin1,HIGH);
 digitalWrite(adPin2,HIGH);
// digitalWrite(adPin3,HIGH);
// digitalWrite(adPin4,HIGH);
 digitalWrite(adPin5,HIGH); 
 digitalWrite(adPin6,HIGH);
// digitalWrite(adPin7,HIGH); 
 digitalWrite(adPin8,HIGH);
 
ReadData();
 
}


void loop()                     
{

}
void ReadData() {
  digitalWrite(readPin, LOW);
  delay(10);
MyByte =
  digitalRead(daPin0)|
  (digitalRead(daPin1)<< 1) |
  (digitalRead(daPin2)<< 2) |
  (digitalRead(daPin3)<< 3) |
  (digitalRead(daPin4)<< 4) |
  (digitalRead(daPin5)<< 5) |
  (digitalRead(daPin6)<< 6) |
  (digitalRead(daPin7)<< 7);
  
// Serial.print(PINC,BIN);  
 Serial.print(MyByte,BIN);
 digitalWrite(readPin, HIGH);  
}

void ClearAddress() {
 digitalWrite(adPin1,LOW);
 digitalWrite(adPin2,LOW);
 digitalWrite(adPin3,LOW);
 digitalWrite(adPin4,LOW);
 digitalWrite(adPin5,LOW);
 digitalWrite(adPin6,LOW);
 digitalWrite(adPin7,LOW);
 digitalWrite(adPin8,LOW); 
}

i think i have worked it out, the serial window doesn’t display the first bits if they are 0