Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. push buttons / only 1 state

push buttons / only 1 state

Scheduled Pinned Locked Moved Development
5 Posts 2 Posters 1.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CrankyCoderC Offline
    CrankyCoderC Offline
    CrankyCoder
    wrote on last edited by
    #1

    Ok, so I am still very very new to this. I made what I am trying to do work, but I am curious if I just fudged it or if there is a "proper" way to go about it.

    I have a basic sensor setup. I am testing it right now by having 1 momentary push button. I got everything working all the way through to my openhab setup. However, when i push the button the state 1 is sent. Perfect. But as soon as I let go it sends 0. Now, I understand why it would. Makes total sense. But in this case, I only want something to happen when I press the button. Not when I release.

    Not sure if there is a better way to go about it, but I did this.

    if (value != oldValue) {
         // Send in the new value
         if (value==HIGH){
         gw.send(msg.set(value==HIGH ? 1 : 0));
         }
         oldValue = value;
      }
    

    I just wrapped the send with an If statement to only run the gw.send when it's high.

    Is there a better way to do this?

    Home Automation Tinkerer
    www.CrankyCoder.net

    Controller: HomeAssistant in Kubernetes
    Gateway: MQTTClientGateway
    MySensors: 2.3

    mfalkviddM 1 Reply Last reply
    0
    • CrankyCoderC CrankyCoder

      Ok, so I am still very very new to this. I made what I am trying to do work, but I am curious if I just fudged it or if there is a "proper" way to go about it.

      I have a basic sensor setup. I am testing it right now by having 1 momentary push button. I got everything working all the way through to my openhab setup. However, when i push the button the state 1 is sent. Perfect. But as soon as I let go it sends 0. Now, I understand why it would. Makes total sense. But in this case, I only want something to happen when I press the button. Not when I release.

      Not sure if there is a better way to go about it, but I did this.

      if (value != oldValue) {
           // Send in the new value
           if (value==HIGH){
           gw.send(msg.set(value==HIGH ? 1 : 0));
           }
           oldValue = value;
        }
      

      I just wrapped the send with an If statement to only run the gw.send when it's high.

      Is there a better way to do this?

      mfalkviddM Offline
      mfalkviddM Offline
      mfalkvidd
      Mod
      wrote on last edited by mfalkvidd
      #2

      @Jason-Brunk the code you have now could be reduced to

      if (value != oldValue) {
           if (value==HIGH){
               gw.send(msg.set(1));
           }
           oldValue = value;
        }
      

      So the sensor will only send high when the button is pressed, and will never send anything else. I don't think that's what you want.

      I think you want something like this:

      boolean state;
      boolean oldvalue;
      
      void loop(){
          value=digitalRead(PIN);
          if (value != oldvalue) {
              if (value == HIGH) {
                  state = ! state; // switch state every time the button is down
                  gw.send(msg.set(state));
              }
              oldvalue = value;
              gw.wait(100); // debounce
            }
      }
      

      I wrote this code on my ipad so it has not been checked for correct syntax but you should be able to get the idea.

      1 Reply Last reply
      0
      • CrankyCoderC Offline
        CrankyCoderC Offline
        CrankyCoder
        wrote on last edited by
        #3

        I do want it to only send when it's high. Just didn't know if there was a way from the framework to do that or if just modding the code in some fashion like I did was the right way to go.

        Home Automation Tinkerer
        www.CrankyCoder.net

        Controller: HomeAssistant in Kubernetes
        Gateway: MQTTClientGateway
        MySensors: 2.3

        1 Reply Last reply
        0
        • mfalkviddM Offline
          mfalkviddM Offline
          mfalkvidd
          Mod
          wrote on last edited by
          #4

          Ok, then your code is fine. I would remove the HIGH ? part and just send 1, to make the code clearer, but it will work anyway.

          1 Reply Last reply
          0
          • CrankyCoderC Offline
            CrankyCoderC Offline
            CrankyCoder
            wrote on last edited by
            #5

            Thank you. New to mysensors still so I didn't know if there was a "framework" method :)

            thanks again!

            Home Automation Tinkerer
            www.CrankyCoder.net

            Controller: HomeAssistant in Kubernetes
            Gateway: MQTTClientGateway
            MySensors: 2.3

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            16

            Online

            11.7k

            Users

            11.2k

            Topics

            113.1k

            Posts


            Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • MySensors
            • OpenHardware.io
            • Categories
            • Recent
            • Tags
            • Popular