Now you might be wondering what use that is, and it’s actually quite handy not to use delay function that’s, because the delay function stops everything cold on the Arduino board. That means nothing else can run while that delay function is counting down its time. So imagine, for example, you had a cell phone and every time you open up an application on your cell phone, you can’t receive a call because that application is running it’s kind of like what happens with the delay function. It interrupts everything else and it just waits out for that delay time. So what we’re going to explore in this lesson is ways to get around using a delay function. For this lesson, you’ll need an Arduino board. I have the Arduino Uno here you can use any art we know or clone. I do recommend sticking with the Arduino name brand for these examples, so that we know we’re all on the same page. You will need one 220 ohm resistor or there abouts. You will need an alligator clip to connect, leads together and you will need one led let’s go ahead and set up the circuit. I take my LED I’m going to take the long pin, also known as the anode of my LED and I’m going to put it into pin 13 on my Arduino board. Then I’m, going to take my resistor doesn’t matter, which end and I’m going to put it into ground on erred way, no ground being the Lotus lowest voltage on the Arduino board.

So current will run from higher voltage to lower voltage and then I’m going to take. My alligator clip and connect those two leads together on the LED and the resistor and then once that’s complete. I can plug my Arduino into my computer now that we’ve got the Arduino plugged in let’s, go ahead and open up our example here, we’re going to go to digital blink without delay. Okay, so this example opens up like many of the other examples. We’Ve got a description of the code at the top comment it out and basically we’ve already talked about what this program will do. It will blink an LED on pin 13 without using the delay function. We can see. We can thank David and Paul for making and modifying this to example, and we know it’s in the public domain, so we can mess with it as we please there’s, even a URL link in here to check it out on the air between a website. So the first block of code is where he initializes and declares his variable variables. I really like what’s been done here because he separates constants from non constant variables, so the first variable he sets up it’s an integer and it is led, pin and set as pin 13. So he doesn’t want that variable to change. So he sets constant front of integer to qualify the effects of that variable. Then we come down to variables that will change so the first one is an integer it’s called LED state and an E and it’s equal to low you’ll, see how handy this is.

You can see low is a keyword and you’ll see how that’s used here shortly. Then we have a new data type called long and we’re signing it to previous Millis. So Millis, just think of milliseconds. All right, he’s set a equal to zero, so in an integer is just what it says is an integer, but it doesn’t store as many values as a long does so long will still only store integer values, but it will store huge numbers comparatively to just integer, Which will not store as many numbers and that’s important, because if you don’t need huge numbers, you want to use integer because you only take up less space. But if you use a long, you know you’re going to use huge numbers, and sometimes you have to do it, and so the software will leave a lot more room for that variable. Okay, so then we come down and we’ve got one more we’ve got one more variable here: we’re called in an interval it’s set to a thousand, and this is going to represent milliseconds, so that’s, actually one second and it’s also a long. So we know that that number will be getting big. So now we come down to our next block of code. It’S void, setup, okay and all we’re doing is setting the mode of the pin so we’re using the pin mode function. What pin do we want to set the mode for LED pin and if you recall that was the constant we set at 13 and we want the mode of that pin to be an output.

So that’s easy enough. Now we come down to our final block of code here, void loop right here, we’ve got quite a few things in here, so the first line here he’s got commented out is basically this is where you would put the code that you would want to be running. All the time so this is like this would be the meat and potatoes to the code and what follows, and what we’ll be talking about, is the code that it will take to blink that LED without the delay function. Okay, so the first thing he does is we have a get a new data type here or a new qualifier rather and what we’re doing is we are setting this variable called current Millis. So this is current milliseconds. It is a long. So again, a long is a very large data, type and we’re saying that it is unsigned. So normally when you have a long data type, it will go from like negative 2 million all the way up to 2 million it’s, actually larger than that. I don’t know the exact number offhand, but but when we say it is unsigned that means that current Millis is going to start at 0 and go all the way to like 4 million in something so it’s. This giant positive number, okay, and the reason we want it to be unsigned is because what will happen is that if that number rolls over – and it is a just along – it will go and roll over to a negative value and then skirt start counting up to 0 and then go all the way to positive and then, if it could, if it fills up, it will go back to negative well, we don’t want that to happen when it gets when it maxes out to it’s 4 million number.

We want to go to go back to 0 all right, so you might be wondering well cheese. What are we going to have to get that? What number would we assign to this variable that it would get up to the 4 millions, and the answer is this function, which is Millis? What Millis does and just again when you see millisecond milliseconds, is Millis, will return the value of milliseconds to that variable based on the number of milliseconds, since the Arduino has started running so the second you apply power to your Arduino. It is got a timer. That is counting in milliseconds the amount of time that has passed and if you want to get to that number, you just use the Millis function: okay, so, and about after 50 days, this Millis function will max out and it will turn over back to zero. Okay. So maybe a little strange, I think, you’ll get it as you think about it. So now the first thing we run into is a if statement, and the condition that is set is it wants to know. Is the current millisecond state minus the previous millisecond state, greater than interval? Okay, so let’s think about this. The first time it runs through so current milliseconds is going to be the time that’s on that’s, captured from the Arduino board in this variable and then previous milliseconds. We set this equal to zero up at the top of the sketch when we start it out.

So basically, it wants to know. Is this amount of time so let’s say it’s it’s been 40 milliseconds since we started the program. Okay, it wants to know, is 40 milliseconds greater than interval. Well, if you recall, we said interval equal to 1000 milliseconds or one second, so if 40 milliseconds into this program, this condition is not met. So the if statement will or will not run but one second into having plugged the Arduino in current Millis would be 1000 let’s, say 1000 and one previous millisecond was zero. So this value is a thousand and one it would be greater than interval, and so this code would execute okay. So now what we do is we set previous Millis equal to current MELAS, so basically we’re saying the what is the last time this code executed? So previous Millis in this case would equal 1001. Ok, so now the code that we’re going to execute is we’re going to state we’re going to we’ve got another. If statement we’re going to see is if, and the condition is led state equals is e – is currently at low. So again, the equal equal sign is just asking: is LHD state equal to low and if it is then we’re going to set LED state equal to high otherwise we’re going to set the LED state equal to low and then what we do is we just digital Write LED, pin that’s, pin 13 of your call in an LED state.

Well, so what is LED state in our case led state will be high because we started off with LED state equal to low all right up in the. If you recall, when we declared LED state, we said LED state is low, so this condition would be met, LED state would be high and so led pin their team led state. The pin is going to be high, ok, a little wordy. I think you get the idea so now we run through the void loop again any code up here that you wanted to be running. All the time would be running once that code finished, executing we’d come down and again we would sign the current milliseconds. So now let’s say: another second is passed. Okay, so now we’re left with C. We write two thousand two milliseconds, so we come to this if statement again, so if current Millis, which is 2002 previous milliseconds, is so previous milliseconds was set at 1001. Theoretically, our last time right, if that number is greater than the interval which was 1000, then this code will execute. So you can see that this code, in that for loop, is going over very rapidly. You know it’s going to be going over several times per millisecond and so it’s, not until this condition is met. That enough time has elapsed in milliseconds that the interval is greater than a thousand, that this code will run and what it’s going to do is it’s.

Going to check the state of the LED, so last time our LED was high. We left our LED state was high, so here the if statement, this condition is not met because our LED is high, so else our LED is going to be sent low all right. So it’s going to turn the LED off and then it’s going to run through that code, one more time, it’s going to see that the LED is low, it’s, going to set it to high and it’s going to do this. Every thousand milliseconds based on that interval. All right set basically goes through how we’re getting around using a delay function, so let’s go ahead and verify the code and let’s upload it and then let’s go ahead and take a look at our art. We know and see what it’s doing all right. Another blinking LED, but what’s cool about this blinking LED is. We are not holding up the program. We can be doing all types of different stuff, while that LED is blinking and that’s the whole concept of not using that delay function.


[adrotate banner=”1″]

arduino delay function Video

[mam_video id=y17QOtAVn_s]



[adrotate banner=”2″]


[mam_tag id=184]



[adrotate banner=”3″]


arduino delay function news






[adrotate banner=”4″]



arduino delay function Social



[adrotate banner=”5″]




Cornell University’s new stretchable optical lace could enable robots to sense how they interact with their environment and adjust their actions accordingly.

A fun way for students to learn about the brachistochrone curve!

Some Arduino (UNO) uP projects. New for me programming in C++.

Have tested random() function for lightning up one of the ten LEDs and also the amount of delay. #ArduinoYun #Arduino


[adrotate banner=”6″]