Overloading the Arduino delay


  • Contest Winner

    I'm not sure if this is possible. But it would help a lot of people, if MySensors would overload the Arduino delay and replace it with a MySensors wait implementation.

    I'm aware of the fact that this might cause some problems with existing libraries. Because they we're build around the delay function. But these libraries aren't the best anyway.

    Please consider this. Because it'll help a lot of MySensors users.


  • Mod

    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.


  • Contest Winner

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


  • Mod

    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.


  • Mod

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


  • Mod

    @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
        } 
    } 
    

  • Mod

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


  • Contest Winner

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



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


  • Mod

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


  • Contest Winner

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



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


  • Contest Winner

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



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


  • Hardware Contributor

    @Theol I like the way you're thinking, trying to make things affordable 👍 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? 😱

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


Log in to reply
 

Suggested Topics

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts