4 bit variable to 4 different pins

Good evening everyone.I hoping for some help. I’m looking to take a 4 bit variable and use that to turn on and off 4 different digital pins. Any and all help is appreciated.

Since none of the Arduino have a 4 bit variable, do you mean 4 bits of an 8 bit variable? If so what are you really trying to do?

Start by reading about bitwise & AND operations…
If the value you’re testing ANDed with the appropriate bits are ‘true’, set those output bits.

Depending on which port pins you’re targeting, you might push the value directly to that port in parallel.

There are no 4bit variables in Arduino, the minimum size is 8 bits ( = 1 byte)
You can use it then to turn on and off digital pins, but not any pins - only the pins that belongs to the same port.

digitalWrite( yourPin, bitRead( yourVariable, bitNumber : HIGH ? LOW );

Which Arduino?
The smallest Arduino memory storage space is 8 bits, which 4 bits of your variable do you want to transfer to which pins?

You probably meant

digitalWrite( yourPin, bitRead( yourVariable, bitNumber) ? HIGH : LOW );

this is nice when yourPin is supplied by indexing an array of four pin numbers.

  digitalWrite(myPin[bitNumber], bitRead( myVariable, bitNumber) ? HIGH : LOW);

a7

Try it here

Wokwi_badge bit read example


// https://wokwi.com/projects/389676427778338817
// https://forum.arduino.cc/t/4-bit-variable-to-4-different-pins/1223620

int yourPin[4] = {4, 6, 8, 9};

void setup() {
  Serial.begin(115200);
  Serial.println("jello whirled!\n");

  for (int ii = 0; ii < 4; ii++)
    pinMode(yourPin[ii], OUTPUT);

    for (int yourVariable = 0; yourVariable < 16; yourVariable++) {
      Serial.print(yourVariable, HEX); Serial.print("     ");

      for (int bitNumber = 0; bitNumber < 4; bitNumber++) {
        Serial.print(bitRead(yourVariable, bitNumber));
        digitalWrite(yourPin[bitNumber], bitRead(yourVariable, bitNumber) ? HIGH : LOW);
        Serial.print("         ");
      }

    Serial.println("");
    delay(555);
  }
}

void loop() {
}

a7

Oops! Nice catch! Thanks!

why not

const byte PinOut [] = { 10, 11, 12, 13 };
const int  Nout = sizeof(PinOut);

void
output (
    int  val,
    int  nBits )
{
    for (unsigned n = 0; n < Nout; n++)  {
        digitalWrite (PinOut [n], val & 1);
        val >>= 1;
    }
}

void
loop (void)
{
    if (Serial.available ())  {
        int val = Serial.read () - '0';
        if (0 <= val && val < 16)
            output (val, 4);
    }
}

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

    for (unsigned n = 0; n < Nout; n++)
        pinMode (PinOut [n], OUTPUT);
}

Nice!

It's like a little puzzle - stab around on the keyboard to see how to make the hex digits a..f or A..F.

Fun.

a7

Why not add a few more language features for consideration?

const byte PinOut [] = { 10, 11, 12, 13 };
const int  Nout = sizeof(PinOut);

void
output (
    int  val,
    int  nBits )
{
    for (unsigned n = 0; n < Nout; n++)  {
        digitalWrite (PinOut [n], val & 1);
        val >>= 1;
    }
}

void
loop (void)
{

  if (Serial.available ()) {
 
    int val = Serial.read ();

    switch (val) {
    case '0' ... '9' :
      val -= '0';
      break;

      case 'a' ... 'f' :
        val -= 'a' - 10;
        break;

      case 'A' ... 'F' :
        val -= 'A' - 10;
        break;

     default :
       val = 32767;
    }

    if (0 <= val && val < 16)
      output (val, 4);
  }
}

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

    for (unsigned n = 0; n < Nout; n++)
        pinMode (PinOut [n], OUTPUT);
}

a7

They don't have to belong to the same port.

Well, yes, I already see that you have taken the banal path of outputting one pin at a time, using a digitalWrite in a loop.

I meant parallel output to all 4 pins at the same time through the I/O register (i e PORTD) - for this it is necessary that the pins belong to the same port.

by the way - if you output data to a pin via digitalWrite() one at a time, it’s unclear why there are all these difficulties with packing the state of multiple pins into one variable?

I wouldn't recommend using direct port manipulation unless we know there is a genuine need to do it. It is more efficient, but it makes the code less portable between different models of Arduino.

Right now, we don't even know for sure that an Arduino is being used! @billystone90 mentions 4-bit variable, which doesn't exist in Arduino language, as far as I know.

1 Like

I agree, but then I don't see the point of using a bit-array variable for this :slight_smile:

1 Like

Until @billystone90 comes back, we all just having fun. Who knows but she where these alleged four bits come from?

a7

1 Like

: ; < = > ?

Is that some kind of chart showing all the characters and a standard encoding for them?

a7

? ASCII