how to convert 8 pcs inputs to binary ?

Hi everynone..

I just start to work with Arduino.
I quite good to program PLC. But a lot of reasons push me to learn Arduino.

Now i need to convert 8 inputs to binary. Actually i want to read inputs and than send as a byte to Siemens PLC.

I was look at the forum but cant get solition. Some one post solition about "shift" command. But it doenst work.

Thank you for help.

Why the vote? I voted for qwerty.

Post what you tried that didn't work and we can see about fixing it.
But basically it is a matter of reading the bits in and setting the bits in a byte. Have you seen the bitSet() function?

If you name the inputs I0 to I7, the byte that holds them all can be calculated like this:
I0 + I1<<1 + I2<<2 + I3<<3 + I4<<4 + I5<<5 + I6<<6 + I7<<7

Sorry for vote. It was my mistake. Its my first time to use this forum. And thank you for voted :slight_smile:

I cant find the code that i couldt work.

But after your clue i researc about bitSet () and find a post below.
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1247060702/8

Grumpy_Mike
you have been posted something to the this topic. But i still cant find a solition. I cant find any information about PIND, PINB vs.

florinc's way is more simple for me. i did it like this.
But it doesnt work. Only get "0" after inputs become TRUE. Nothing effect.

I need to convert 8 inputs to a byte value. And then i will send it to the PLC.
And please write a complate code.
Thank you..

int p4=4, p5=5, p6=6, p7=7;
  byte var;
  boolean a,b,c,d;

void setup() {          
Serial.begin (9600);  
pinMode(p4, INPUT); 
pinMode(p5, INPUT); 
pinMode(p6, INPUT); 
pinMode(p7, INPUT); 
}

void loop() {
a = digitalRead(p4);
b = digitalRead(p4);
c = digitalRead(p4);  
d = digitalRead(p4);

var = a + b<<1 + c<<2 + d<<3;
Serial.println (var, BIN);
delay (500);
}

Use the #icon when posting code.

In this bit:-

a = digitalRead(p4);
b = digitalRead(p4);
c = digitalRead(p4); 
d = digitalRead(p4);

You are reading the same pin 4 times!

But it doesnt work. Only get "0" after inputs become TRUE. Nothing effect.

How did you connect the buttons (a schematic would be nice)?

a = digitalRead(p4);
b = digitalRead(p4);
c = digitalRead(p4);  
d = digitalRead(p4);

You are aware that you read just one button, aren't you?

Hi again ..

I try it as below ;

byte var;
byte OO, O1, O2, O3, O4, O5, O6, O7;

void setup() {          
  Serial.begin (9600);  

  pinMode(2, INPUT); 
  pinMode(3, INPUT); 
  pinMode(4, INPUT); 
  pinMode(5, INPUT); 
  pinMode(6, INPUT); 
  pinMode(7, INPUT); 
  pinMode(8, INPUT); 
  pinMode(9, INPUT); 
}

void loop() {

  OO = digitalRead(2);  
  O1 = digitalRead(3);
  O2 = digitalRead(4);
  O3 = digitalRead(5);  
  O4 = digitalRead(6);
  O5 = digitalRead(7);
  O6 = digitalRead(8);
  O7 = digitalRead(9);

  var = OO + O1<<1 + O2<<2 + 03<<3 + 04<<4 + 05<<5 + 06<<6 + 07<<7;
  Serial.println (var);
  delay (100);
}

But it doesnt work..
i only get zero on the serial monitor.

No more help until you start posting code using the #icon like you have been asked to do. :0

hint - you can even modify existing posts with the # button

Thank you for patience.

Just fixed messages.
waiting for your help.

Thank you.

How did you wire the buttons (show a diagram, photo, schematic etc)?
Normally, if all inputs are pulled up (10k resistors to Vcc), you should get 0xFF (all 1s). The button, when pressed, would connect the input to ground, so you should see 0 on that particular input.

Thanks for modifying the post.
Check your buttons are working with:-

So add some debug print statements:-

Serial.println (OO);
Serial.println (O1);
.........

Then change you code to:-

OO = digitalRead(2) & 1;  
  O1 = digitalRead(3) & 1;
  O2 = digitalRead(4)& 1;
  O3 = digitalRead(5)& 1;  
  O4 = digitalRead(6)& 1;
  O5 = digitalRead(7)& 1;
  O6 = digitalRead(8)& 1;
  O7 = digitalRead(9)& 1;

Hello,

are this 8 lines connected to 0V or 5V?. Otherwise you should switch on the internal pull up.

Andreas