How to turn off the widget "virtually"


#1

hi. cayenne community. hope someone can help me. i want a device to turn on by physically pressing the widget button but i want it to turn off automatically after 5 seconds without having to press it again. In my sketch, I used a delay of 5000 then used digitalwrite to turn it off. it worked in that the device turned off but still the widget is set to on. How can i modify my sketch so that after 5 seconds, the devices turns off and the widget button at the same time. here is a portion of my sketch. Hope u guys can help me.

CAYENNE_IN(3)
{
int value = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL_MEDIUMCOF, ACTUATOR_PIN_MEDIUMCOF, value);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_MEDIUMCOF, value);
delay (5000);
digitalWrite(ACTUATOR_PIN_MEDIUMCOF, LOW);
}


#2

you can try something like this:

unsigned long lastMillis = 0;
void loop() {
   Cayenne.loop();
   
   if (x == 1){
    if(millis() - lastMillis > 5000) {
      Cayenne.virtualWrite(3, 0);
      digitalWrite(ACTUATOR_PIN_MEDIUMCOF, LOW);
     x = 0;
   }
 }
}

CAYENNE_IN(3)
{
int value = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL_MEDIUMCOF, ACTUATOR_PIN_MEDIUMCOF, value);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_MEDIUMCOF, value);
lastMillis = millis();
x = 1;

}

#3

Hi Shrami. It says x was not declared in the scope.


#4

then you need to add int x; which i forgot earlier :grin:


#5

thanks buddy as always. it worked!