Ok, here's the erratic code:
void setup()
{
pinMode(11, OUTPUT);
pinMode(13, OUTPUT);
pinMode(12, INPUT_PULLUP);
}
void loop()
{
digitalWrite(11, HIGH);
if(digitalRead(12) == LOW)
{
digitalWrite(13, LOW);
}
else
{
digitalWrite(13, HIGH);
}
delay(500);
digitalWrite(11, LOW);
if(digitalRead(12) == LOW)
{
digitalWrite(13, LOW);
}
else
{
digitalWrite(13, HIGH);
}
delay(500);
}
When pin11 is LOW, that "line" of switches is scanned. When HIGH, another line of switches is scanned. (In this test sketch, I have stripped off all other lines and use just one switch to isolate the problem.) And the problem was that even though pin12 wasn't connected to a switch closed to LOW and it was pulled up, it still returned LOW occationally.
But here's the working code:
void setup()
{
pinMode(11, OUTPUT);
pinMode(13, OUTPUT);
pinMode(12, INPUT_PULLUP);
}
void loop()
{
digitalWrite(11, HIGH);
delay(1);
if(digitalRead(12) == LOW)
{
digitalWrite(13, LOW);
}
else
{
digitalWrite(13, HIGH);
}
delay(500);
digitalWrite(11, LOW);
delay(1);
if(digitalRead(12) == LOW)
{
digitalWrite(13, LOW);
}
else
{
digitalWrite(13, HIGH);
}
delay(500);
}
The only difference is the two "delay(1);" lines. It seems one millisecond is enough to settle down the induction or whatever that pin11 may have caused while going LOW.
Next step is to see if it still works when I remove the "delay(500);" lines. But I can't do that tonight, I'm busy with other stuff.