Help me understand the logic here

I am trying to run build in LED when pin 3 is connected to pin 2.
Pin 3 is setup to be HIGH and pin 2 is not set up to be anything so logic tells me it is LOW by default.

When i touch PIN2 with PIN3 using a jumper wire, i expect to get 2 blinks.
I get 4

I dont get it. Why does this function run twice ?

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(2, INPUT);
  pinMode(3, OUTPUT);
  digitalWrite(3, HIGH);
}

void loop() {
  if  (digitalRead(2) == HIGH) {
    myFunction();
  }
}

void myFunction(){
  digitalWrite(13, HIGH);
  delay(500);  
  digitalWrite(13, LOW); 
  delay(500); 
}

Welcome
Well done for using code tags. Not quite the right section but you can hit the flag and ask a moderator to change it for you.

A floating pin could be anything. Best not to let them float. Post a schematic. Generally a button, which is what you seem to be creating is done by setting the pin to input pull-up and then grounding it to switch

Look up debouncing also. You also should look up edge detection and state change code. Your current code will just keep running over and over if 2 is high. I’m surprised you get a standard number of blinks other than if your reflexes and the delay happen to work that way

Wire a switch as S3 (or S2) is wired in the image below.

When the switch is closed, it places a LOW on pin 2, then you read that LOW in your code.


pinMode(2, INPUT_PULUP);

. . .

  if  (digitalRead(2) == LOW) 
  {
    myFunction();
  }

1 Like

I dont know what u mean by floating pin. Do i need a varible instead of a float value, is that what you are saying ?
Either way, i dont think this will make a difference. It makes sense both ways.

Schematics of what ?
All am doing is connecting pin 2 and 3 with a jumper wire in hopes that built in led will blink 2 times as per code.
I get 3 blinks if i remove the jumper wire fast enough but normally its 4 blinks.

This is nothing to do with the hardware, i am simply lacking something in the code. I dont understand why its doing it.
To me, it makes sense that it will blink 2 times every time i jump the wires, but it blinks 3 or 4 times instead.

It has every thing to do with hardware as your pin is floating.

Place your jumper between pin 2 and GND (not 2 to 3).

A wire can produce 10s of transitions when touching a LOW/HIGH, this is referred as bounce.

Also, you should look at when pin 2 changes state, not if pin 2 is at a LOW/HIGH.

A floating pin is a pin that has not been pulled high or low so could be anything. Not a good thing if you want a reliable button. Use the internal pull-up resistor to set the pin high then ground it to pull low and low equals button pushed

see the diagram above. Pin 20, D5, is not floating. it is pulled up by that external resistor when the switch is open

pin 25 is not floating. it is pulled down to ground by that external resistor when the switch is open

most of the other pins on the right are open and could be at any voltage from ground to 5V. they are floating. if you don't make a pin go to a voltage you choose, it can wander.

if you make pin 2 your ground path and pull it to ground with INPUT_PULLDOWN you run LED current through the microscopic internal resistor. you make your Arduino a fuse. avoid using the microcontroller as a current source or sink.

i noticed that if i leave a jumper wire connected to pin 2 (but not anywhere else, literally floating in mid air) function runs.

How sensitive is this thing, a fly will set it off ?

Your input pin 2 is floating.

It is extremely sensitive if the input pin is left floating.

Static in the air can cause problems.

Wire pin 2 as S2 or S3 above.

A LOW says pin 2 is connected to GND.

It is not “sensitive” you are just using it wrong. You have not “told” the pin to be any specific voltage so it is happily wondering around being whatever voltage seems nice and your code is running whenever it’s condition is met

Ok two people tell me 2 different things.
How to set pin 2 voltage then ?
When pin is set to HIGH by the code, its 5V
What do you mean i didnt specify any voltage ?
I kinda understand that i just told it to be high but its at 5V all the time according to my multimeter.
Do i need an oscilloscope to see that it actually goes randomly LOW ? Is that what am going to discover ?

Pin 2 is set as an input. It has no voltage set. It will be whatever voltage it feels like and therefore can activate your code (when it’s voltage is ~5). It will be 5v when connected to your high output pin 3 but it will also be 5v at other random times. It is floating!

You have been given a schematic and detailed instructions. To make a button stop what you are doing. It is wrong! Change to what has been advised. Set your buttonPin to input pull-up and connect it to ground when you want to “press’ your button

If you do this your pin will always be physically pulled up to 5v by the internal resistor and will read HIGH until it is grounded when it will read LOW. Now you have a clean high low logic to detect

Please, if you won’t follow instructions give, take time out and Google Arduino floating pin.

Ok i begin to understand.
So i need to ground pin 2 in order for it to have 0V at all times or is there a way to set it to 0 with code ?
I dont want to deal with hardware right now. I am just trying to understand programming logic and some issues it has with the hardware on which it runs.

No. You need to use hardware, a resistor! There is one inside the arduino which is activated by the code input pull-up but it will change your pin to 5v not 0v. You ground the pin to low to activate your button.

If you want a HIGH for activate the button you need an external resistor to pull to ground

Pin 2 is an input pin, you do not set it HIGH/LOW in your sketch.

An output pin can be set to HIGH/LOW.

Ok that was a very helpful video.
Is there a video explaining input output pins ?
I changed my pin to output and now function does not run at all.

Google is a very useful tool.

One other thing to understand is you posted in the wrong section, so I have moved it here. Please read the description of the section you posted in and understand it is only for installation of the IDE not for your project.

So basically you chose the wrong project then.

An output is an output so it doesn’t work as a button as a button, by definition is an input.

Take it slow;
Do you understand what input pullup is?