Odd digital pin reading

Hello,

I have a piece of plastic, resting at 10 degrees off vertical, that has 2 holes drilled in it.

I have a patch wire attached to ground on an Arduino YUN, with the other end poking through one hole on the plastic board.

I have another patch wire attached to digital pin 2 (configured in the sketch as INPUT_PULLUP) on the Arduino, the other end of which is poking through the other hole on the plastic board.

The distance between the pins is a few mil less than the diameter of a penny coin.

The following code sketch is running on the Arduino:

#define START_PIN 2
#define MAX_PINS 1

void setup() {
Serial.begin(9600);
for ( int pin = START_PIN; pin < START_PIN + MAX_PINS; pin++) {
pinMode( pin, INPUT_PULLUP);
}
}

void loop() {
for ( int pin = START_PIN; pin < START_PIN + MAX_PINS; pin++)
{
Serial.print( getState( pin));
Serial.print("\t");
}
Serial.println();
delay(1000); // delay in between reads for stability
}

char* getState( int pin)
{
if ( digitalRead( pin) == 0)
return "HERE";
else
return "gone";
}

I place a penny on the board resting on the 2 protruding patch wires, the output is “HERE”, I lift the coin up a little and “gone” is output.

All good … but if I flick the coin up to open the ‘switch’ and let it drop back onto the patch wires again, sometimes I will receive “gone” from all subsequent outputs on the serial monitor.
This state will continue until I move the coin off and then on the wires.

Could anyone tell me why this is happening and whether there is a possible fix?

Andrew

What is the purpose of the FOR loop if you have only one pin to test?

It sounds to me as if the coin just is not making a good contact with your wires - have you tried cleaning the coin with fine sandpaper?

...R

The real project will work with 8 coins so the for loop will iterate over each pin. I will try cleaning the coins, thanks for your advice.