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

Описание к видео Doing multiple timed things with Arduino: Unleash the millis()!

🤩 FREE Arduino Crash Course 👇👇
https://bit.ly/get_Arduino_skills

Want to learn more? Check out our courses! https://bit.ly/33ceYv4

Want to do multiple thing with Arduino? Then master the Arduino millis function!

This video will walk step-by-step through how to use millis with Arduino.

**Get the code, transcript, challenges, etc for this lesson on our website**
http://bit.ly/2WecXIl

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

**About Us:**
This Arduino tutorial was created by Programming Electronics Academy. We are an education company who seek to help people learn about electronics and programming through the ubiquitous Arduino development board.

**We have no affiliation whatsoever with Arduino LLC, other than we think they are cool.**

Комментарии

Информация по комментариям в разработке