{"id":13880,"date":"2017-04-01T18:19:19","date_gmt":"2017-04-01T22:19:19","guid":{"rendered":"http:\/\/scruss.com\/blog\/?p=13880"},"modified":"2017-04-01T18:20:53","modified_gmt":"2017-04-01T22:20:53","slug":"simple-like-really-simple-arduino-periodic-timer-with-bretts-millistimer-library","status":"publish","type":"post","link":"https:\/\/scruss.com\/blog\/2017\/04\/01\/simple-like-really-simple-arduino-periodic-timer-with-bretts-millistimer-library\/","title":{"rendered":"Simple \u00e2\u20ac\u201d like, really simple \u00e2\u20ac\u201d Arduino periodic timer with Brett&#8217;s MillisTimer library"},"content":{"rendered":"<p>I don&#8217;t know how many times I&#8217;ve written bad Arduino code to call a function every few milliseconds. Sometimes this bad code works well enough for my sketch to actually work. Often, it either doesn&#8217;t work at all or does something I really didn&#8217;t expect.<\/p>\n<p>So on <a href=\"https:\/\/day.arduino.cc\/\">Arduino Day 2017<\/a>, I&#8217;m glad I found out about <a href=\"https:\/\/github.com\/bhagman\/MillisTimer\">bhagman\/MillisTimer: A Wiring and Arduino library for working with millis().<\/a> It couldn&#8217;t be simpler to use: include the library, write the function you want to call every <em>N<\/em> milliseconds, set up the timer to run every <em>N<\/em> millis, and put <code>timer.run()<\/code> in a loop that&#8217;s called frequently. The library handles the timing and resetting all by itself.<\/p>\n<p>As an example, here&#8217;s the eternal \u00e2\u20ac\u0153Hello, World!\u00e2\u20ac\u009d of the embedded world, <a href=\"https:\/\/www.arduino.cc\/en\/Tutorial\/Blink\">Blink<\/a>, rewritten to use MillisTimer:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/\/ MillisTimerBlink - blink LED every second\r\n\/\/  using Brett Hagman's MillisTimer library\r\n\/\/  https:\/\/github.com\/bhagman\/MillisTimer\r\n\/\/  (or use Sketch \u00e2\u2020\u2019 Include Library \u00e2\u2020\u2019 Manage Libraries \u00e2\u20ac\u00a6 to install)\r\n\/\/ scruss - 2017-04-01\r\n\r\n#include &lt;MillisTimer.h&gt;\r\nMillisTimer timer1;               \/\/ new empty timer object\r\nconst int led_pin = LED_BUILTIN;  \/\/ use the built-in LED\r\n\r\nvoid flash() {                    \/\/ function called by timer\r\n  static boolean output = HIGH;\r\n  digitalWrite(led_pin, output);  \/\/ set LED on or off\r\n  output = !output;               \/\/ toggle variable state High\/Low\r\n}\r\n\r\nvoid setup() {\r\n  pinMode(led_pin, OUTPUT);       \/\/ use built-in LED for output\r\n  timer1.setInterval(1000);       \/\/ set timer to trigger every 1000 millis\r\n  timer1.expiredHandler(flash);   \/\/ call flash() function when timer runs out\r\n  timer1.setRepeats(0);           \/\/ repeat forever if set to 0\r\n  timer1.start();                 \/\/ start the timer when the sketch starts\r\n}\r\n\r\nvoid loop() {\r\n  timer1.run();                   \/\/ trigger the timer only if it has run out\r\n  \/\/ note that run() has to be called more frequently than the timer interval\r\n  \/\/  or timings will not be accurate\r\n}\r\n<\/pre>\n<p>Note that MillisTimer only triggers when <code>timer.run()<\/code> is called. Sticking a <code>delay(2000)<\/code> in the main loop will cause it to fire far less frequently than the interval you set. So it&#8217;s not technically a true periodic timer, but is good enough for most of my purposes. If you want a true interrupt-driven timer, use the <a href=\"http:\/\/playground.arduino.cc\/Main\/MsTimer2\">MsTimer2<\/a> library. It relies on the timer interrupts built into the Arduino hardware, and isn&#8217;t quite as easy to use as MillisTimer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I don&#8217;t know how many times I&#8217;ve written bad Arduino code to call a function every few milliseconds. Sometimes this bad code works well enough for my sketch to actually work. Often, it either doesn&#8217;t work at all or does something I really didn&#8217;t expect. So on Arduino Day 2017, I&#8217;m glad I found out [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7],"tags":[2207,3048,2573],"class_list":["post-13880","post","type-post","status-publish","format-standard","hentry","category-computers-suck","tag-arduino","tag-arduinod17","tag-timer"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pQNZZ-3BS","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/13880","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/comments?post=13880"}],"version-history":[{"count":4,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/13880\/revisions"}],"predecessor-version":[{"id":13883,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/13880\/revisions\/13883"}],"wp:attachment":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/media?parent=13880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/categories?post=13880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/tags?post=13880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}