[SOLVED]Microswitch detection issue

I have a microswitch connected with Common on 3V3 pin and NC on D2. Here is the code I’m using:

void setup() {
    pinMode(D7, OUTPUT);
    pinMode(D2, INPUT);
}

static unsigned char switchState;

void loop() {
    switchState = digitalRead(D2);
        if(switchState == HIGH)
            {
                digitalWrite(D7,HIGH);
                Particle.publish("Door", "Open");
            }
        else if(switchState == LOW)
            {
                digitalWrite(D7,LOW);
                Particle.publish("Door", "Closed");
            }
}

The problem is when I push the switch (which will open it) it should read D2 as being LOW and set D7 LED to LOW. It’s not. The D7 LED remains HIGH regardless of the switch, and the publish method does not fire either.

I do notice that when I close the switch, the D7 LED dims just a bit -maybe just a power thing…

Any ideas?

May or may not have something to do with he issue you're facing, but you should take a look at this regardless:

NOTE: Currently, a device can publish at rate of about 1 event/sec, with bursts of up to 4 allowed in 1 second. Back to back burst of 4 messages will take 4 seconds to recover.

Ah, that could be the problem as I see in the logs that it fires off 4 “open” statuses before stopping and not responding. I need to adjust my code to only respond once if it detects the switch open, or once if closed.

Ideally, I’d like to just monitor the switch status and publis that event one time.

You could take a look at this: https://docs.particle.io/reference/firmware/photon/#interrupts

While your NC prevents the pin from floating while not actuated, it might float when keeping it pressed for a while, so you should go for pinMode(D2, INPUT_PULLDOWN); too.

To prevent multi-firing when polling, you'd need to keep track of the previous state and only act on a change.

1 Like

Yeah - without a pulldown your results will be unpredictable.

You should also debounce the input, or else you may see some spurious results (although the publish() in yoour example might take so long that the bouncing is done by the time loop() iterates.) Debouncing is good practice whenever set of physical contacts is invoved regardless.

1 Like

Thanks for all of your help guys! @AndyW and @ScruffR - a combination of both of your replies got me where I want to be.

bool doorOpen = false;
bool previousState = false;

void setup() {
    pinMode(D7, OUTPUT);
    pinMode(D2, INPUT_PULLDOWN);
}

static unsigned char switchState;

void loop() {
    switchState = digitalRead(D2);
    if (switchState == HIGH)
        {
            doorOpen = true;
        } else if (switchState == LOW) {
            doorOpen = false;
        }
        
    if(doorOpen == true && previousState == false)
        {
            digitalWrite(D7,HIGH);
                
            Particle.publish("Door", "Open");
                
            doorOpen = false;
            previousState = true;
        }
        else if(doorOpen == false && previousState == true)
        {
            digitalWrite(D7,LOW);
                
            Particle.publish("Mailbox", "Closed");
                
            doorOpen = true;
            previousState = false;
        } else {
                //nothing to do
        }
}

You guys rock!

3 Likes

Welcome - glad you’re up and running.

2 Likes