NoDelay Arduino Library

While I was in the process of programming a simple robot (One day I’ll write that up), I started working on an easy way to use the mills function to help perform tasks at different times. The concept is taken from the BlinkWithoutDelay example that comes with the Arduino IDE and instead of using the delay function to wait for a task, you use the mills function and measure the amount of time that has passed before running code again. This is a very useful way of writing code as it allows for different tasks to execute without stopping.

There is a bit of a catch to using this method though, and that is you need to do a bit of setup in order to use it. When using just delay, all you need to do is call the delay function and tell it how long you would like to wait. On the other hand, if you want to use a time based delay, you need to setup a few variables first. This might seem like a small trade off but there is a bit more to it. In order to use time delay, you need to create three different variables for each delay you want. It is not bad when you just want to use one delay, but if you’re writing a complex program with a lot of delays needed then things start to add up.

The robot I am putting together is very simple, but I don’t plan on keeping it that way. I know in the future I want to add more sensors, and I am going to need to poll all those sensors at different times, this is why I started writing what was at first a simple class to help me scale my project. This simple class then became a library as I started to add some features to it and realized there are other projects I want to build that could benefit from this code.

The library is not very complex but it has some useful features. There is an option to set the delay time, this is called setdelay. You pass it the amount of time in milliseconds you want the delay to be. You are also able to use this to change a delay time to something else if needed. Next is update, which when called returns a Bool (true or false) if the set amount of time has passed. You can use this in an if instatement to run code. Then lastly (for now maybe) is fupdate. This might seem like a typo but that is its name and it stands for function update. When creating a noDelay object, you have the ability to set a function to call when the set delay time has passed. This function can only be a void function that takes no arguments. You still need to call it like with update but instead of using an if statement, every time the delay time is exceeded a function containing your code is called.

Included in the library is to example sketches for blinking an LED. They are variants of the BlinkWithoutDelay example but one uses update, which works almost the same as the original example and the second which uses a function call.

You can find NoDelay on my GitHub page HERE or https://github.com/M-tech-Creations/NoDelay
You can now install NoDelay right from the Arduino IDE! Just search NoDelay in the Library Manager!

Here is a video showing the library in action. I’m blinking an LED while fading two others at different speeds.

Here is the code for BlinkFadeFade (Note: I used an Arduino Mega for this demo. If you want to make it yourself, and are using an Arduino Uno, you will need to use different pins as not all the digital pins for the Uno support PWM). This is just something that I quickly put together just to show how useful this library can be.

Arduino C, Programing , , , , ,

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.