Now that's really weird because, it works and you're right, it shouldn't!
I am running 0022. Here's the q&d test code that I just recompiled and it runs and does what I expect!
I ground the jumper in 7 to the jumper in GND and the monitor prints 0. I break the connect and it prints 1. Just before the closing bracket there it should say: return A;
But .. it .. WORKS!
void setup( void )
{
Serial.begin(9600);
pinMode( 7, INPUT );
digitalWrite( 7, HIGH );
pinMode( 3, INPUT );
digitalWrite( 3, HIGH );
}
byte wasPS3 = 1;
byte pinState3 = 1;
byte wasPS7 = 1;
byte pinState7 = 1;
byte getButton( byte pin )
{
byte A, B, C = 0;
A = digitalRead( pin );
while ( C < 4 ) // need 3 reads in a row, 5 ms apart, that match
{
delay( 5 );
B = digitalRead( pin );
if ( B == A )
{
C++;
}
else {
C = 0;
A = B;
}
}
}
void loop( void )
{
pinState3 = digitalRead( 3 );
if ( pinState3 != wasPS3 )
{
wasPS3 = pinState3;
Serial.println( pinState3 + 100, DEC );
}
pinState7 = getButton( 7 );
if ( pinState7 != wasPS7 )
{
wasPS7 = pinState7;
Serial.println( pinState7, DEC );
}
}
Must be something the compiler did. Maybe the first time ever a compiler fix my F-U.
I made the change and on test it still works:
byte getButton( byte pin )
{
byte A, B, C = 0;
A = digitalRead( pin );
while ( C < 4 ) // need 3 reads in a row, 5 ms apart, that match
{
delay( 5 );
B = digitalRead( pin );
if ( B == A )
{
C++;
}
else {
C = 0;
A = B;
}
}
return A;
}
And as far as should the button read HIGH or LOW when not pressed, the answer is that it depends on how it's wired up and what you do with the pins.
In this case the power is fed through about a 20k internal resistor to the pin the 'button' is connected to. There's almost not enough current there to light up a led (a red one, it was pathetic little glow and didn't bring pin 7 low... try it!) much less burn out a pin. It's safe and it doesn't require me extra parts beyond 3 jumpers and the UNO to run my test.
And my test says it takes dirty switches to need debouncing! Dirtier than touching jumper ends at least.
And LOL, 5V through 220R to a red led makes one very bright red led. Through 20k and it's Mick Jagger, one of the Glimmer Twins! Because that's -all- I got was a near dead cig-butt glimmer!