How can i take 4 bit binary input

i want to take 4 bit binary input from 4 pin (8,9,10,11)
to count 0000,0001,0010,0011,0100,................

i want to take 4 bit binary input from 4 pin (8,9,10,11)

The digitalRead() and bitWrite() functions come to mind.

please ,How can i used The bitWrite() function

You could do something like set up an array, and using a FOR loop, read the inputs from the four pins.

Psuedo:
byte array[3];
void loop {
for(count = 0; count<4; count ++)
{
array[count] = bitRead(8 + count, 1);
Serial.print(array[count])
}
Serial.println();
}
something like this.

i need to enter input value Randomly from 4 pin such as 0000,0010,0100,1000,............ then save this value entered in memory called value as 4 bit
then check the value saved
if (value==0010)
{

}
if (value==0100)
{

}

my problem i can't save the value input from pin 8,9,10,11 as four binary bit

MostafaHamdy:
my problem i can't save the value input from pin 8,9,10,11 as four binary bit

As PaulS explains, you can do this easily with the bitWrite() function. It's documented in the Arduino online reference - go look it up.

Something else to try, Arduino Reference - Arduino Reference

look at PINB, this reads in the inputs of pins 8 - 13 as 0000 0000 => (0,0,13,12 11,10,9,8)

so you could probably do
if(PINB == 00001111) {
...
}

else if (PINB == 00001010) {
...
}
etc

that might work, but try to get more info from that page.

when i used PIND that give weight of all pins on PORTD from 0 to 7 ,i only need weight of pins from 4 to 7

Oh, ok those ports can be set up as inputs or outputs with DDRD, look again at:

DDRD = B11111110; // sets Arduino pins 1 to 7 as outputs, pin 0 as input
DDRD = DDRD | B11111100; // this is safer as it sets pins 2 to 7 as outputs
// without changing the value of pins 0 & 1, which are RX & TX

So I think you want to do this,
DDRD = B00001110; //sets 4-7 as inputs, 1,2 & 3 as outputs, 0 as input

  • made a minor edit

MostafaHamdy:
when i used PIND that give weight of all pins on PORTD from 0 to 7 ,i only need weight of pins from 4 to 7

You can use bitwise AND(&) to mask off 0 to 3.

thanks :smiley:

I wouldn't recommend using direct port manipulation to solve this problem. Firstly because solving it using the Arduino runtime library (or ordinary 'C'/C++ bitwise operators) is very simple once you have got the concept of calling a function (and arguably easier to understand that directly manipulating and masking the state of the I/O ports) and secondly because if you use the runtime library then the resulting sketch will be portable. Accessing the ports directly isn't necessary and doesn't (in my opinion) make the solution any simpler.

PeterH:
I wouldn't recommend using direct port manipulation to solve this problem. Firstly because solving it using the Arduino runtime library (or ordinary 'C'/C++ bitwise operators) is very simple once you have got the concept of calling a function (and arguably easier to understand that directly manipulating and masking the state of the I/O ports) and secondly because if you use the runtime library then the resulting sketch will be portable. Accessing the ports directly isn't necessary and doesn't (in my opinion) make the solution any simpler.

Will you elaborate on that a little bit? Are you suggesting using digitalRead() and bitWrite(), 'Set() and 'Clear()? Can you use those library functions to capture the state of multiple bits at one instance in time?

ount 0000,0001,0010,0011,0100,..

Easy:

  d=(digitalRead(q3))?0x08:0x00 | //read q3
      (digitalRead(q2))?0x04:0x00 | //read q2
      (digitalRead(q1))?0x02:0x00 | //read q1
      (digitalRead(q0))?0x01:0x00;  //read q0

It would be easier and faster to use the port input data register.

Will you elaborate on that a little bit?

Irrational fear of dealing with hardware - an oxymoron given that embedded programming is about dealing with hardware and software.

PapaG:
Will you elaborate on that a little bit? Are you suggesting using digitalRead() and bitWrite(), 'Set() and 'Clear()? Can you use those library functions to capture the state of multiple bits at one instance in time?

That's what I was suggesting, yes. Although using multiple calls to digitalRead() means the different inputs are read at different times, you're only talking about a difference of milliseconds and nothing so far has indicated that the four inputs have to be read absolutely simultaneously. The nice thing about Arduino as a develpment environment is that it gives you a nice friendly API that is portable across the whole range of Arduino platforms. It doesn't stop advanced programmers from hitting the hardware directly if that's what they want, but imo the friendly API is worth having, especially for a relative newcomer as the OP appears to be. Using the API also has the advantage that it is easier to understand what the code does without having to read chip data sheets to see what the various registers do, also an advantage for people not experienced with low level programming on the Arduino platform.

Ok, I agree with that, but it is still a hassel to get the bits in a readable variable. I tried to put them in an array but I couldn't convert them without concatnating the zeros. So I went straight to the ports.

PeterH:

PapaG:
Will you elaborate on that a little bit? Are you suggesting using digitalRead() and bitWrite(), 'Set() and 'Clear()? Can you use those library functions to capture the state of multiple bits at one instance in time?

That's what I was suggesting, yes. Although using multiple calls to digitalRead() means the different inputs are read at different times, you're only talking about a difference of milliseconds and nothing so far has indicated that the four inputs have to be read absolutely simultaneously. The nice thing about Arduino as a develpment environment is that it gives you a nice friendly API that is portable across the whole range of Arduino platforms. It doesn't stop advanced programmers from hitting the hardware directly if that's what they want, but imo the friendly API is worth having, especially for a relative newcomer as the OP appears to be. Using the API also has the advantage that it is easier to understand what the code does without having to read chip data sheets to see what the various registers do, also an advantage for people not experienced with low level programming on the Arduino platform.

I agree with all that. I just wanted to make sure I hadn't missed some library function that read a whole port at a time.

I'm inclined to use the capabilities of a high level language that allow me to do what I could do in assembler if they're available, with portability across platforms of lesser importance. I'm not recommending anyone else do that though.

I used the same thing in assembler too, but its been almost 3 years since I had to write anything in assembler, so im a little rusty.

HazardsMind:
Ok, I agree with that, but it is still a hassel to get the bits in a readable variable. I tried to put them in an array but I couldn't convert them without concatnating the zeros. So I went straight to the ports.

You could certainly put them in an array for temporary storage, although I'd have to look up what type to declare for HIGH and LOW. Then you could read them sequentially from the array and based on the value, use the bitSet(x,n)/bitClear(x,n) function with the array index as n, to create a variable x.