Simple switch issue

Hi!

I’m just trying a simple program to get me started but I don’t understand what my Particle is doing.

I’ve connected a switch between D2 and GND with an IF statement dependent on it’s state. The whole thing should run on a button-push on IFTTT but I get the same response (ON, OFF, ON, OFF) irrespective of whether the switch is engaged or not.

I’ve removed the switch and just clamped the wires together (in case it was a fault with the switch) and it’s made no difference.

Please see the code below, if you’re able to help I’d really appreciate it. I know it’s probably a dead simple one, it’s just to get me started on a bigger project.

Thanks!

volatile bool doToggle = false;

int led1 = D7;

int posDetector1 = D2; 

int posStatus1 = LOW; 

void setup() {

    pinMode(led1, OUTPUT);
    Particle.function("toggleBlink", toggleBlink);
    pinMode(posDetector1, INPUT_PULLDOWN);
    Particle.variable("posStatus1", &posStatus1, INT); 

}

void loop() {
    
    if (doToggle == true) { 
  
        posStatus1 = digitalRead(posDetector1);

            if (posStatus1 == LOW) {
  
                digitalWrite(led1, HIGH);
                delay(300);
                digitalWrite(led1, LOW);
                delay(300);
                digitalWrite(led1, HIGH);
                delay(300);
                digitalWrite(led1, LOW);  
                delay(300);
                digitalWrite(led1, HIGH);  
                delay(300);
                digitalWrite(led1, LOW);
                delay(300);
                digitalWrite(led1, HIGH);  
                delay(300);
                digitalWrite(led1, LOW);

                doToggle = false;
	
            }
	
            else {
                
                digitalWrite(led1, HIGH);
                delay(3000);
                digitalWrite(led1, LOW); 
                doToggle = false;
				
            }   
    }
            
}


int toggleBlink(String command) {
    doToggle = true;
}
pinMode(posDetector1, INPUT_PULLDOWN);

If you want to detect a switch closing to GND you cannot use INPUT_PULLDOWN since with that the signal will be LOW with or without your switch closed.
You need to use INPUT_PULLUP.

And as a side note

Particle.variable("posStatus1", &posStatus1, INT); 

this form is deprecated. Just use the modern version

Particle.variable("posStatus1", posStatus1);

And for nit picking: Since you do doToggle = false; regardless of the switch state, take that bit out of the inner condition blocks and write it only once.

3 Likes

Thanks so much ScruffR.

For your last line, do you mean that, because I have:

volatile bool doToggle = false;

in the top section, that I don’t need:

doToggle = false

in the “if” statements in the loop or vice versa?

Nope, I mean this


void loop() {
  if (doToggle == true) { 
    posStatus1 = digitalRead(posDetector1);
    if (posStatus1 == LOW) {
      ...  
      doToggle = false;  // superfluous due to else branch
    }
    else {
      ...
      doToggle = false;  // superfluous due to if branch
    }   

    //doToggle = false;  // this would be the logical position

  }
}

You have the same instruction in both branches, so it will be done irrespective of posStatus1 and hence it should not be inside the conditional blocks but outside of them.

1 Like

Aah! I get you! Thanks!