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. void receive and while loop question.....

void receive and while loop question.....

Scheduled Pinned Locked Moved Troubleshooting
9 Posts 5 Posters 101 Views 4 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.
  • skywatchS Offline
    skywatchS Offline
    skywatch
    wrote on last edited by skywatch
    #1

    I have a new project that requires a separate function from 'loop' to handle some stuff, no problem there except within this function I need a 'while' loop that can only be exited if a value is received from the controller via the receive function.

    My question is, will the receive function be called sutomatically by mysensors or will I need to manually call it within the while loop?

    mfalkviddM 1 Reply Last reply
    0
    • skywatchS skywatch

      I have a new project that requires a separate function from 'loop' to handle some stuff, no problem there except within this function I need a 'while' loop that can only be exited if a value is received from the controller via the receive function.

      My question is, will the receive function be called sutomatically by mysensors or will I need to manually call it within the while loop?

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

      @skywatch the receive function will be called if your while loop calls wait() . If your while loop does not call wait(), no messages will be processed.

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

        @skywatch
        no idea what you're trying to do, but maybe it would be better to use async/state machine.
        why not doing something simple like this:

        bool isFooEnabled= false;
        loop() {
          if (!isFooEnabled) {
             doMain();
          }
          else {
           doFoo();
          }
        }
        
        receive() {
        // on your msg, set isFooEnabled to true or false to start/stop doFoo
        }
        

        Like this nothing can block, your doFoo() doesn't need a while loop, you could just use if/then with states variables. Then on each arduino loop mysensors message will be processed and receive function called

        1 Reply Last reply
        4
        • skywatchS Offline
          skywatchS Offline
          skywatch
          wrote on last edited by skywatch
          #4

          Thanks for the suggestions. I got into a jam and took some time out. I'll be working on it today I hope.

          The main thing is to remotely send a calibration command to make the device read variables form the sensor once and run a calibration routine that measures max and min values for it's location. Then send an 'un-calibration' so that it runs normally and only triggers an alarm only if values outside the calibration window occur.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            grumpazoid
            wrote on last edited by
            #5

            Hope ok to ask here as I am struggling a bit to understand wait() in context with loops. I have used it with success with a separate receive function but need a bit of clarification.

            I have read on another post that wait is the same as using the millis method but can't get my head around how to use it.
            If using millis I would use the state machine if else method as suggested by @scalz

            How do I use wait in one place and then get another loop or process to run in the meantime?

            Raspberry Pi 3B - Domoticz + Node Red
            Arduino Pro Mini 3.3V - W5100 Ethernet, Nrf24
            Arduino Mega, RFLink 433Mhz
            Sonoff/ESP8266/Tasmota switches

            YveauxY 1 Reply Last reply
            0
            • G grumpazoid

              Hope ok to ask here as I am struggling a bit to understand wait() in context with loops. I have used it with success with a separate receive function but need a bit of clarification.

              I have read on another post that wait is the same as using the millis method but can't get my head around how to use it.
              If using millis I would use the state machine if else method as suggested by @scalz

              How do I use wait in one place and then get another loop or process to run in the meantime?

              YveauxY Offline
              YveauxY Offline
              Yveaux
              Mod
              wrote on last edited by Yveaux
              #6

              @grumpazoid said in void receive and while loop question.....:

              How do I use wait in one place and then get another loop or process to run in the meantime?

              Simple, you don't ;-)

              Wait() will block processing your code for the specified amount of milliseconds, while millis() will immediately return the current time, in milliseconds.
              Arduino is single threaded, so only one task can run at any time. To execute some code, while another piece is 'waiting' you should use millis() to determine how much time elapsed each time your code is executed, eg

              void loop() {
                static unsigned long start0 = millis();  // have a valid initial value
                unsigned long now = millis();
                if (now - start0 > 1000ul) {
                  // run once every second
                  //... Your code... 
                  start0 = now;
                } 
                static unsigned long start1 = millis();
                now = millis();
                if (now - start1 > 2000ul) {
                  // run once every 2 seconds
                  //... Your other code... 
                  start1 = now;
                } 
                // etc
              

              http://yveaux.blogspot.nl

              1 Reply Last reply
              0
              • G Offline
                G Offline
                grumpazoid
                wrote on last edited by grumpazoid
                #7

                @Yveaux

                Thanks for that explanation. Understanding this, the bit I am still confused about is how I can use wait() and still able to listen for incoming messages. Is that an exception to the rule? What stops the processing being blocked in this case?

                Raspberry Pi 3B - Domoticz + Node Red
                Arduino Pro Mini 3.3V - W5100 Ethernet, Nrf24
                Arduino Mega, RFLink 433Mhz
                Sonoff/ESP8266/Tasmota switches

                YveauxY 1 Reply Last reply
                0
                • G grumpazoid

                  @Yveaux

                  Thanks for that explanation. Understanding this, the bit I am still confused about is how I can use wait() and still able to listen for incoming messages. Is that an exception to the rule? What stops the processing being blocked in this case?

                  YveauxY Offline
                  YveauxY Offline
                  Yveaux
                  Mod
                  wrote on last edited by
                  #8

                  @grumpazoid wait() is a special implementation of a wait loop in the mysensors stack, that also processes messages while at the same time waiting for the requested time to elapse.
                  It is in principle the same as i showed before: it checks time elapsed and if some time is still remaining it handles incoming messages.

                  http://yveaux.blogspot.nl

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    grumpazoid
                    wrote on last edited by
                    #9

                    @Yveaux Makes sense now. Thanks again. :hugging_face:

                    Raspberry Pi 3B - Domoticz + Node Red
                    Arduino Pro Mini 3.3V - W5100 Ethernet, Nrf24
                    Arduino Mega, RFLink 433Mhz
                    Sonoff/ESP8266/Tasmota switches

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


                    29

                    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