Loop Switch Normally Closed

Hello! I'm trying to use a loop switch to simulate a normally closed switch. I essentially want my loop switch to activate my LED on Port 2 when I separate the contacts, which is plugged into port 4.

Thank you for your help!

void setup() {
// put your setup code here, to run once:
pinMode(2, OUTPUT);
pinMode(4, INPUT);
}

void loop() {
// put your main code here, to run repeatedly:

if(digitalRead(4)==LOW){
digitalWrite(2, HIGH);}

if(digitalRead(4)==HIGH){
digitalWrite(2, LOW);
}

if(digitalRead(4)== HIGH)
digitalWrite(2, HIGH);

}

Instead of connecting the loop switch between +5 and ground, which leaves the input "floating" and thus in an unpredictable state, I suggest connecting one end to ground, the other end to the input, and using INPUT_PULLUP in the pin setup to enable the internal pull up resistor. The loop input will indicate HIGH when it's broken, LOW normally.

Why does your sketch repeat exactly the same code three times?

if(digitalRead(4)==LOW){
digitalWrite(2, HIGH);}

if(digitalRead(4)==HIGH){
digitalWrite(2, LOW);
}

if(digitalRead(4)== HIGH)
digitalWrite(2, HIGH);
}

Sorry this makes no sense
The first condition is ok, the second is ok, but it could be } else {

The third if() is pointless because it defeats the second…

Hello
Post a schematic and your sketch well formated and commented in code tags "</>" as provided in this editor.

I've worked in electronics for 60 years and dont know what a loop switch is - can you enlighten me please?

1 Like

Yeah! :worried:

Sorry for low-res image. It’s an improvised switch created by creating a loop of wire contacts around the insulation of another. When the wires are pulled they complete a circuit and provide power.

My version is a bit simpler. I have two contacts from my wires stripped down and bent into “J” hooks and contacting each other. I want my circuit to supply power once I pull the wires apart and they are no longer in contact.

My apologies! I was actually in a rush in posting this as I was heading out the door. I had been finagling with the code and ended up pasting a few too many times. Thank you for the tip! I’ll give it a try later!

My apologies. I will work on this tomorrow.

void setup() {
pinMode(2, OUTPUT);
pinMode(4, INPUT_PULLUP);
}

void loop() {
digitalWrite(2, digitalRead(4) );
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.