<?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/category/tutorials/arduino/millis</link><description>Recent content in millis() on Bald Engineer</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Sun, 03 Apr 2022 00:05:55 +0000</lastBuildDate><atom:link href="https://www.baldengineer.com/category/tutorials/arduino/millis/index.xml" rel="self" type="application/rss+xml"/><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>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: Chasing LEDs with millis()</title><link>https://www.baldengineer.com/arduino-chasing-leds-with-millis.html</link><pubDate>Thu, 12 Dec 2013 12:00:34 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=2117</guid><description>A popular LED project is the “Larson Scanner.” This scanner emulates the effect seen on KIT from Knight Rider and the Cylons in Battlestar Galactica. The code is usually written using “delay()” which means you can’t combine it with anything else. The following code could be put into a function, called periodically and allow your code to scan while doing other things.&#10;One thing to note, a proper Larson Scanner has some persistence to the next and previous LEDs, this code does not. Maybe that would be a good exercise for the reader?<p><img src="https://www.baldengineer.com/wp-content/uploads/2012/09/millis-chasing-led-larson-scanner-banner.png" alt="Arduino:  Chasing LEDs with millis()"/></p></description></item><item><title>Arduino: Timing Code with Millis() as a Stopwatch</title><link>https://www.baldengineer.com/arduino-timing-code-with-millis-as-a-stopwatch.html</link><pubDate>Wed, 11 Dec 2013 12:25:22 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=2085</guid><description>Since most Arduino boards do not have debug capability, this limits the programmer to using Serial.prints. A useful piece of information might be knowing how long certain parts of code are running. Here’s a simple example that demonstrations:&#10;How to properly use Serial.flush() (hint: it’s for TRANSMIT, not RECEIVE!) How long Serial.print()s can “tie up” the Arduino. At 9600 baud, you’ll see that the Arduino is only busy for about 20 milliseconds in the first chunk of code, but busy for 93 milliseconds in the next. That’s because the Arduino (since 1.0) started using a transmit-buffer. Did you know your code doesn’t wait for a Serial.print to finish?<p><img src="https://www.baldengineer.com/wp-content/uploads/2012/09/millis-code-stopwatch-banner3.png" alt="Arduino:  Timing Code with Millis() as a Stopwatch"/></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>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>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>