pin macros

I am learning to use the pin macros from pins_arduino.h

The code below attempts to print out the ports from some of the pin.

The problem I am having is when I accessing the macro within the for loop
I do not get the correct values for the ports.

The correct values are 2, 3 & 4 for ports B,C, & D
I do get the correct value when not using a for loop.

Here is the code.

#include <avr/pgmspace.h>
uint8_t myport,i ;



void setup() 
{   
  Serial.begin(9600);  
  while(!Serial);
	
  i=10;
  myport = digital_pin_to_port_PGM[i];   //get port for pin 10
  Serial.println(myport); 
  
  Serial.println();
   
  for(i=0; i<14; i++) {                   
     myport = digital_pin_to_port_PGM[i];  //get port for pin i
     Serial.println(myport); 
  }
                
}

void loop() {
	

}

You should not be using digital_pin_to_port_PGM directly. That array is stored in PROGMEM so you can not access it like a normal variable. You have to read program memory.

That is why they have this macro

#define digitalPinToPort(P) ( pgm_read_byte( digital_pin_to_port_PGM + (P) ) )

Use that instead.

That worked !

Thank you, karma++