questions about pinMode()

Briefly, my question is: How fast can one change pinMode back and forth?

I'm working on a set of drum triggers, and I'll have a bunch of analog sensors to read. For each trigger, I'm using a peak detector circuit with a diode, op amp, and capacitor that holds a voltage until I get a chance to take a reading via an analog pin. Once I've got the reading, I want to drain the capacitor to reset the circuit. I figure the way to do this is something like this:

// switch pin connected to the capacitor to low-impedance mode
pinMode(drainPin,OUTPUT);  // delay here to allow chip to stabilize?

// drain the capacitor
digitalWrite(drainPin,LOW); // allow time to drain capacitor to zero?

// switch drain pin back to high-impedance mode
pinMode(drainPin,INPUT();

I don't have an oscilloscope so I can't really figure out a good way to test if this is going to work the way I expect it to or not. (Aside from trial and error!) Do I need to insert a delay to let the chip stabilize a bit, or does the pin change modes more or less instantaneously?

And when I switch the pin to OUTPUT mode, isn't it already in a LOW state? (That's my impression from reading the reference.) In other words, can I just skip the middle statement?

And does anyone know how long it takes to drain a 1µF capacitor through an input pin? Will I need a delay before switching back to INPUT mode to be sure the capacitor is drained?

I sure would appreciate any insight on this. Thanks in advance!

Chip

How big is your capacitor? (I hope that's not too personal a question)

A rough rule of thumb for the resistance of a pin driver is about 50 ohms, so if you know the capacitance you're draining you can compute the time constant (tau=RC) and allow about 5 of those (5*RC) to say "it's discharged".

For example, with your 1uF capacitor then the time constant is about 50us and in about 250us you can expect your capacitor to be fully discharged. Give or take, more or less, YMMV, etc. That would be a good starting point for the delay.

The pin change itself is instantaneous (well...on the order of a few hundred nanoseconds to allow the instructions to actually execute).

And yes, you can leave out the middle statement that sets the pin output to 0. Once done, it's done for good until you change it (so you should do it once).

Awesome - thanks, RuggedCircuits! Great info, especially the bit about 50[ch937] for the pins. And thanks for reminding me it's [u]5*[/u]RC. (I would have gone with RC and wasted a lot of time scratching my head!)

I also came across the stuff in the Reference pages about setting the ports directly. That will help improve response time, too. Excellent!

Thanks again for your help-

Chip