Is there an easy way to determine the current pin mode of a digital pin? Something like pinMode(pin) ?
The pin mode is what you set it to. Not that difficult to remember, if it's important.
But if you want to you can:
uint8_t getPinMode(uint8_t pin){
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
volatile uint8_t *reg, *out;
if (port == NOT_A_PIN) return 0xFF;
reg = portModeRegister(port);
out = portOutputRegister(port);
//Check for tris (DDxn)
if( (*reg) & bit ){ //is output
return OUTPUT;
}
else{ //is input
//Check state (PORTxn)
if( (*out) & bit ){ //is set
return INPUT_PULLUP;
}
else{ //is clear
return INPUT;
}
}
}
septillion, that's what I was looking for. Looks like a modified version of the standard pinMode function.
I'm a little confused on what exactly, is being returned by digitalPinToBitMask. Is it an 8 bit mask, that always has a 1 in the position of the pin in the corresponding port?
It indeed is. And yes, a byte with a one in the position of the pin in question. You can use is to mask the rest of the port register.
Although you can determine the current pinMode of a pin why make things complicated ? As suggested earlier, just set a variable to the state that you set it to or a more general variable holding the current state of the program. After all, what else is going to change the mode apart from your program ?
Sometimes it can be usefull if a library does stuff as well. And why save something that's already saved? If you save it again you have to change that state as well..
UKHeliBob, this Arduino is next to the electrical breaker panel in the garage. It's a Mega with an ethernet shield attached. All it does is report the state of inputs to a separate HTTP server, and do with outputs what the HTTP server requests.
It's a hassle to transfer *.ino files from my workstation in the office to the laptop, then take the laptop to the garage to upload to the Arduino. I would rather set it up so I can change the pinMode via HTTP, but before I do so I want to add a sanity check to the mode of the pin.
Why use a variable, for something that's already stored in a variable, then I would have to add a sanity check to make sure my variable corresponds to the existing variable.
I am intrigued. Can you share with us what the devices are that sometimes respond to outputs from the Arduino and sometimes provide inputs to the Arduino ?
Sounds like you might benefit from a standalone programmer - put your file on an SD card and walk out with the programmer, or just the SD card even and leave the programmer out there:
http://www.crossroadsfencing.com/BobuinoRev17/Programmer.html
