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. Troubleshooting
  3. example of using sleep

example of using sleep

Scheduled Pinned Locked Moved Troubleshooting
5 Posts 2 Posters 541 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.
  • markjgabbM Offline
    markjgabbM Offline
    markjgabb
    wrote on last edited by
    #1

    Hi all

    I know this has probably been addressed before but i cant seem to find any examples of eactly how sleep should be used in this situation

    my snippet is as below

    sleep(int BUTTON_PIN, int CHANGE, unsigned long ms=0);

    im guessing i have my syntax wrong, but cant find any good examples

    i keep getting the following error

    expected primary-expression before 'int'

    basicly i just want the node to sleep forever untill the button is pressed... i know ive got something wrong, but i cant find any examples in the forum that use this verity of sleep

    Y 1 Reply Last reply
    0
    • markjgabbM markjgabb

      Hi all

      I know this has probably been addressed before but i cant seem to find any examples of eactly how sleep should be used in this situation

      my snippet is as below

      sleep(int BUTTON_PIN, int CHANGE, unsigned long ms=0);

      im guessing i have my syntax wrong, but cant find any good examples

      i keep getting the following error

      expected primary-expression before 'int'

      basicly i just want the node to sleep forever untill the button is pressed... i know ive got something wrong, but i cant find any examples in the forum that use this verity of sleep

      Y Offline
      Y Offline
      Yveaux
      Mod
      wrote on last edited by
      #2

      @markjgabb please post your full code.

      http://yveaux.blogspot.nl

      1 Reply Last reply
      0
      • markjgabbM Offline
        markjgabbM Offline
        markjgabb
        wrote on last edited by
        #3

        hey @Yveaux

        // Enable debug prints to serial monitor
        #define MY_DEBUG 
        
        // Enable and select radio type attached
        #define MY_RADIO_RF24
        //#define MY_RADIO_RFM69
        
        #include <MySensors.h>
        #include <Bounce2.h>
        
        #define CHILD_ID 1
        #define BUTTON_PIN  3  // Arduino Digital I/O pin for button/reed switch
        
        Bounce debouncer = Bounce(); 
        int oldValue=-1;
        
        // Change to V_LIGHT if you use S_LIGHT in presentation below
        MyMessage msg(CHILD_ID,V_LIGHT);
        
        
        //=========================
        // BATTERY VOLTAGE DIVIDER SETUP
        // 1M, 470K divider across battery and using internal ADC ref of 1.1V
        // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
        // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
        // 3.44/1023 = Volts per bit = 0.003363075
        #define VBAT_PER_BITS 0.003363075  
        #define VMIN 1.9                                  //  Vmin (radio Min Volt)=1.9V (564v)
        #define VMAX 3.0                                  //  Vmax = (2xAA bat)=3.0V (892v)
        int batteryPcnt = 0;                              // Calc value for battery %
        int batLoop = 0;                                  // Loop to help calc average
        int batArray[3];                                  // Array to store value for average calc.
        int BATTERY_SENSE_PIN = A0;                       // select the input pin for the battery sense point
        //=========================
        
        void setup()  
        {  
          // Setup the button
          pinMode(BUTTON_PIN,INPUT);
          // Activate internal pull-up
          digitalWrite(BUTTON_PIN,HIGH);
        
          // After setting up the button, setup debouncer
          debouncer.attach(BUTTON_PIN);
          debouncer.interval(5);
        
        }
        
        void presentation() {
          // Register binary input sensor to gw (they will be created as child devices)
          // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
          // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
          sendSketchInfo("Lounge Multi", "3.0");
          wait(100);
          present(CHILD_ID, S_LIGHT);  
        }
        
        
        //  Check if digital input has changed and send in new value
        void loop() 
        {
          debouncer.update();
          // Get the update value
          int value = debouncer.read();
        
          if (value != oldValue) {
             // Send in the new value
             send(msg.set(value==HIGH ? 1 : 0));
             oldValue = value;
          }
          batM();
          sleep(int BUTTON_PIN, int CHANGE, unsigned long ms=0);
        }
        
        
        
        
        
        void batM() //The battery calculations
        {
           delay(500);
           // Battery monitoring reading
           int sensorValue = analogRead(BATTERY_SENSE_PIN);    
           delay(500);
           
           // Calculate the battery in %
           float Vbat  = sensorValue * VBAT_PER_BITS;
           int batteryPcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.);
           Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %");  
           
           // Add it to array so we get an average of 3 (3x20min)
           batArray[batLoop] = batteryPcnt;
          
           if (batLoop > 2) {  
             batteryPcnt = (batArray[0] + batArray[1] + batArray[2] + batArray[3]);
             batteryPcnt = batteryPcnt / 3;
         
           if (batteryPcnt > 100) {
             batteryPcnt=100;
         }
         
             Serial.print("Battery Average (Send): "); Serial.print(batteryPcnt); Serial.println(" %");
               sendBatteryLevel(batteryPcnt);
               batLoop = 0;
              }
             else 
             {
             batLoop++;
             }
        }
        
        
        Y 1 Reply Last reply
        0
        • markjgabbM markjgabb

          hey @Yveaux

          // Enable debug prints to serial monitor
          #define MY_DEBUG 
          
          // Enable and select radio type attached
          #define MY_RADIO_RF24
          //#define MY_RADIO_RFM69
          
          #include <MySensors.h>
          #include <Bounce2.h>
          
          #define CHILD_ID 1
          #define BUTTON_PIN  3  // Arduino Digital I/O pin for button/reed switch
          
          Bounce debouncer = Bounce(); 
          int oldValue=-1;
          
          // Change to V_LIGHT if you use S_LIGHT in presentation below
          MyMessage msg(CHILD_ID,V_LIGHT);
          
          
          //=========================
          // BATTERY VOLTAGE DIVIDER SETUP
          // 1M, 470K divider across battery and using internal ADC ref of 1.1V
          // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
          // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
          // 3.44/1023 = Volts per bit = 0.003363075
          #define VBAT_PER_BITS 0.003363075  
          #define VMIN 1.9                                  //  Vmin (radio Min Volt)=1.9V (564v)
          #define VMAX 3.0                                  //  Vmax = (2xAA bat)=3.0V (892v)
          int batteryPcnt = 0;                              // Calc value for battery %
          int batLoop = 0;                                  // Loop to help calc average
          int batArray[3];                                  // Array to store value for average calc.
          int BATTERY_SENSE_PIN = A0;                       // select the input pin for the battery sense point
          //=========================
          
          void setup()  
          {  
            // Setup the button
            pinMode(BUTTON_PIN,INPUT);
            // Activate internal pull-up
            digitalWrite(BUTTON_PIN,HIGH);
          
            // After setting up the button, setup debouncer
            debouncer.attach(BUTTON_PIN);
            debouncer.interval(5);
          
          }
          
          void presentation() {
            // Register binary input sensor to gw (they will be created as child devices)
            // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
            // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
            sendSketchInfo("Lounge Multi", "3.0");
            wait(100);
            present(CHILD_ID, S_LIGHT);  
          }
          
          
          //  Check if digital input has changed and send in new value
          void loop() 
          {
            debouncer.update();
            // Get the update value
            int value = debouncer.read();
          
            if (value != oldValue) {
               // Send in the new value
               send(msg.set(value==HIGH ? 1 : 0));
               oldValue = value;
            }
            batM();
            sleep(int BUTTON_PIN, int CHANGE, unsigned long ms=0);
          }
          
          
          
          
          
          void batM() //The battery calculations
          {
             delay(500);
             // Battery monitoring reading
             int sensorValue = analogRead(BATTERY_SENSE_PIN);    
             delay(500);
             
             // Calculate the battery in %
             float Vbat  = sensorValue * VBAT_PER_BITS;
             int batteryPcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.);
             Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %");  
             
             // Add it to array so we get an average of 3 (3x20min)
             batArray[batLoop] = batteryPcnt;
            
             if (batLoop > 2) {  
               batteryPcnt = (batArray[0] + batArray[1] + batArray[2] + batArray[3]);
               batteryPcnt = batteryPcnt / 3;
           
             if (batteryPcnt > 100) {
               batteryPcnt=100;
           }
           
               Serial.print("Battery Average (Send): "); Serial.print(batteryPcnt); Serial.println(" %");
                 sendBatteryLevel(batteryPcnt);
                 batLoop = 0;
                }
               else 
               {
               batLoop++;
               }
          }
          
          
          Y Offline
          Y Offline
          Yveaux
          Mod
          wrote on last edited by
          #4

          @markjgabb said in example of using sleep:

          sleep(int BUTTON_PIN, int CHANGE, unsigned long ms=0);

          You should remove the int & unsigned long ms= from the function call. These are only required when defining a function :

          sleep(BUTTON_PIN, CHANGE, 0);
          

          http://yveaux.blogspot.nl

          1 Reply Last reply
          0
          • markjgabbM Offline
            markjgabbM Offline
            markjgabb
            wrote on last edited by
            #5

            @Yveaux
            Cheers that's perfect.... unfortunately my FTDI programmer broke last night so now i have to wait for a new one to arrive so that i can do some more work on it :(

            1 Reply Last reply
            0

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better 💗

            Register Login
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            19

            Online

            12.0k

            Users

            11.2k

            Topics

            113.4k

            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