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. Feature Requests
  3. Overloading the Arduino delay

Overloading the Arduino delay

Scheduled Pinned Locked Moved Feature Requests
15 Posts 5 Posters 4.9k 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.
  • mfalkviddM Offline
    mfalkviddM Offline
    mfalkvidd
    Mod
    wrote on last edited by mfalkvidd
    #2

    I don't like the idea of overloading stuff, because troubleshooting things when standard functions don't behave the way they should is almost impossible. But I do think that some of people's problems would be solved if people knew to use wait() instead of delay().

    The FastLED library has a similar problem, since they want to keep updating the leds just as MySensors wants to keep checking the radio. Their corresponding function to wait() is FastLED.delay().

    So if I have a node that uses FastLed and MySensors, should I use FastLED.delay() or wait()? The answer is probably to build my own delay-function which calls both, with short intervals, something like (but better, since this crude version doesn't handle sub-2 ms delays and probably lots of other things):

    void delay_graceful(long delay){
        long start = millis();
        while ( millis() - start < delay){
            FastLED.delay(1);
            wait(1);
        }
    }
    

    On the other hand, esp8266 needs calling yield() but has overloaded delay() so it calls yield(). Maybe that's the way to go for MySensors as well?

    Digging up the reasoning behind Fastled's and esp's choices would probably be useful.

    An alternative could be to add a compile warning when delay is used, to notify the user of the potential problem and direct them to a link/thread where different solutions are discussed.

    TheoLT 1 Reply Last reply
    2
    • mfalkviddM mfalkvidd

      I don't like the idea of overloading stuff, because troubleshooting things when standard functions don't behave the way they should is almost impossible. But I do think that some of people's problems would be solved if people knew to use wait() instead of delay().

      The FastLED library has a similar problem, since they want to keep updating the leds just as MySensors wants to keep checking the radio. Their corresponding function to wait() is FastLED.delay().

      So if I have a node that uses FastLed and MySensors, should I use FastLED.delay() or wait()? The answer is probably to build my own delay-function which calls both, with short intervals, something like (but better, since this crude version doesn't handle sub-2 ms delays and probably lots of other things):

      void delay_graceful(long delay){
          long start = millis();
          while ( millis() - start < delay){
              FastLED.delay(1);
              wait(1);
          }
      }
      

      On the other hand, esp8266 needs calling yield() but has overloaded delay() so it calls yield(). Maybe that's the way to go for MySensors as well?

      Digging up the reasoning behind Fastled's and esp's choices would probably be useful.

      An alternative could be to add a compile warning when delay is used, to notify the user of the potential problem and direct them to a link/thread where different solutions are discussed.

      TheoLT Offline
      TheoLT Offline
      TheoL
      Contest Winner
      wrote on last edited by
      #3

      @mfalkvidd you point out one of my main concerns. But libraries shouldn't depend on a delay because that causes timing issues on lower frequency arduino's like e.g. The Pro Mini 3.3V. A good library should use other timer mechanisms, like most libraries do.

      For those libraries you could add a compiler flag indicating that Arduino delay should be used or the MySensors variant.

      Overloading is probably not the right name. It should be overriding.

      YveauxY 1 Reply Last reply
      0
      • TheoLT TheoL

        @mfalkvidd you point out one of my main concerns. But libraries shouldn't depend on a delay because that causes timing issues on lower frequency arduino's like e.g. The Pro Mini 3.3V. A good library should use other timer mechanisms, like most libraries do.

        For those libraries you could add a compiler flag indicating that Arduino delay should be used or the MySensors variant.

        Overloading is probably not the right name. It should be overriding.

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

        It's just better by design to not use any delays or waits of whatever kind.
        I understand it's simple to implement, but for arduino and any libraries which depend on regular execution (especially when combining stuff) it's better to run as fast as you can and check a timer for regular execution.
        An even better solution is implementing a state machine, but this concept may be harder to grasp for the majority of the arduino community.

        http://yveaux.blogspot.nl

        mfalkviddM 1 Reply Last reply
        2
        • YveauxY Yveaux

          It's just better by design to not use any delays or waits of whatever kind.
          I understand it's simple to implement, but for arduino and any libraries which depend on regular execution (especially when combining stuff) it's better to run as fast as you can and check a timer for regular execution.
          An even better solution is implementing a state machine, but this concept may be harder to grasp for the majority of the arduino community.

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

          @Yveaux I agree, but doing what you suggest is way out of league for the users that would be helped by theol's suggestion.

          YveauxY 1 Reply Last reply
          1
          • mfalkviddM mfalkvidd

            @Yveaux I agree, but doing what you suggest is way out of league for the users that would be helped by theol's suggestion.

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

            @mfalkvidd not necessarily. We could also supply e.g. a macro that wraps the check for elapsed time.
            Just some idea:

            void loop() 
            {
                RUN_EVERY_MS(1000)
                {
                    // Code executed once per second
                } 
                RUN_EVERY_MS(1234)
                {
                    // Code executed once per 1.234 seconds
                } 
            } 
            

            http://yveaux.blogspot.nl

            mfalkviddM 1 Reply Last reply
            2
            • YveauxY Yveaux

              @mfalkvidd not necessarily. We could also supply e.g. a macro that wraps the check for elapsed time.
              Just some idea:

              void loop() 
              {
                  RUN_EVERY_MS(1000)
                  {
                      // Code executed once per second
                  } 
                  RUN_EVERY_MS(1234)
                  {
                      // Code executed once per 1.234 seconds
                  } 
              } 
              
              mfalkviddM Offline
              mfalkviddM Offline
              mfalkvidd
              Mod
              wrote on last edited by mfalkvidd
              #7

              @Yveaux FastLed provides a similar macro, and it is indeed useful. But it wouldn't help the newbies who copy and paste code with delay statements, or those who follow the general advice on the net which is to use delay.

              The problem is that people aren't aware that they shouldn't use delay. If they were aware, the problem wouldn't exist.

              TheoLT 1 Reply Last reply
              0
              • mfalkviddM mfalkvidd

                @Yveaux FastLed provides a similar macro, and it is indeed useful. But it wouldn't help the newbies who copy and paste code with delay statements, or those who follow the general advice on the net which is to use delay.

                The problem is that people aren't aware that they shouldn't use delay. If they were aware, the problem wouldn't exist.

                TheoLT Offline
                TheoLT Offline
                TheoL
                Contest Winner
                wrote on last edited by
                #8

                @mfalkvidd I'm covering this topic in my workshop. When I'm done with the presentation I'll post a link in the moderator area. Maybe it's useful as a sort of tutorial for new MySensors users? In another form than a keynote of course.

                YveauxY 1 Reply Last reply
                0
                • tlpeterT Offline
                  tlpeterT Offline
                  tlpeter
                  wrote on last edited by
                  #9

                  That would be very usefull.
                  Like me many people like to work with there hands and copy code.
                  I am in code a little bit but not enough to build my own code.
                  When i see code than most of it i understand.

                  TheoLT 1 Reply Last reply
                  0
                  • TheoLT TheoL

                    @mfalkvidd I'm covering this topic in my workshop. When I'm done with the presentation I'll post a link in the moderator area. Maybe it's useful as a sort of tutorial for new MySensors users? In another form than a keynote of course.

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

                    @TheoL good idea!

                    But more general, it could also land in a decent troubleshooting or good-practise section on the site.
                    I think we may expect some programming skills from mysensors users, albeit limited.

                    http://yveaux.blogspot.nl

                    1 Reply Last reply
                    2
                    • tlpeterT tlpeter

                      That would be very usefull.
                      Like me many people like to work with there hands and copy code.
                      I am in code a little bit but not enough to build my own code.
                      When i see code than most of it i understand.

                      TheoLT Offline
                      TheoLT Offline
                      TheoL
                      Contest Winner
                      wrote on last edited by
                      #11

                      @tlpeter The thing is, that creating your own MySensors sketches isn't that hard. But you got to have a basic knowledge of MySensors. I don't know yet how I'll present it, but I'll share some of the topics from my workshop on the MySensors forum or website.

                      1 Reply Last reply
                      0
                      • tlpeterT Offline
                        tlpeterT Offline
                        tlpeter
                        wrote on last edited by
                        #12

                        @TheoL , i saw that there is a workshop but unfortunally i am very busy coming week so i can't come.
                        I will need some more time learning the code.
                        Funny i enough i used a wait for my rain sensor (moisture sketch with analog instead of digital)
                        I remove the wait and it is running much better.

                        TheoLT 1 Reply Last reply
                        0
                        • tlpeterT tlpeter

                          @TheoL , i saw that there is a workshop but unfortunally i am very busy coming week so i can't come.
                          I will need some more time learning the code.
                          Funny i enough i used a wait for my rain sensor (moisture sketch with analog instead of digital)
                          I remove the wait and it is running much better.

                          TheoLT Offline
                          TheoLT Offline
                          TheoL
                          Contest Winner
                          wrote on last edited by
                          #13

                          @tlpeter There might be another option in the future. You might have time them. I just want to learn people how to use MySensors and have fun building things.

                          1 Reply Last reply
                          0
                          • tlpeterT Offline
                            tlpeterT Offline
                            tlpeter
                            wrote on last edited by
                            #14

                            For sure i want it to learn too as it is frustrating thinking you are doing it right but have nothing but trouble :smile:

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

                              @Theol I like the way you're thinking, trying to make things affordable :thumbsup: easing coding life for noobs, not so easy task.

                              Imho I prefer non blocking stuff too, state machine like. I think this is really the way to think for a machine, IOT.

                              I think this is kind of education..more mcu programming related.
                              A very basic state machine just needs few "If" and/or "switch case". Often, people don't know theyr're doing a state machine ;) I mean the basics of course, because you can use function pointers etc which are more advanced, use more mem etc..I choose function pointers or "If/switch" depending of the context but looks better with pointers. I hope I don't say dumb things or Yveaux will correct me :)

                              Ideally, very basic and important stuff a newbie need to learn for playing with a MCU, then with Mysensors, would be:

                              • to understand why there is no "multitask" regarding how an mcu works. and irq, priority and pullup/downs..
                              • a bit of algo
                              • and arduino reference..

                              Sure Mysensors looks easy to use. And that's true ;)
                              But if people think they don't need to learn this, even with multiple override, they will be limited, with more bugs, and more questions.. Then, of course, newbie would be happy to have done something, so he shares (thx). And an other noob comes, takes the sketch and share etc. finally where's quality..

                              I have rarely seen this (about delay) when i was learning from microchip community though ;) And that's what I thought first time I saw arduino community! What this..so much people are using delay here! I will have to write my things from scratch, I can't trust, I will have not time to check everything..damn! Hopefully that's not completely true!
                              Have they read some reference before starting? With all resources from arduino, I think there is no excuse :) Led blinking without delays etc..

                              Hmm, a bit philosophical..Vulgarisation, mastering :) I understand main Mysensors goals. Why not a graphical ui in place, which would use "blocks" instead of code? :scream:

                              I'm always interested with your stuff @Theol and will keep an eye on this too :)

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


                              21

                              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