Testing a sensor with sleep



  • 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


  • Mod

    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.



  • 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;
    }


  • Hardware Contributor

    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..



  • 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


  • Hardware Contributor

    in your case just after the sleep



  • 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!!


Log in to reply
 

Suggested Topics

1
Online

11.2k
Users

11.1k
Topics

112.5k
Posts