<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>millis() on Bald Engineer</title><link>https://www.baldengineer.com/tag/millis</link><description>Recent content in millis() on Bald Engineer</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Wed, 10 Oct 2018 04:35:40 +0000</lastBuildDate><atom:link href="https://www.baldengineer.com/tag/millis/index.xml" rel="self" type="application/rss+xml"/><item><title>Arduino millis() plus addition does not add up</title><link>https://www.baldengineer.com/arduino-millis-plus-addition-does-not-add-up.html</link><pubDate>Wed, 10 Oct 2018 13:00:23 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=7571</guid><description>In the past, I’ve covered how to reset Arduino millis() and have provided a growing list of examples using millis(). While reviewing the code for the elegoo Penguin Bot, I was reminded of a millis() mistake I see often: addition. The only way to properly handle millis() rollover is with subtraction. Let’s look at why (and how.)&#10;What is Arduino millis() The Arduino library has a function called millis() which returns the number of milliseconds the processor has been running. On other platforms, you might see references to a “tick counter.” It is the same idea. A hardware timer keeps incrementing a counter at a known rate. In this case, that rate is milliseconds.<p><img src="https://www.baldengineer.com/wp-content/uploads/2018/10/Arduino-millis-banner.jpg" alt="Arduino millis() plus addition does not add up"/></p></description></item><item><title>Five Arduino math fixes for when it is wrong</title><link>https://www.baldengineer.com/arduino-math-fixes.html</link><pubDate>Wed, 04 Jul 2018 13:16:52 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=7463</guid><description>While the Arduino library does an excellent job of hiding some of C/C++’s warts, at the end of the day, it is still just C/C++. This fact causes a few non-intuitive issues for inexperienced programmers. When it looks like Arduino math is wrong, it is probably one of these reasons.&#10;When people ask me for help with their programming, I check each of these Arduino math mistakes. If your code seems to be hitting a bug, check to make sure it is not how the compiler handles math.<p><img src="https://www.baldengineer.com/wp-content/uploads/2018/07/Arduino-Math-Chalkboard-1960px-v2.jpg" alt="Five Arduino math fixes for when it is wrong"/></p></description></item><item><title>Detect short and long button press using millis</title><link>https://www.baldengineer.com/detect-short-long-button-press.html</link><pubDate>Wed, 19 Apr 2017 12:00:48 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=6440</guid><description>To detect a short and long button press using millis can give your project more functionality without adding more buttons. In this line-by-line example, I show how to react to a user pressing a button for a short period (100ms) or a long period (over 500ms). My example changes the blink rate of an LED on short presses. A long button press turns off the LED.&#10;In the code, I make use of a struct so that a single variable can be used to track multiple parameters. The benefit of this method is that adding multiple buttons is easy. You could create an array of these tyepdef’d variables.<p><img src="https://www.baldengineer.com/wp-content/uploads/2017/04/lbl-short-and-long-button-press.jpg" alt="Detect short and long button press using millis"/></p></description></item><item><title>Fading LED: analogWrite millis() Example</title><link>https://www.baldengineer.com/fading-led-analogwrite-millis-example.html</link><pubDate>Wed, 23 Nov 2016 14:34:21 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=6250</guid><description>It’s a well-known fact of engineering: LEDs make everything look better. And that means a Fading LED is even better. Using Arduino’s analogWrite(), fading a LED is just a matter of a loop. If you use delay(), you can’t easily add other actions. What can you do? Well, Fading a LED with millis() is pretty simple. Here’s the code to do it and a quick explanation.&#10;//pwm-fade-with-millis.ino // Example Fading LED with analogWrite and millis() // See baldengineer.com/fading-led-analogwrite-millis-example.html for more information // Created by James Lewis const byte pwmLED = 5; // define directions for LED fade #define UP 0 #define DOWN 1 // constants for min and max PWM const int minPWM = 0; const int maxPWM = 255; // State Variable for Fade Direction byte fadeDirection = UP; // Global Fade Value // but be bigger than byte and signed, for rollover int fadeValue = 0; // How smooth to fade? byte fadeIncrement = 5; // millis() timing Variable, just for fading unsigned long previousFadeMillis; // How fast to increment? int fadeInterval = 50; void setup() { // put pwmLED into known state (off) analogWrite(pwmLED, fadeValue); } void doTheFade(unsigned long thisMillis) { // is it time to update yet? // if not, nothing happens if (thisMillis - previousFadeMillis &gt;= fadeInterval) { // yup, it's time! if (fadeDirection == UP) { fadeValue = fadeValue + fadeIncrement; if (fadeValue &gt;= maxPWM) { // At max, limit and change direction fadeValue = maxPWM; fadeDirection = DOWN; } } else { //if we aren't going up, we're going down fadeValue = fadeValue - fadeIncrement; if (fadeValue &lt;= minPWM) { // At min, limit and change direction fadeValue = minPWM; fadeDirection = UP; } } // Only need to update when it changes analogWrite(pwmLED, fadeValue); // reset millis for the next iteration (fade timer only) previousFadeMillis = thisMillis; } } void loop() { // get the current time, for this time around loop // all millis() timer checks will use this time stamp unsigned long currentMillis = millis(); doTheFade(currentMillis); } Global Variables for Fading LED Constants The constant “pwmLED” is the pin that gets faded. (Make that pin supports analogWrite().) The two #define statements, UP and DOWN, are simple states for the LED. They make the code easier to read. The “maxPWM” value is set to 255 for 8-bit AVR boards like the Uno. If you’re using the ESP8266, set this value to 1023. (You can also use the constant “PWMRANGE.”)<p><img src="https://www.baldengineer.com/wp-content/uploads/2016/11/led-fading-millis-example.jpg" alt="Fading LED: analogWrite millis() Example"/></p></description></item><item><title>Use Arduino millis() with buttons to delay events</title><link>https://www.baldengineer.com/use-millis-with-buttons-to-delay-events.html</link><pubDate>Wed, 27 Jan 2016 16:00:16 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=5720</guid><description>One of the common questions related to using the millis() function in Arduino, is around timed events. After an event occurs, you want the code to wait for some time before doing the next step. But you don’t want to stop the program with delay().&#10;In this example, we will use millis() to wait a few seconds after a pushbutton press to turn on an LED. Then a few seconds later, we will turn it off. All without using delay().<p><img src="https://www.baldengineer.com/wp-content/uploads/2016/01/lbl-delayedMillisActions.png" alt="Use Arduino millis() with buttons to delay events"/></p></description></item><item><title>Proper Larson Scanner with Software PWM (Repost)</title><link>https://www.baldengineer.com/proper-larson-scanner-with-sw-pwm.html</link><pubDate>Wed, 21 Oct 2015 16:00:03 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=5501</guid><description>Last week I had a detailed Arduino tutorial on software pulse width modulation using millis() and micros(). Why? Because I wanted to create a Proper Larson Scanner, with persistence and at least 8 LEDs.&#10;KITT larson scanner example&#10;Even though it is a popular project for the Arduino Uno, most Larson scanner tutorials, like my first one, have a few flaws. First, there is no persistence, or tail, to the LED as it moves back and forth. Persistence could be solved by using pulse-width-modulation. The Uno and other 328p-based micros only have 6 Pulse Width Modulation (PWM) pins. And let’s be honest, every project is made better by adding more LEDs. :)<p><img src="https://www.baldengineer.com/wp-content/uploads/2015/10/cylon-pumpkin-proper-larson-banner.jpg" alt="Proper Larson Scanner with Software PWM (Repost)"/></p></description></item><item><title>Arduino Software PWM with millis()</title><link>https://www.baldengineer.com/software-pwm-with-millis.html</link><pubDate>Wed, 07 Oct 2015 13:00:04 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=5478</guid><description>The Arduino Uno has six pins dedicated to Pulse Width Modulation (PWM). PWM is great for analog-like control for the speed of motors or LED fading. But what if you want to control more than 6 devices? Or what if you’re using the PWM pins to control servo motors, but still want to fade an LED on a 7th pin?&#10;One option is to change boards and processors. For example, you could move up to the Arduino Mega 2560. That means a bigger board and more cost.<p><img src="https://www.baldengineer.com/wp-content/uploads/2015/10/lbl-arduino-software-pwm.jpg" alt="Arduino Software PWM with millis()"/></p></description></item><item><title>Blink without delay() explained line-by-line</title><link>https://www.baldengineer.com/blink-without-delay-explained.html</link><pubDate>Wed, 05 Aug 2015 13:00:27 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=5275</guid><description>Making the move to millis()-based code can be daunting. You have to rethink your logic, implement flags, program a state machine — and more importantly, start using millis(). Generally in forums and on IRC people will just point to the “blink without delay” example, hoping the commented code is enough for a new user. It’s not enough.&#10;I have a growing list of millis()-based tasks posted in my millis() cookbook. But sometimes that those examples might be too simple or not close enough to your project’s end target. That got me thinking about different ways to help explain how millis() works. I thought “wow, you need to understand every line of an example” which leads me to: line-by-line.<p><img src="https://www.baldengineer.com/wp-content/uploads/2015/08/lbl-blinkWithoutDelay.png" alt="Blink without delay() explained line-by-line"/></p></description></item><item><title>Microcontroller state machine with enum tutorial</title><link>https://www.baldengineer.com/state-machine-with-enum-tutorial.html</link><pubDate>Wed, 29 Jul 2015 13:00:07 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=5251</guid><description>Flag variables are great, and totally not evil, when you just have two states: ON or OFF. What about when you have multiple states? Is there an option better than creating multiple flag variables?&#10;The C-language has a declaration type just for this purpose. It is called an enumeration, or enum.&#10;Setting up a state machine with enum is a surprisingly simple. Arduino and embedded programmers should use them!&#10;All you need to do is create descriptive tag names, and let the compiler assign them an integer value. Unlike a #define which is just a macro replacement, the compiler treats an enum as your personal variable type.<p><img src="https://www.baldengineer.com/wp-content/uploads/2015/07/state-machines-with-enum-tutorial.jpg" alt="Microcontroller state machine with enum tutorial"/></p></description></item><item><title>Pushbutton and Flashing LED tutorial with millis()</title><link>https://www.baldengineer.com/pushbutton-and-flashing-led-tutorial-with-millis.html</link><pubDate>Wed, 06 May 2015 14:00:21 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=4930</guid><description>How to blink (or flash) a LED without delay() and detect button pushes One of the limitations of the delay() function is that nothing else can really be done. This presents a problem when you want to flash a LED while waiting for a pushbutton to be pressed.&#10;Flashing the LED with millis() and using a flag variable to find if the LED should be flashing solves this problem.&#10;Consider this another example to my virtual millis() cookbook.<p><img src="https://www.baldengineer.com/wp-content/uploads/2015/05/pushbutton-and-millis_bb-fixed2.png" alt="Pushbutton and Flashing LED tutorial with millis()"/></p></description></item><item><title>Arduino: De-Bounce a Button with micros() or millis()</title><link>https://www.baldengineer.com/arduino-de-bounce-a-button-with-micros.html</link><pubDate>Fri, 13 Dec 2013 12:25:45 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=2095</guid><description>The Arduino is fast, humans are slow. When you push down a button, what seems like a single change to slow humans is really multiple presses to an Arduino. This is known as “bouncing.” Figure 1 is an oscilloscope screenshot showing what could happen when a button is pressed.&#10;Figure 1 - All the bouncing!&#10;The top trace shows the high-low-high transition of the button press. But notice all of the activity at the start of the high-to-low transition. The bottom trace is a “zoomed-in” view of that area. The button is actually transitioning multiple times before staying low. Internally the contacts are “bouncing.” This could result in false reads. In software, we can ignore these bounces by waiting some period of time after the first transition.<p><img src="https://www.baldengineer.com/wp-content/uploads/2013/12/MG_1763.jpg" alt="Arduino:  De-Bounce a Button with micros() or millis()"/></p></description></item><item><title>Arduino: Independent On-Off Times with Millis()</title><link>https://www.baldengineer.com/millis-ind-on-off-times.html</link><pubDate>Tue, 10 Dec 2013 12:25:29 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=2059</guid><description>When using delay() to flash a LED there is a time for the LED to be on and then off. This makes it easy to have independent control of the “on” and “off” times. The standard blink without delay example doesn’t give you this flexibility.&#10;millis-cookbook-two-duty-cycles&#10;This example code gives you complete independent control of how long a LED (or any OUTPUT pin) stays “ON” or “OFF”. This also demonstrates a very simple two-state state machine.<p><img src="https://www.baldengineer.com/wp-content/uploads/2012/09/millis-ind-on-off-banner.png" alt="Arduino:  Independent On-Off Times with Millis()"/></p></description></item><item><title>Arduino Millis() Examples</title><link>https://www.baldengineer.com/arduino-millis-examples.html</link><pubDate>Mon, 09 Dec 2013 23:00:51 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?page_id=2055</guid><description>You’ve recently learned about millis() and can’t way to delete all of your references to delay(). The problem is that you don’t know quite how to convert your code into millis()-compatible code. Here is a (running) list of millis() examples I’ve put together to help.&#10;Baldengineer’s Arduino millis() Examples Arduino Multitasking - Step by step examples of how to convert delay() code into millis() based code, to simulate multitasking. Police Lights - Flash two LEDs like strobing police lights Control ON and OFF time for a flashing LED. - More control than “blink without delay” Stopwatch Example - Calculate how much time code takes to execute Chasing LEDs - Larson-scanner style chasing pattern De-bounch a button (or switch) - No need for de-bouncing capacitor Delayed events after a button push - Timed events (button push is just example.) analogWrite() PWM Fading - No delay() and a simple function to keep a LED fading with PWM Detect Short and Long Button Press - Give one button multiple functions You might also want to check out my “Blink Without Delay - Line by Line Tutorial.” It is a much more in-depth explanation than the comments provided with the Arduino IDE example.</description></item><item><title>A week of Arduino Millis()</title><link>https://www.baldengineer.com/a-week-of-arduino-millis.html</link><pubDate>Mon, 09 Dec 2013 13:25:24 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=2079</guid><description>By far one of the most powerful functions of the Arduino library is the millis() counter. It is also one of the most misunderstood functions. Mastering millis() can unlock incredible amounts of potential with an Arduino. That’s why I’m spending an entire week on millis().&#10;Each day this week I’m posting a new example of how to use millis() in your code. Each will be cataloged on this newly created Arduino Millis() Cookbook Page. If you have ideas for other examples, leave comments below.<p><img src="https://www.baldengineer.com/wp-content/uploads/2012/09/a-week-of-millis-banner.png" alt="A week of Arduino Millis()"/></p></description></item><item><title>Arduino Example: Police Lights with millis()</title><link>https://www.baldengineer.com/arduino-example-police-lights-with-millis.html</link><pubDate>Sat, 28 Jul 2012 18:00:20 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?page_id=1121</guid><description>Based on a question from Andrew on the initial Multitasking with millis() tutorial, this example shows how to create a Police-Light like strobe effect. The following code uses no calls to delay().&#10;Code also available via Pastebin.org.&#10;// English for which LED to Strobe #define RED 0x0 #define BLUE 0x1 // Variable to track which LED is on byte whichLED = RED; // Where are the LEDs connected? const int LED_Red = 7; const int LED_Blue = 11; // State variables for the LEDs byte Red_State = LOW; byte Blue_State = LOW; // Some delay values to change flashing behavior unsigned long switchDelay = 250; unsigned long strobeDelay = 50; // Seed the initial wait for the strobe effect unsigned long strobeWait = strobeDelay; // Variable to see when we should swtich LEDs unsigned long waitUntilSwitch = switchDelay; // seed initial wait void setup() { pinMode(LED_Red, OUTPUT); pinMode(LED_Blue, OUTPUT); } void loop() { digitalWrite(LED_Red, Red_State); // each iteration of loop() will set the IO pins, digitalWrite(LED_Blue, Blue_State); // even if they don't change, that's okay // Toggle back and forth between the two LEDs if ((millis() - waitUntilSwitch) &gt;= 0) { // time is up! Red_State = LOW; Blue_State = LOW; whichLED = !whichLED; // toggle LED to strobe waitUntilSwitch += switchDelay; } // Create the stobing effect if ((millis() - strobeWait) &gt;=0 ) { if (whichLED == RED) Red_State = !Red_State; if (whichLED == BLUE) Blue_State = !Blue_State; strobeWait += strobeDelay; } } State Variables Red_State and Blue_State Instead of using digitalWrite() directly, “state” variables are used to determine the state (on or off) of each LED. This allows logic inside the loop() to determine when the lights should be turned on or off. Each iteration of loop() will update the LEDs with a digitalWrite() based on this state variable.</description></item><item><title>Arduino: How do you reset millis() ?</title><link>https://www.baldengineer.com/arduino-how-do-you-reset-millis.html</link><pubDate>Mon, 16 Jul 2012 14:00:42 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=861</guid><description>The quick answer to “How do you reset millis()” is: You Don’t! And here’s why: if you did, it would potentially break most libraries and functions that rely on it. Generally the reason people want to reset it, is that they are concerned about rollover. Instead of focusing on resetting millis(), here is how to use it correctly.&#10;Need to brush up on how millis() works? I’ve got a tutorial on how to effectively multi-task with millis() and another line-by-line tutorial on blink Without delay Avoiding rollover and checking how much time as passed is done in a single line:<p><img src="https://www.baldengineer.com/wp-content/uploads/2012/07/stopwatch_000018042439.jpg" alt="Arduino:  How do you reset millis() ?"/></p></description></item><item><title>millis() Tutorial: Arduino Multitasking</title><link>https://www.baldengineer.com/millis-tutorial.html</link><pubDate>Fri, 07 Jan 2011 04:01:55 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=294</guid><description>After working through these exercises, check out this article on how to avoid rollover or reset mills(). After learning how to flash a single LED on your Arduino, you are probably looking for a way to make cool patterns, but feel limited by the use of delay(). If you ask in the forums, you get told to look at the “Blink Without Delay” example. This example introduces the idea of replacing delay() with a state machine. If you’re confused how to use it, this tutorial is setup to take you from blinking two LEDs with delay, to using an alternate method, right down to how you can use millis().<p><img src="https://www.baldengineer.com/wp-content/uploads/2011/01/eca9168a883497e1bb33bd3a47ba1baa-300x197.png" alt="millis() Tutorial: Arduino Multitasking"/></p></description></item></channel></rss>