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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
/* * Name: BlinkFadeFade * This is a Demo sketch written to show what NoDelay can do * to make running different tasks at near the same time simple * * Written by Mario of M-tech Creations * 4/24/2021 fixed issue with not declared in this scope error * 7/19/15 */ #include "NoDelay.h" int led1 = 13, led2 = 12, led3 = 11; int ledState = LOW; int brightness = 0; int fadeAmount = 5; int brightness2 = 0; int fadeAmount2 = 5; void blink(); //Must declare function before noDelay, function can not take arguments void fade1(); void fade2(); noDelay nBlink(1000, blink); noDelay nFade1(20, fade1); noDelay nFade2(60, fade2); void setup() { pinMode(led1,OUTPUT); pinMode(led2,OUTPUT); pinMode(led3,OUTPUT); } void loop() { nBlink.update(); nFade1.update(); nFade2.update(); } void blink() { if (ledState == LOW) ledState = HIGH; else ledState = LOW; // set the LED with the ledState of the variable: digitalWrite(led1, ledState); } void fade1() { analogWrite(led2, brightness); // change the brightness for next time through the loop: brightness = brightness + fadeAmount; // reverse the direction of the fading at the ends of the fade: if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ; } } void fade2() { analogWrite(led3, brightness2); // change the brightness for next time through the loop: brightness2 = brightness2 + fadeAmount2; // reverse the direction of the fading at the ends of the fade: if (brightness2 == 0 || brightness2 == 255) { fadeAmount2 = -fadeAmount2 ; } } |