Sensor sleep causing problems



  • I'm using the Temp Sensor Example to build a freezer alarm. It has a beeper, and a button on it to silence the beeper.

    The code to make it beep is:

    digitalWrite(BUZZERPIN, HIGH);
    delay(500);
    digitalWrite(BUZZERPIN, LOW);
    delay(500);
    

    But, this temp sensor sketch has a built in delay of 30 seconds between reads. Which means that I get a couple of beeps, and then silence for 30 seconds. I also have a button that needs to be read, so if it's pressed, it sets a variable that silences the alarm. I'm pretty sure that cannot be read during sleep.

    'm guessing the answer is no, but is there a way to fork a thread that handles the buzzer, and another that looks for the button press? What is the solution to this? Do I just set the sleep to 1 second for the temp read and then just note that I have to hold the button until the button read happens and the buzzer stops?


  • Mod

    In case of alarm you can programmatically exclude the sleep so the loop runs continuously until temperature gets back to normal or button pressed, or use millis to count time according to the needs.

    PS Take a look at this and following article for inspiration


  • Hero Member

    @signal15 You can achieve what you want but you will need to do as @gohan has indicated.
    When the Temperature is within normal limits use the normal code with sleep to save power.
    When a temperature outside the desired range is detected then bypass the normal temp code and run the different code needed to execute the beep etc.

    You will need to use non blocking delays for the beep and for the time between temp readings an example here


  • Hardware Contributor

    Hello,

    I would do something much more simple

    At the beginning of the sketch :

    bool buzzerState = false;
    

    Then at the end of your sketch where you currently have the sleep() :

    if (AlarmModeIsActive) {
    buzzerState = not(buzzerState);  // invert state of buzzer
    digitalWrite(BUZZERPIN, buzzerState);
    sleep(BUZZER_PIN, CHANGE, 2000);  // Sleep but put interrupt on the button so we don't have to wait for a reaction when pushing the button
    }
    else {
      // here do normal sleep for your node
     sleep(30000);
    }
    

    Of course don't forget to reset buzzer state and set buzzer pin to low when button is pressed.


Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.