detect pin mode possible?

WizenedEE:

afasias:
this compiles but I can't test it now. I'm still not sure about the "oldSREG = SREG" trick and the CLI that are used in pinMode()

cli() disables interrupts, and SREG = oldSREG restores them to the state they were at when oldSREG was saved.

The general rule is that you'll want to save the status register (SREG) somewhere in your function (doesn't matter where) before cli(). You'll want to call cli() right before you do any manipulation of ports (eg reg and out). Then right after you finish manipulating the ports you want to restore the status register.

However, I think reading them without disabling interrupts is okay because they're 1 byte and therefore atomic. Get a second opinion, though.

I also think that a single byte read is atomic by nature on a 8 bit AVR chip.

Now if you do ever have the need to 'protect code' requiring atomic protection that arduino has the needed functions already defined for you that will handle saving and restoring the SREG register:

void loop()
{
  noInterrupts();
  // critical, time-sensitive code here
  interrupts();
  // other code here
}