Port manipulation and bit read

Hi everybody. I already posted this topic but I still have not find solution.

I want to use the bitRead function: Serial.println(bitRead(PORTD,3)); //Reads bit 3 of register PORTD which contains the current state (high/low) of pin 3. It works fine. I need to replace PORTD with a variable like variable="PORTD" (because I want to read different ports depending of differents conditions in my sketch)

String variable ="PORTD"; Serial.println(bitRead(variable,3)); //doesn't work How can I do that

Thank you

String variable = "PORTD";

if ( variable == "PORTD" )
{
  Serial.println(bitRead(PORTD,3));
}

You need to read PIND.

http://playground.arduino.cc/Learning/PortManipulation

PORTD is the register for the state of the outputs. For example;

PORTD = B10101000; // sets digital pins 7,5,3 HIGH

You will only see 5 volts on these pins however if the pins have been set as outputs using the DDRD register or with pinMode().

PIND is the input register variable ? it will read all of the digital input pins at the same time.

From the ATMEL full doc covering the 328, in section 14. I/O Ports:

Three I/O memory address locations are allocated for each port, one each for the Data Register – PORTx, Data Direction Register – DDRx, and the Port Input Pins – PINx. The Port Input Pins I/O location is read only, while the Data Register and the Data Direction Register are read/write. However, writing a logic one to a bit in the PINx Register, will result in a toggle in the corresponding bit in the Data Register

That highlighted bit is a neat trick I had to be sure of. Writing to PINx changes the Port, reading PINx is safe.

Thank you Codingbadly. Yes I can use the if function. but to simplify my sketch I want to use: Serial.println (bitRead (variable,3)); when variable is sometimes PORTD or PORTE....

pchatill: but to simplify my sketch

Speaking of your sketch, if you would like more help... https://m.youtube.com/watch?v=tdQL_7osGzc

pchatill: Thank you Codingbadly. Yes I can use the if function. but to simplify my sketch I want to use: Serial.println (bitRead (variable,3)); when variable is sometimes PORTD or PORTE....

Why do you want to read the output states?

I apologize that I am commenting on such an old post, but couldn't the OP simply do this:

if(myCondition) {
    #define CHECKPORT PORTD
}
else {
    #define CHECKPORT PORTC
}

Serial.println(bitRead(CHECKPORT,3));

The preprocessor is blind to your C++. The compiler is blind to your macros.

In other words, the preprocessor sees this...

stuff-to-ignore
    #define CHECKPORT PORTD
stuff-to-ignore
    #define CHECKPORT PORTC
stuff-to-ignore CHECKPORT stuff-to-ignore

...and reduces it to this (with a warning) (there is more reduction that is not germane)...

stuff-to-ignore
stuff-to-ignore
stuff-to-ignore PORTC stuff-to-ignore

...so the compiler just sees this...

if(myCondition) {
}
else {
}
Serial.println(bitRead(PORTC,3));

Weird how random posts, long dead, are brought back like this.

Anyway, this example compiles and seems to work on my 2560 but probably breaks a bunch of rules:

volatile uint8_t *regInput;
static bool
    bReg = false;
byte
    val;

void setup() 
{
    Serial.begin(9600);
    pinMode( 5, INPUT_PULLUP ); //PE3
    pinMode( 6, INPUT_PULLUP ); //PH3
}

void loop() 
{
    if( bReg )
        regInput = &PINE;
    else
        regInput = &PINH;
        
    val = *regInput;
    Serial.print( (bReg == false) ? "PINH ":"PINE " );    
    Serial.println( val, HEX );

    bReg ^= true;
}