digitalWrite when between certain DEC

Hello

I programmed this a while ago where pin 2 is my LSB and pin 9 is my MSB:

 for(int i = 0, m = 2, k = 3; i<=64; i++){
    if(input>= m && input <= k){
    digitalWrite(3, HIGH);
  } else{
digitalWrite(3, LOW);
}
  m += 4;
  k += 4;
   }

I just realised this is an programming mistake and re-wrote one of the bits to:

if((input>= 64 && input <= 127) || (input>= 192 && input <= 255)){
    digitalWrite(8, HIGH);
  } else {
  digitalWrite(8, LOW);
   }
   //---------------Bit 8------------------//
    if(input>= 128 && input <= 255){
    digitalWrite(9, HIGH);
  } else {
    digitalWrite(9, LOW);
  }

However, the less significant the bit gets, thre more I have to define between which values input has to be to enable digitalWrite. Is there a better optimative way?

The reason why I am programming this way is because I am trying to get a linear voltage behavior with a R2R ladder including a op-amp. Since this is easier

for(input = 0; input <= 103; input++){ // 0V naar 4V
writeBit();
}
for(input = 103; input <= 203; input++){ // 4V naar 9V
writeBit();
delayMicroseconds(5);
}

than

PORTD = B00000000;
PORTD = B00000001;
PORTD = B00000010;
PORTD = B00000011;
// etc....

Why not do the following?

input = some_processing_or_whatever();
PORTD = input;

Which Arduino?

Just the arduino uno.

LightuC:
Why not do the following?

input = some_processing_or_whatever();

PORTD = input;

.... I'll try this out. I might have... overthink a bit.... I can use Decimal for the input variable and it will make binary out of it?

outsider:
Which Arduino?

Arduino Uno.

Decimal and binary are just to interpretations to a number. The (decimal) number 62 for example is equivalent to 3E (hex) or 00111110 (binary) or 76 (octal) or ... They all represent the same number, but use a different base. In memory they will be exactly the same. So these three statements are the same:

PORTD = 62;
PORTD = 0x3E;
PORTD = 0b00111110;

LightuC:
Decimal and binary are just to interpretations to a number. The (decimal) number 62 for example is equivalent to 3E (hex) or 00111110 (binary) or 76 (octal) or ... They all represent the same number, but use a different base. In memory they will be exactly the same. So these three statements are the same:

PORTD = 62;

PORTD = 0x3E;
PORTD = 0b00111110;

Thanks for the help. I have made it working however, PORTD uses pin 0 and 1 which are RX and TX pins. It's not the most ideal but it works and is the most fast solution at the moment.

duykhang2:
Thanks for the help. I have made it working however, PORTD uses pin 0 and 1 which are RX and TX pins. It's not the most idea but it works and is the most fast solution at the moment.

If other pins from the PORT are used in another way, you need to perform a read-modify-write cycle on the port:

// read current content of PORTD
uint8_t currentPort = PORTD;
// clear all pins, except the ones that are used in another way
currentPort &= ((1 << 0) | (1 << 1));
// set your inputs, mask out the pins 0 and 1
currentPort |= input & 0xFC;
// write back the result
PORTD = currentPort;

You might also want to clear the input pins, as setting them will enable/disable the pullups.