Hello,
my son made a programming mistake by confusing digitalWrite with pinMode: he wrote things like
pinMode(pin, LOW); and pinMode(pin, HIGH); which I thought should be meaningless, however, it does compile, and when applied to the hardware it turns out to look like this:
pinMode(pin, LOW); might to do the same as digitalWrite(pin, HIGH); (not proven in the application, however outcome is the same)
pinMode(pin, HIGH);seems to do the same as digitalWrite(pin, LOW); (brings the pin to ground)
isn't that weird?
any idea of the reason(s) why it is doing that?
Cheers,
Pascal.
pinMode(pin, INPUT_PULLUP);
turns on the pullup resistors, which often confuses people because the pin outputs about 5V when measured by a multimeter (and it will light up an LED weakly).
It may be that misuse of LOW in that function call also turns on the pullup resistor.
Edit: Here is the source code for pinMode(). If the second parameter's value is not INPUT (0) or INPUT_PULLUP (2), the pin becomes an output.
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;
// JWS: can I let the optimizer do this?
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;
}
}
INPUT, OUTPUT, HIGH and LOW are just different names for 0 and 1. pinMode() doesn't know which one was used.
Arduino.h
#define HIGH 0x1
#define LOW 0x0
#define INPUT 0x0
#define OUTPUT 0x1
#define INPUT_PULLUP 0x2
pinMode(pin, LOW) is the same as pinMode(pin, INPUT)
pinMode(pin, HIGH) is the same as pinMode(pin, OUTPUT)