Doing multiple timed things with Arduino: Unleash the millis()!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
- [Instructor] Hello, I hope you're doing fantastic. In this series, we've been talking about using the Arduino millis function to create timed events. Like maybe every three seconds you want a server to move or maybe every minute you wanna send a status update to a web server. Whatever it may be, you want something to occur at a timed interval. Now, if you haven't seen the previous lessons in this series, I highly recommend that after this lesson you go back and check 'em out, I think you're gonna find them very helpful. Now in this lesson, we are going to cover creating multiple timed events using the Arduino millis function, specifically in this lesson, we are going to map out a program that will execute two independent timed events. We'll code from scratch a program that reads and displays two sensor values at different intervals and finally we'll drink Kool-Aid and watch the sun go down. Every good program has an even better algorithm to go along with it, so before we start typing away at the Arduino IDE, first I wanna write out our plan of action. This is kind of like what we're trying to accomplish. Okay, I've got two sensors. One is a light-dependent resistor or LDR and the other is a temperature sensor. And what I wanna do is I wanna read these values and I wanna display them to the Serial Monitor window on my computer but I don't wanna do it all the time. I want to have it happen at two separate intervals. I want my light-dependent resistor to read and display every second and I want my temperature sensor to read and display every five seconds. So, I've got two separate events. So, I'm just gonna lay it out like event one that happens every second. I wanna read my LDR and then I wanna display the LDR value and then event two, this is gonna happen every five seconds, I wanna read a temperature sensor and then display that temperature sensor. And I'm gonna display them to the serial monitor window on the Arduino. Okay, so this is kind of the plan of action and you know, looking at this, I figure we could use the Arduino millis function to set up the timing for these events, I could use analogRead to read the LDR and the temperature sensor values and then we could use the Serial library to display those values to the Serial Monitor Window on our computer. When you're creating a program that has repetitive timed events, it also doesn't hurt to kind of lay out the timing of the events on a piece of paper. If we lay this out on a timeline, we can see that we have two events that overlap every five seconds. Luckily for us, we are using the millis function as opposed to trying to make this work with the Arduino delay function, so this should definitely be achievable. Now in this lesson, we are focusing on demonstrating how to code timed events with Arduino. So, I'm not gonna go into setting up the LDR and temperature sensor circuits, so just assume that I have them wired up. But if you're wondering how to do that, definitely check out the ProgrammingElectronics.com website for lessons on setting up different circuits. All right, so let's go ahead and jump into the Arduino IDE and start coding this thing from scratch. So, here we are in the Arduino IDE and I'm gonna start by just making myself a little to-do list in comments, so let me do that. Okay, so I've got a little to-do list written out here and I'm gonna kinda work through 'em. I won't hold myself tight to there but I just thought I'd give it a shot, it might help me kind work through the program. The first thing I'm gonna do is set up some pins for where I've got my sensors attached, so let me do that. Okay, so I've got two constants and I made these constants because they're not gonna change, these sensors are always gonna be at these pins and I used analog pin A2 and analog pint A4 for the LDR and temp sensor respectively, so that's pretty straight forward. I've got my circuit set up there. Actually I'm using Kit-on-a-Shield to kind of work through this program and now what I'm gonna do is set up variables for the timed events so I'll do that now. All right, so I have created two constants and two variables. The two constants are the eventTimes. This is like the interval at which I want each of these events to happen. Like I mentioned, I want to read and display the light-dependent resistor value every 1,000 milliseconds. I want to read and display the temperature sensor every 5,000 milliseconds or every five seconds, so I've got those set up. Those intervals aren't gonna change, so I made them constants. Oh but you know what? I need to make them unsigned too also. Let me do that. Okay, the variables I've created I've named previousTime_1 and previousTime_2, they're both unsigned longs. The reason I'm using this unsigned long data type is because the value for millis gets really big and I wanna make sure that I've got enough storage in that variable type, in that variable data type to hold that really big number. So, these pervious time variables allow us to help track the time as these events occur. And if any of this is looking foreign to you, make sure to check out the previous lessons and some of this code has already been explained. All right, so I'm gonna move on to the setup and I'm gonna set up Serial communication. I'll do that now. All right, I've used the begin function from the Serial library and I initiate Serial communication using the Serial.begin function. I'm using 9600 zip par rate. Pretty stand stuff right there. So, now I'm down in the loop and what I wanna do in the loop and I wanna do it frequently is I wanna update the current time, so I'm gonna create a currentTime variable and it's gonna constantly get updated with the millis function. So, I'm gonna do that now. So, I've created a variable named currentTime and I set it equal to the return value of millis. So, currentTime is gonna hold a snapshot of the value millis has counted up to to that point and every time through the loop, we're gonna be updating it and again it's an unsigned long because it's gonna hold a really big number and so, I wanna make sure that the currentTime variable has space to hold that really big number. All right, now I'm gonna set up the event timing for the first event. Let me do that. Okay, so I have set up the timing for my first event. So, the key to this event timing is this first if statement condition. And what we're doing is we're comparing the difference between the current time and the previous time with our event interval, so you can think of eventTime_1 as just 1,000 because it's a constant, it's never gonna change and what we're doing is we're looking at the currentTime which is always updating and we're seeing how much further it is from the previousTime that the event happened. In the previousTime, this previousTime_1, it only gets updated every time the event happens, so it kind of lags behind the currentTime, so as that first calculation increases, eventually it's going to exceed that eventTime interval. So, once this condition becomes true, then we run our event code, we print the Serial monitor, hit the LDR is and then what we do is an analogRead so analogRead, we're looking at the light-dependent resistor pin that we had declared previously, so we get that reading and we print it out to the Serial Monitor Window, so this is the event code, this is what we actually wanna have happen. Now down here, this is also a very important statement. This is how we are updating our previousTime so that in another 1,000 milliseconds, we can get this code to run again, so we're setting the previousTime equal to the currentTime, so we're nudging this value to kind of catch up with the currentTime. So, if this first line of code is a little confusing to you, I get it, it is kind of confusing the first time around. I think if you just kind of run some numbers through this, watch some of the previous lessons, you'll start to get how pretty straight forward this is. Okay, so what I'm gonna do is I am going to run this and I'm gonna upload it just to make sure everything seems to be working okay so far. Okay, so it looks about every second, I'm getting this reading from my light-dependent resistor. I'll put my hand over, okay, everything looks like it's working appropriately. So, that's cool, I have one timed event. Well, now I wanna add another timed event. How am I gonna do that? Well, all I'm gonna do is repeat this exact same thing but I'm gonna have different code on the inside and I'm gonna use the variables that I'd set up for that second timed event. So, I'll do that now. All right, well, you can see that I pretty much just did the exact same thing over again, I just used different variables this time. I used the event_2 variables as opposed to the event_1 variables. I've uploaded the sketch and I've opened the Serial Monitor and we can see that hey, this is pretty sweet. I'm getting my five light-dependent resistor readings and then I'm also getting my temperature sensor reading and the events are happening at the time I wanted them to be, so I've got two independent repetitive events working here right in this program. And it wasn't that painful to write this out. I think the toughest part is just wrapping your head around that if statement condition. Once you can nail that down and understand the comparison that's happening, we've got this interval on the right, that's how often we want something to happen and then we're just comparing the currentTime, the time that has elapsed and the previousTime, how long has it been since the last event? And those two values are both getting updated. Once you can wrap your head around that, and it does take a while, you might not fully get it after this lesson. I understand that completely. I would recommend in writing this out yourself once, maybe try turning an LED on and off or some events, just some really basic program just so you can drive this home. Okay, well, let's just do a quick review of what we did in this lesson. First, we mapped out what we were doing just by writing out our algorithm before we actually get into the code. Then we went into the Arduino IDE and from scratch we wrote a program that created two separate timed events. Well, I hope you enjoyed this lesson. Again, there's a whole series on using this millis function. Definitely check out the other videos in the series. I look forward to seeing you next time. Have a great day, bye.
Info
Channel: Programming Electronics Academy
Views: 61,705
Rating: 4.9707136 out of 5
Keywords: millis(), using millis, timed events with Arduino, Arduino, Arduino(Brand), Arduino Tutorial, Arduino Lesson, Open Source Hardware Group, Learning Arduino, Microcontrollers, Electronics, Arduino IDE, Arduino Sketch, Computer programming, C++, Programming Electronics Academy
Id: hD3cR25MbW8
Channel Id: undefined
Length: 12min 24sec (744 seconds)
Published: Thu May 02 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.