"Button" works but "Date and Time" doesn't?


#1

Hi All!

I must admit, I’m a little stumped. I’m trying to set up an IFTTT recipe to trigger a servo on a “button” push, but it only seems to work every second “button” push.

I’ve attached the recipe in case I’m doing something wrong in there.

I’m sure there’s something wrong in the code, but it’s driving me mad. Can you help?

Cheers

Tim

// ** NOTES
    // ** posStatus1 -> Reed switch for openServo -> HIGH=DISENGAGED, LOW=ENGAGED
    // ** posStatus2 -> Reed switch for twistServo -> HIGH=DISENGAGED, LOW=ENGAGED
    // ** openServo -> Operates string that moves blinds apart -> HIGH=CLOSED
    // ** twistServo -> Operates beaded cord that turns blinds -> HIGH=FLUSH
    // ** 
    // ** 


#define TWIST_TIME 2000



int servoPin2 = D1; // ToggleTwist servo
Servo twistServo;

int buttonPin1 = D2; // ToggleTwist servo

int posDetector2 = D3; // ToggleTwist switch input

int led1 = D7; //indicator



int servoPos = 90; // Reset servo position

int posStatus2 = 0; // Position status variable ToggleTwist
    


volatile bool doTwist1 = false;
volatile bool doTwist2 = false;



void setup() {
    
        pinMode(buttonPin1, INPUT_PULLUP);
           
        pinMode(led1, OUTPUT);   
        
        twistServo.write(90);
     
        twistServo.attach( servoPin2 ); // Attach Twist servo to D1
        
        Particle.function("toggleTwist", toggleTwist);
        
        Particle.function("commandTwist", commandTwist);
            
        pinMode(posDetector2, INPUT_PULLUP); // Check status of ToggleTwist switch
            
        Particle.variable("posStatus2", posStatus2); // Declare status variable to check ToggleTwist position status
        
        digitalWrite(led1, LOW);   
            
}

    
    
    
    
void loop () {
    
        bool buttonState = !digitalRead(buttonPin1);

        if (doTwist1 == true || buttonState == HIGH) { // ToggleTwist blinds open/closed
         
           
            posStatus2 = digitalRead(posDetector2); // Status of ToggleTwist switch
               
                
					if (posStatus2 == LOW) {
					    
					    // If FLUSH TO WINDOW
					    digitalWrite(led1,HIGH);
						twistServo.write( 100 ); 
						delay(TWIST_TIME); 
						twistServo.write( 90 );
						digitalWrite(led1,LOW);
						delay(1000);
						digitalWrite(led1,HIGH);
						twistServo.write( 100 ); 
						delay(TWIST_TIME); 
						twistServo.write( 90 );
						digitalWrite(led1,LOW);
						
						doTwist1 = false; 
						
					}

                    else { 
					
						doTwist1 = false; 
					
					}
				
		}


        else if (doTwist2 == true) { // ToggleTwist blinds open/closed
         
           
            posStatus2 = digitalRead(posDetector2); // Status of ToggleTwist switch
               
                
					if (posStatus2 == HIGH) {
					    
					    // If FLUSH TO WINDOW
						twistServo.write( 80 ); 
						delay(TWIST_TIME); 
						twistServo.write( 90 );
					    digitalWrite(led1,HIGH);
						digitalWrite(led1,LOW);
						delay(500);
						digitalWrite(led1,HIGH);
						delay(500);
						digitalWrite(led1,LOW);
						delay(500);
						digitalWrite(led1,HIGH);
						delay(500);
						digitalWrite(led1,LOW);
						twistServo.write( 100 ); 
						delay(TWIST_TIME); 
						twistServo.write( 90 );
						
						doTwist2 = false; 
						
					}

                    else { 
					
						doTwist2 = false; 
					
					}
				
		}

		else {
		    
		}
			
			
           
}  
    
    
    
int toggleTwist(String command) {
    
	if (posStatus2 == LOW) {
	    
		doTwist1 = true;
		
		}
	
	else { 
	    
		doTwist2 = true;
		
		}
}


int commandTwist(String command) {
	
	if (posStatus2 == LOW) {
	    
		doTwist1 = true;
		
		}
	
	else { 
	    
		doTwist2 = true;
		
		}
}

#2

Did you try calling the function from another mean than IFTTT? Like using cli for example.


#3

Hi Fabien, I wouldn’t know where to start, sorry. I’m still really new at this and IFTTT is pushing my abilities as it is. :smile:


#4

You can see how to install cli here: https://docs.particle.io/guide/tools-and-features/cli/photon/
The list of cli command is here: https://docs.particle.io/reference/cli/ You will be interrested in the call command.


#5

What do you mean by second push? Did the “pushing” trigger the function on the Photon?


#6

Hi Kenneth, only ever other time. It does work, and then it doesn’t, then it does again… it’s so weird that I’m struggling to describe it.


#7

Can you try something simpler like calling a function to toggle an LED?


#8

I have and that works fine so I know it’s something in my code that it doesn’t like.


#9

I’ll simplify the problem to setting a flag like this for testing first:

boolean twist = false;

void loop() {
  if(twist) {
  twist = false;
  //code to turn the servo
  }
}
int toggleTwist(String command) {
   twist = true;
   return 1;
}