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. Testing a sensor with sleep

Testing a sensor with sleep

Scheduled Pinned Locked Moved Development
megasleep
7 Posts 3 Posters 2.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

    So I have a test sensor I have been working with. It's not my final build for this as I will eventually put it on an arduino pro mini. But for now I have an atmega I am testing with. It connects via the RF no problem. I have the code putting it to sleep. When I press the button it's waking back up and running through the loop again. However, even though it's running through the loop, it's not seeing that the button is/was pressed.

    Here is my code

    void loop() 
    {
      
      uint8_t value;
      static uint8_t sentValue=2;
      gw.sleep(200);
      //debouncer.update();
      // Get the update value
      //int value = debouncer.read();
      
      value = digitalRead(3);
      
      //int value = digitalRead(BUTTON_PIN);
      Serial.write(value + "*****");
      if (value != oldValue) {
         // Send in the new value
         if (value==HIGH){
         gw.send(msg.set(value==HIGH ? 1 : 0));
         }
         oldValue = value;
      }
      Serial.write("alive\n");
      gw.sleep(BUTTON_PIN-2, LOW, 0);
    } 
    

    So each time I press the button I see the word "alive" printed on the console. But I also see, "*****" with no value in front of it. I should see the state of the button I would think.

    Atleast that's what I am expecting.

    Not sure if there is something I am missing here or if it's related to the atmega I am using.

    Thanks! Sorry for being such a noob here lol

    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 mfalkvidd
      #2

      I don't think adding a number and a string works in C/C++. Use
      Serial.print(value);
      Serial.print("*****");
      instead.

      You might want to place gw.sleep(200) just before the gw.sleep at the end as well, otherwise you might miss the press if the button is released too fast.

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

        Wouldn't this part still execute even if my serial.write is incorrect?

        @Jason-Brunk said:

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

        Home Automation Tinkerer
        www.CrankyCoder.net

        Controller: HomeAssistant in Kubernetes
        Gateway: MQTTClientGateway
        MySensors: 2.3

        1 Reply Last reply
        0
        • scalzS Offline
          scalzS Offline
          scalz
          Hardware Contributor
          wrote on last edited by scalz
          #4

          hello.

          you can't concatenate variable with string like you're doing. that can give unpredicatble results. unfortunately not as simple as you could do in other language. it's bit more low level here so you have to use C/C++ strings function or your own for this.

          Another thing :

          • if you write : serial.write(20), you won't see "20" in Serial monitor, but instead you will see the corresponding char in ascii chart.
          • if you want to see "20" in serial monitor, you should write serial.write("20")
          • another way coud be serial.print(20), or serial.print("20"), or a variable like x=20 and then serial.print(x)

          As you can see, if you want to see things in your serial monitor for debug, it's easier to use. serial.print or serial.println (println add a new line at the end). write function can be used for other tasks.

          So here in your example, you would better write :
          Serial.print(value);
          Serial.println("");
          You can use Serial.println(F("
          ***"));
          for saving ram memory as it would store your string in flash instead of ram. you have more flash than ram, sometimes can be useful. but if it's for a few ponctual debug no need of this of course

          for more details, you can have a look to Arduino reference, or C/C++ reference too.
          I hope this helps a bit :)

          Edit: @Jason-Brunk you should look this link about the basics of mysensors lib https://www.mysensors.org/download/sensor_api_15 You will see the right parameters for the sleep function. you would prefer use FALLING (instead of LOW), and then use a variable like x = sleep(.... Then x will tell you why it has woken up and then you can test it to send or not..

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

            Thanks @scalz

            I tried the sleep function with CHANGE and with HIGH. My mega never woke up. Only with HIGH.

            I am actually testing with a variable on the gw.sleep now.

            I am thinking I am missing something with the interrupts. When the interrupt is fired, does it restart the entire MCU? or does it pick up right after the sleep?

            That might help me get where I need

            Home Automation Tinkerer
            www.CrankyCoder.net

            Controller: HomeAssistant in Kubernetes
            Gateway: MQTTClientGateway
            MySensors: 2.3

            1 Reply Last reply
            0
            • scalzS Offline
              scalzS Offline
              scalz
              Hardware Contributor
              wrote on last edited by
              #6

              in your case just after the sleep

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

                Ok I got it to work. I misunderstood how the interrupts worked. Got it now. The culprit was the if statement detecting if the state changed. Since the state was being maintained while sleep, it wasn't firing when I pushed the button.

                Thanks for everyones help!!

                Home Automation Tinkerer
                www.CrankyCoder.net

                Controller: HomeAssistant in Kubernetes
                Gateway: MQTTClientGateway
                MySensors: 2.3

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


                17

                Online

                11.7k

                Users

                11.2k

                Topics

                113.0k

                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