The Arduino code is open source - if you look in <yourinstall>/hardware/arduino/core/arduino/wiring_digital.c you can see the code for pinMode().

It looks feasible to alter this to return the previous mode - perhaps suggest this to the Arduino team as a future improvement? Or if you feel capable take a backup and experiment with your own version.

You are right it would probably be best to add a new function getPinMode() to sense the current mode without changing it.

Another useful functionality might be a call to "register" control of a pin - so that conflicting libraries can automatically detect the conflict and return an error code at start up? Some other library may think it owns a pin whatever the pinMode. Something like pinUsed() and markPinUsed().

Another useful functionality might be a call to "register" control of a pin - so that conflicting libraries can automatically detect the conflict and return an error code at start up?

That's similar to what I'm doing with some code I'm writing at the moment. It works well especially when there are non-obvious clashes, for example you use PWM and later try to set the same pin as an input.

In a nutshell any multiple mode changes to a pin are flagged, you can however mask pins if you expect to change their mode more than once.

Wow! Thank you all for your responses. This is great. I didn't know the code was open source; moreover, the example for the uno should work well (my class is using uno's as well). Man, this is awesome - thanks again for all the feedback and info.

It's important because I'm really limited to the amount of time that I have for "in-class" implementations. So, if I can at least say, "hey, this is a problem - go fix it" - it gives the student a more focused on approach on what needs to be solved, but yea - this is a great idea too - just setting the pin up myself, and remembering its setting.... cool...

I fear that I'm not going to have enough time to run around and assist each student w/ his or her problem, so it's nice for me to make this library that I can run simple unit tests against the code - ensuring that the student is using the functions provided by the arduino library correctly.

anyway, this is awesome - I wanted to thank you all again for the great feedback!

Christopher, just so I understand, the intent is to provide a library (that includes getPinMode() probably among other things) that the students will include in their sketch, and that will be called at some point(s) to check various things?

Or is the intent to run checks against source code, on some other platform?

I think the function I provided should work with any board that the Arduino environment supports, since it calls the same support functions as digitalRead(), and these are designed flexibly to operate with different boards. But like I say, I only tested it on an Uno (and a clone at that.)