Port manipulation with bitread

Another possibility is to create an array (can be in PROGMEM) to contain the different port addresses your interested in and then use your variable to choose which element of the array you will work with. Something like this:

#include <Arduino.h>
#include <avr/io.h>

void setup()
{
  volatile uint8_t PortVariables[] = { PORTB, PORTD };
  uint8_t SelectionVariable = 1;  // Setting it to default to PORTD

  Serial.begin(9600);
  
  // Send the value of bit 3 of PORT D to serial console
  Serial.println(bitRead(SelectionVariable,3));

  SelectionVariable = 0; // Setting it to PORTB

  // Send the value of bit 3 of PORT B to serial console
  Serial.println(bitRead(SelectionVariable,3));

}

void loop()
{
}