Announcement

Collapse
No announcement yet.

Trigger event on release of Arduino input

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trigger event on release of Arduino input

    I use the Arduino plugin to control Hue lights with one button. Single push = toggle presets, Push 1sec = off.
    Problem is that the 1sec push also triggers the next scene.
    This would be solved if the event triggers on release of the contact.

    Maybe I'm missing something, but is it possible to trigger an event on release of a contact input on the Arduino board?

    Thanks in advance!

  • #2
    Is this 'one button' a physical button connected to the Arduino or an HSTouch button? I think it could be done either way, but would be handled differently.

    Mike

    Comment


    • #3
      Originally posted by WAVI View Post
      I use the Arduino plugin to control Hue lights with one button. Single push = toggle presets, Push 1sec = off.
      Problem is that the 1sec push also triggers the next scene.
      This would be solved if the event triggers on release of the contact.

      Maybe I'm missing something, but is it possible to trigger an event on release of a contact input on the Arduino board?

      Thanks in advance!
      This is going to be a tough one. The button released trigger would be:

      IF the Arduino Pin changes and becomes Open

      The problem is that would be true whether the button was pressed and released immediately or held for 1 second then released.

      You probably have to use a Timer and the timing would be difficult. Here is an idea to build on, it may or may not work. It will be dependent upon how past your Arduino responds to input state change. This can be affected by noise on the button wiring and debounce settings on the board. It also may be affected by your HS server hardware - if it is a fast system it may be reliable, but on a slower system like a Pi based controller, the timing may be too critical to be reliable. I tested these events on a little NodeMCU on my system and they worked most of the time. I didn't have a button, I was just plugging in a jumper. It may work better with a button and a button will work better with a noise suppression capacitor at the pin on the Arduino. On each Event I set cannot rerun for 2 seconds to keep them from running again too quickly.

      Click image for larger version  Name:	Capture.PNG Views:	1 Size:	102.8 KB ID:	1273115

      Randy Prade
      Aurora, CO
      Prades.net

      PHLocation - Pushover - EasyTrigger - UltraECM3 - Ultra1Wire3 - Arduino

      Comment


      • #4
        Assuming you are using a physical pushbutton switch, to avoid the possible delays and latency between the Arduino and the Homeseer plugin, you could do all of the timing in the Arduino sketch. This would require using an API board in the plugin. I came up with a couple of variations that might lead you somewhere. These are based on the Uno, using pin 2 as input for the pushbutton and pins 3, 4, and 5 for outputs (LEDs in my tests). You could change the digitalWrite to SendToHS() to integrate with Homeseer.

        The first example uses press-to-toggle, and press-and-hold for off. The second example is a slight variation. It counts clicks; 1-click turns on output 3, 2-clicks output 4, and 3-clicks output 5. The clicks must happen within the 'period' that is defined (1 second in this example). Click and hold for at least one second to turn them off. One side effect here is that to count clicks, the action won't execute until the 'period' timer has elapsed.

        Example 1 - Toggle


        Code:
        int pinState=1;
        int lastPinState=1;
        int pinCount=0;
        unsigned long startHoldMillis;
        unsigned long startMillis;
        unsigned long currentMillis;
        const unsigned long period = 1000;  // the value is a number of milliseconds
        
        void setup() {
          pinMode(2,INPUT_PULLUP);
          pinMode(3,OUTPUT);
          pinMode(4,OUTPUT);
          pinMode(5,OUTPUT);
        
          startMillis = millis();  //initial start time
        }
        
        void loop() {
        
          pinState = digitalRead(2); // pushbutton
          currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
        
          // check to see if the input button has been pressed or released
          if (pinState != lastPinState) {
            startMillis = currentMillis;  // save the start time of the current pin state.
        
            // save the start time of the current pushbutton release
            if (lastPinState == 1) {
              startHoldMillis = currentMillis;
            }
        
            // toggle outputs on button releases
            if (currentMillis - startMillis < period  &&  currentMillis - startHoldMillis < period) {
                if (lastPinState == 0) {
                   if ((digitalRead(3) == LOW && digitalRead(4) == LOW && digitalRead(5) == LOW) || digitalRead(5) == HIGH) {
                     digitalWrite(3,HIGH);
                     digitalWrite(4,LOW);
                     digitalWrite(5,LOW);
                   }
                   else if (digitalRead(3) == HIGH) {
                     digitalWrite(3,LOW);
                     digitalWrite(4,HIGH);
                     digitalWrite(5,LOW);
                   }
                   else if (digitalRead(4) == HIGH) {
                     digitalWrite(3,LOW);
                     digitalWrite(4,LOW);
                     digitalWrite(5,HIGH);
                   }
                  }
            }
            lastPinState = pinState;
          }
        
          // after period has elapsed
          if (currentMillis - startMillis >= period) {
            // check for pushbutton release after being held for Hold period
            if (lastPinState == 0) {
              digitalWrite(3,LOW);
              digitalWrite(4,LOW);
              digitalWrite(5,LOW);
            }
          }
          delay(30); // debounce
        }

        Example 2 - Quick click counter

        Code:
        int pinState=1;
        int lastPinState=1;
        int pinCount=0;
        unsigned long startHoldMillis;
        unsigned long startMillis;
        unsigned long currentMillis;
        const unsigned long period = 1000;  // the value is a number of milliseconds
        
        void setup() {
          pinMode(2,INPUT_PULLUP);
          pinMode(3,OUTPUT);
          pinMode(4,OUTPUT);
          pinMode(5,OUTPUT);
        
          startMillis = millis();  //initial start time
        }
        
        void loop() {
        
          pinState = digitalRead(2); // pushbutton
          currentMillis = millis();  // get the current "time" (actually the number of milliseconds since the program started)
        
          // check to see if the input button has been pressed or released
          if (pinState != lastPinState) {
                  startMillis = currentMillis;  // save the start time of the current pin state.
        
            // save the start time of the current pushbutton release
            if (lastPinState == 1) {
              startHoldMillis = currentMillis;
            }
        
            // count quick button pushes the occur within period
            if (currentMillis - startMillis < period  &&  currentMillis - startHoldMillis < period) {
                if (lastPinState == 0) {
                  pinCount = pinCount + 1;
                }
            }
              lastPinState = pinState;
          }
        
          // after period has elapsed
          if (currentMillis - startMillis >= period) {
            if (pinCount == 0) {
              // check for pushbutton release after being held for period
              if (lastPinState == 0) {
                digitalWrite(3,LOW);
                digitalWrite(4,LOW);
                digitalWrite(5,LOW);
              }
            }
            else if (pinCount == 1) {
                 digitalWrite(3,HIGH);
                 digitalWrite(4,LOW);
                 digitalWrite(5,LOW);
            }
            else if (pinCount == 2) {
                 digitalWrite(3,LOW);
                 digitalWrite(4,HIGH);
                 digitalWrite(5,LOW);
             }
            else if (pinCount == 3) {
                 digitalWrite(3,LOW);
                 digitalWrite(4,LOW);
                 digitalWrite(5,HIGH);
             }
            pinCount = 0; // reset button pushes counter
          }
          delay(30); // debounce
        }

        Comment


        • #5
          Originally posted by rprade View Post
          This is going to be a tough one. The button released trigger would be:

          IF the Arduino Pin changes and becomes Open

          The problem is that would be true whether the button was pressed and released immediately or held for 1 second then released.

          You probably have to use a Timer and the timing would be difficult. Here is an idea to build on, it may or may not work. It will be dependent upon how past your Arduino responds to input state change. This can be affected by noise on the button wiring and debounce settings on the board. It also may be affected by your HS server hardware - if it is a fast system it may be reliable, but on a slower system like a Pi based controller, the timing may be too critical to be reliable. I tested these events on a little NodeMCU on my system and they worked most of the time. I didn't have a button, I was just plugging in a jumper. It may work better with a button and a button will work better with a noise suppression capacitor at the pin on the Arduino. On each Event I set cannot rerun for 2 seconds to keep them from running again too quickly.


          I have been testing these settings for an hour now, and it didn't miss one action. Seems this is a workable solution!
          To make sure the light turns on with the same brightness as it was turned off I created two identical events. The first checks if the light status is off, and than turns in on. The second checks if the light status is not-off, and than sends the UP command to change the brightness of the lamp. (of course this command can be replaced by a jowihue scene or event)

          Click image for larger version

Name:	image_75151.png
Views:	11
Size:	137.7 KB
ID:	1273588

          To turn the light off there are 2 possibilities:
          By releasing the button after it is pressed for more than 1 second, or
          by pressing the button for more than 1 second.
          Both seem to work fine.

          Click image for larger version  Name:	HUE2.png Views:	1 Size:	229.1 KB ID:	1273589
          Thank you very much for your quick answer! I will continue testing, if it proves to be stable all light switches in my house will be connected to the Arduino board. Thanks again!!

          Comment


          • #6
            I'm happy it is working for you.
            Randy Prade
            Aurora, CO
            Prades.net

            PHLocation - Pushover - EasyTrigger - UltraECM3 - Ultra1Wire3 - Arduino

            Comment

            Working...
            X