How to Return Pin to Undefined State?

I think this is a hardware problem, and not a software problem, so I thought that this would be the best place to post it.

If anyone has ever seen a schematic diagram for an LED matrix, you know that the LEDs in the matrix are wired so that each row has a common ground, and each column a common (parallel) power. Or vice versa. I have built such circuits before on a breadboard, and it is possible to turn an individual LED on and off by connecting its row to ground and its column to power.

Now I have built such a circuit with the Arduino 328 LilyPad (which, as far as I'm aware, should function just like its non-sewable equivalent).

The problem is that once a particular pin has been defined as an OUTPUT, it seems to inherently have 0 volts (LOW), even if you haven't explicitly set it as such.

I have tried two program versions for my 3 x 3 matrix, in which pins 7 through 9 are rows/grounds, and 10 through 12 are columns/powers. In the first version, I set the pin modes in the setup() subroutine---OUTPUT for pins 7 through 12. When you set one of the columns/powers to HIGH, all three LEDs in that column come on, even though you haven't explicitly set all three rows/grounds to LOW/0.

I thought that perhaps waiting until I got inside a subroutine to set the pin modes would work, and that perhaps exiting the subroutine would cause the pins to lose their modes, and return to an undefined state. This works better than the first version. I pass the subroutine the row and column number, then set the pin mode and the voltage inside the subroutine. I used this order (1,1), (1,2), (1,3), (2,1), (2,2), etc. The first three calls actually work! Pins 8 and 9 haven't yet been defined as OUTPUTs, and the individual LEDs actually do turn on and then off. Hooray, I thought! (Rule of thumb: never say hooray until your program has completed.) Well, that exhilaration only lasted until the call to (2,1). The LEDs in (1,1) and (2,1) came on.

So obviously, it's the same problem once again: once a pin has been defined as an OUTPUT, it "remembers" that. I have been looking for a way to return a pin to the undefined state, like it was before the mode was set, but haven't found a way to do that. The only acceptable values for mode are OUTPUT and INPUT, and there is no value for undefined or null.

Does anyone know a way around this? I tried setting the rows/grounds to HIGH to turn them off, rather than setting the columns/powers to LOW, but they were still lighting (albeit weakly). I don't think that this is an issue with the programming, but here is the relevant portion of the code that worked for row 1...

void lightAnLED(unsigned char rowNumber, unsigned char columnNumber) { int rowPin; int colPin;

if (rowNumber == 1) { rowPin = 7; } else if (rowNumber == 2) { rowPin = 8; } else if (rowNumber == 3) { rowPin = 9; }

if (columnNumber == 1) { colPin = 10; } else if (columnNumber == 2) { colPin = 11; } else if (columnNumber == 3) { colPin = 12; }

pinMode(rowPin, OUTPUT); pinMode(colPin, OUTPUT); digitalWrite(rowPin, LOW); digitalWrite(colPin, HIGH); delay(2500); digitalWrite(colPin, LOW); delay(1000); }

????? So... how to return a pin to the undefined state ?????

Thanks for your help!

I have been looking for a way to return a pin to the undefined state, like it was before the mode was set, but haven't found a way to do that. The only acceptable values for mode are OUTPUT and INPUT, and there is no value for undefined or null.

I don't believe there is such a thing as a "undefined state". The AVR datasheets say that all digital pins default to input mode with the internal pull-ups turned of which electrically means the pin assumes a high impedeance input mode. So to bring a digital pin that was prior setup as a digital ouput pin to such a 'default state', just perform the following:

pinMode(pin, INPUT); // set pin to input digitalWrite(pin, LOW); // turn off internal pullup resistor

Lefty

silly question maybe, but why dont you just digital write to them high?

Thank you thank you thank you RetroLefty!!!

YESsssss! It works!

I posted this on a couple other forums, and got no answer, but had a feeling that someone with more knowledge of the Arduino itself could point me in the right direction.

I actually had tried setting them to HIGH. That didn't work. They dimly lit, and there was other strange behavior.

I knew I had to find what the initial state was.

Reading the AVR datasheets is something I have now added to my To Do list...