Touch buttons - booleans going crazy

Hello,

I've made touch buttons out of copper tape and 1M pullup resistors connected to my Arduino UNO. One half of the sensor goes to the pin and to 5V via a 1M resistor. The other half goes to ground. When you lay your finger across the two, they act like a button and the pin reads low. Hooray.

However, I'm getting some strange behaviour. I'm trying to get the button to return a boolean of true if it's active via a function like this:

bool isItPressed(int pin) {
if (pin == LOW) {
return true;
} else {
return false;
}
}

void loop() {
int buttonPin = 5;
int buttonPinState = digitalRead(buttonPin);
if (buttonPin == LOW) {
Serial.println("I AM ON!");
} else {
Serial.println("I AM OFF!");
}
}

This code is supposed to print that line ONCE, no matter how long I press the button, and return back to false when I let go.

If I use an old-fashioned push button with this code it works perfectly - just registers one push. However, when I use the touch button the bool is false until I press the button with my finger, at which point the bool goes ON OFF ON OFF ON OFF until I release it. I can't figure out why it keeps flipping the bool value back and forth if I'm holding my finger on it (all connections are really secure and there's no funny business - I've checked all connections with a meter). What could be making the pin read alternating high and low values repeatedly until I let go?

Has anyone experienced this? Is it the resistor value? Some kind of charge across the skin? I'm kind of stumped, and I want reliable performance so I'm very curious about what the problem is.

What voltage is your finger attached to? Since you have such a high resistance connecting that pad to 5v, your finger is practically just acting like an antenna for RF. The human body typically emits something like 50hz as we're surrounded by mains voltage AC cables.

Try holding on the the GND with the other hand when you touch it. Likely as not it still won't help a lot as your body has such a high impedance.

You cannot be testing with this code because it does not work as you expect.

int buttonPin = 5;
int buttonPinState = digitalRead(buttonPin);
if (buttonPin == LOW) {

the statement "buttonPin == LOW" is comparing 5 to LOW, which is never equal.

Where is your setup function that sets the serial port? Why did you declare function "isItPressed(int pin)" and then never called it? Kindly please get in the habit of indenting ... as you write more difficult code, it's going to kill you trying to determine what's inside which brackets. The setup function looks something like this:

void setup() {
// put all setup code here that runs once:
pinMode (IN11,INPUT_PULLUP);
pinMode (OUT9,OUTPUT);
Serial.begin(9600);
}

You need to set the pinMode if you expect the output to turn on.

The code will only print it once if you write it that way. The loop function repeates many many times a second and will print out every time round unless you write your code correctly and look for a state change and not just a state.

There's an example in 02.Digital section of the examples called "StateChangeDetection" - start there...