Great!
why am i not setting the input pin during the setup() call
The pinMode() function configures the port as an input or an output. Technically, it configures the DDR
(Data Direction Register) which corresponds to the pin that is specified by pinMode(). If the bit of corresponding DDR is 0, the pin is configured as an input. If the bit of DDR is 1, the pin is configured as an output. The initial values of DDRs are zero, which is INPUT.
Basically, all pins just default to input 'mode'.
The actual code which is run when calling pinMode() looks something like this -
Code: Select all
void pinMode(uint8_t pin, uint8_t mode)
{
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
volatile uint8_t *reg, *out;
if (port == NOT_A_PIN) return;
reg = portModeRegister(port);
out = portOutputRegister(port);
if (mode == INPUT) {
uint8_t oldSREG = SREG;
cli();
*reg &= ~bit;
*out &= ~bit;
SREG = oldSREG;
} else if (mode == INPUT_PULLUP) {
uint8_t oldSREG = SREG;
cli();
*reg &= ~bit;
*out |= bit;
SREG = oldSREG;
} else {
uint8_t oldSREG = SREG;
cli();
*reg |= bit;
SREG = oldSREG;
}
}
So...unless you have a need to call it more than once throughout a sketch, it's best to keep it in setup() -- Helps to avoid excess processing time, memory usage, and possible errors. Keeps the looping code simpler.