// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if (buttonState != LASTbuttonState){
LASTbuttonState = buttonState;
if (buttonState == HIGH) {
// turn TRANSISTOR on:
digitalWrite(TransPin, HIGH);
Serial.print("HIGH");
Serial.print("\n\r");
}
if (buttonState == LOW) {
digitalWrite(TransPin, LOW);
}
}
}
I am powering from the USB cable.
No connections made to the board, except a wire ( 3 inches long ) to pin 22. Other end of the wire is not yet connected to anything.
The Serial Monitor is showing a constant non-stop stream of "HIGH" messages, but the wire in pin 22 is not connected to anything.
Reset button on the board does not change anything.
If I remove the wire from the pin 22, the Serial Monitor stops spewing out the "HIGH" messages.
Plug in the wire, and it starts again.
I have tried a number of different pins for the input, all with the same result.
an input pin with no resistor will pick up anything from passing taxis to your favourite radio station
even a high value resistor will stop this silly behaviour
any reason why you don't want to use the built-in?
Been stumped by this for 2 hours. I never thought a small piece of wire could pick up enough current to register as an input. Was starting to think that I had damaged my board.
Didn't go for the internal pull-up as the test I am doing requires a constant LOW, with the action of the code only happening if the input pin receives a HIGH signal.
Am going to be moving the cell phone a bit further away from the bed-side table from tonight !!
Haha... dude, if you have nothing connected to an I/O pin - not even a pull-up or pull-down resistor - it'll be a "floating" pin, meaning it reads literally completely random values. I mean... you could seriously initialize a random function using a floating pin as input. It's totally random when there's nothing connected. And a pushbutton has two states: open and closed. So if it's open (not pushed), it's connected to nothing... still floating. Only when it's connected (e.g. to ground), it has a definite state - 0, ground. Any other time, it floats.
That's why we use internal pull-ups on buttons and call them "active" when they return 0:
pinMode(22, INPUT); // set as input (default anyway)
digitalWrite(22, HIGH); // set pull-up resistor on port 22