From the course: Arduino: Prototyping
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Understand timers - Arduino Tutorial
From the course: Arduino: Prototyping
Understand timers
- [Instructor] We've seen the problem when the delay function blocks your code from doing anything else. Now we'll see about another way to code without blocking by using a simple timer. A timer is based on the built in function Millis. Millis returns the number of milliseconds since the sketch began. Basically you save the value of millis when the timer starts, then you keep going until the required number of additional have passed. When that happens you run your time code. Let's look at the pseudocode describing a timer in our prototype. First of all you save the current millis as the start time. Then in the loop, if the current millis minus start time is greater than the timerLength, then your timer has expired, so you reset the timer, update your animation, and continue on. Now let's look at the code used to implement a timer. First we need some variables. One for the timerStart, one for the timer interval, and…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.