{"id":1684,"date":"2020-09-10T11:10:43","date_gmt":"2020-09-10T09:10:43","guid":{"rendered":"https:\/\/www.pschatzmann.ch\/home\/?p=1684"},"modified":"2021-12-14T15:22:13","modified_gmt":"2021-12-14T14:22:13","slug":"arduino-blinking-sketch","status":"publish","type":"post","link":"https:\/\/www.pschatzmann.ch\/home\/2020\/09\/10\/arduino-blinking-sketch\/","title":{"rendered":"How to Blink in Arduino &#8211; Alternative Designs"},"content":{"rendered":"<p>if you google for <strong>&#8220;Arduino LED blinking sketch&#8221;<\/strong> you might find something like this:<\/p>\n<pre><code>#define ONBOARD_LED  2\n\nvoid setup() {\n  pinMode(ONBOARD_LED,OUTPUT);\n}\n\nvoid loop() {\n  delay(1000);\n  digitalWrite(ONBOARD_LED,HIGH);\n  delay(100);\n  digitalWrite(ONBOARD_LED,LOW);\n}\n<\/code><\/pre>\n<p>(I&#8217;am using an ESP32 and therefore I needed to define the ONBOARD_LED because this is not predefined in Arduino.h)<\/p>\n<p>This is basically working but it adds a <strong>delay of 1.1 seconds<\/strong> to the loop which might be pretty <span>disastrous<\/span> if you you have some other <strong>time critical functionality<\/strong> which needs to happen in the loop as well.<\/p>\n<p>So a less intrusive implementation is using the time to figure out if led needs to be switched on or off:<\/p>\n<pre><code><br \/>#define ONBOARD_LED  2\n\nbool led_state = true;\nint led_interval = 1000;\nlong led_timeout;\n\nvoid setup() {\n  pinMode(ONBOARD_LED, OUTPUT);\n}\n\nvoid loop() {\n  \/\/ update led\n  if (led_interval&gt;0 &amp;&amp; millis()&gt;led_timeout){\n     led_state = !led_state;\n     led_timeout = millis()+led_interval;\n     digitalWrite(ONBOARD_LED, led_state);\n  }\n}\n\n<\/code><\/pre>\n<p>You might think about abstracting the functionality in a separate class<\/p>\n<pre><code>\/\/ content of LED.h\n\nclass LED {\n  private:\n    int pin;\n    bool state;\n    int interval;\n    long timeout;\n\n  public:\n    LED(int pin=2) {\n      this-&gt;pin = pin;\n      pinMode(pin, OUTPUT);\n    };\n\n    void setState(boolean on) {\n        this-&gt;state = on;\n        this-&gt;timeout = 0;\n        digitalWrite(pin,state);\n    };\n\n    bool getState() {\n      return state;\n    };\n\n    void setBlinking(int ms) {\n        this-&gt;interval = ms;\n        this-&gt;state = false;\n        this-&gt;timeout = millis()+interval;\n    };\n\n    void update() {\n         if (timeout&gt;0 &amp;&amp; millis()&gt;timeout){\n            state = !state;\n            timeout = millis()+interval;\n            digitalWrite(pin,state);\n         }\n    };\n\n};\n<\/code><\/pre>\n<p>And then your blinking sketch gets pretty small:<\/p>\n<pre><code>#include \"LED.h\"\n#define ONBOARD_LED  2\n\nLED led(ONBOARD_LED);\n\nvoid setup() {\n  led.setBlinking(1000);\n}\n\nvoid loop() {\n  led.update();\n}\n\n<\/code><\/pre>\n<h2>Summary<\/h2>\n<p>I like the concept of using separate classes to abstract the complexity in Arduino Sketches. It also helps to make the Sketches more readable.<\/p>\n<p>In standard C++ it is required that you split the declaration (which is in the .h file) from the implementation (which is in .cpp files). Above I used the shortcut and added the implementation directly into the .h file.<\/p>\n<p>I think that in a <strong>Arduino Project<\/strong> you might get away with this. You will definitely need to do this split if you want to publish your code as Arduino Library<\/p>\n","protected":false},"excerpt":{"rendered":"<p>if you google for &#8220;Arduino LED blinking sketch&#8221; you might find something like this: #define ONBOARD_LED 2 void setup() { pinMode(ONBOARD_LED,OUTPUT); } void loop() { delay(1000); digitalWrite(ONBOARD_LED,HIGH); delay(100); digitalWrite(ONBOARD_LED,LOW); } (I&#8217;am using an ESP32 and therefore I needed to define the ONBOARD_LED because this is not predefined in Arduino.h) This [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1699,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[20],"tags":[30],"class_list":["post-1684","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino","tag-back-to-basics"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Blink in Arduino - Alternative Designs - Phil Schatzmann<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pschatzmann.ch\/home\/2020\/09\/10\/arduino-blinking-sketch\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Blink in Arduino - Alternative Designs - Phil Schatzmann\" \/>\n<meta property=\"og:description\" content=\"if you google for &#8220;Arduino LED blinking sketch&#8221; you might find something like this: #define ONBOARD_LED 2 void setup() { pinMode(ONBOARD_LED,OUTPUT); } void loop() { delay(1000); digitalWrite(ONBOARD_LED,HIGH); delay(100); digitalWrite(ONBOARD_LED,LOW); } (I&#8217;am using an ESP32 and therefore I needed to define the ONBOARD_LED because this is not predefined in Arduino.h) This [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pschatzmann.ch\/home\/2020\/09\/10\/arduino-blinking-sketch\/\" \/>\n<meta property=\"og:site_name\" content=\"Phil Schatzmann\" \/>\n<meta property=\"article:published_time\" content=\"2020-09-10T09:10:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-14T14:22:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2020\/09\/blink.gif\" \/>\n\t<meta property=\"og:image:width\" content=\"200\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/gif\" \/>\n<meta name=\"author\" content=\"pschatzmann\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"pschatzmann\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2020\\\/09\\\/10\\\/arduino-blinking-sketch\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2020\\\/09\\\/10\\\/arduino-blinking-sketch\\\/\"},\"author\":{\"name\":\"pschatzmann\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"headline\":\"How to Blink in Arduino &#8211; Alternative Designs\",\"datePublished\":\"2020-09-10T09:10:43+00:00\",\"dateModified\":\"2021-12-14T14:22:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2020\\\/09\\\/10\\\/arduino-blinking-sketch\\\/\"},\"wordCount\":216,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2020\\\/09\\\/10\\\/arduino-blinking-sketch\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2020\\\/09\\\/blink.gif\",\"keywords\":[\"Back to Basics\"],\"articleSection\":[\"Arduino\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2020\\\/09\\\/10\\\/arduino-blinking-sketch\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2020\\\/09\\\/10\\\/arduino-blinking-sketch\\\/\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2020\\\/09\\\/10\\\/arduino-blinking-sketch\\\/\",\"name\":\"How to Blink in Arduino - Alternative Designs - Phil Schatzmann\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2020\\\/09\\\/10\\\/arduino-blinking-sketch\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2020\\\/09\\\/10\\\/arduino-blinking-sketch\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2020\\\/09\\\/blink.gif\",\"datePublished\":\"2020-09-10T09:10:43+00:00\",\"dateModified\":\"2021-12-14T14:22:13+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2020\\\/09\\\/10\\\/arduino-blinking-sketch\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2020\\\/09\\\/10\\\/arduino-blinking-sketch\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2020\\\/09\\\/10\\\/arduino-blinking-sketch\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2020\\\/09\\\/blink.gif\",\"contentUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2020\\\/09\\\/blink.gif\",\"width\":200,\"height\":200},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2020\\\/09\\\/10\\\/arduino-blinking-sketch\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Blink in Arduino &#8211; Alternative Designs\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#website\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/\",\"name\":\"Phil Schatzmann Consulting\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\",\"name\":\"pschatzmann\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"contentUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"width\":305,\"height\":305,\"caption\":\"pschatzmann\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Blink in Arduino - Alternative Designs - Phil Schatzmann","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pschatzmann.ch\/home\/2020\/09\/10\/arduino-blinking-sketch\/","og_locale":"en_US","og_type":"article","og_title":"How to Blink in Arduino - Alternative Designs - Phil Schatzmann","og_description":"if you google for &#8220;Arduino LED blinking sketch&#8221; you might find something like this: #define ONBOARD_LED 2 void setup() { pinMode(ONBOARD_LED,OUTPUT); } void loop() { delay(1000); digitalWrite(ONBOARD_LED,HIGH); delay(100); digitalWrite(ONBOARD_LED,LOW); } (I&#8217;am using an ESP32 and therefore I needed to define the ONBOARD_LED because this is not predefined in Arduino.h) This [&hellip;]","og_url":"https:\/\/www.pschatzmann.ch\/home\/2020\/09\/10\/arduino-blinking-sketch\/","og_site_name":"Phil Schatzmann","article_published_time":"2020-09-10T09:10:43+00:00","article_modified_time":"2021-12-14T14:22:13+00:00","og_image":[{"width":200,"height":200,"url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2020\/09\/blink.gif","type":"image\/gif"}],"author":"pschatzmann","twitter_card":"summary_large_image","twitter_misc":{"Written by":"pschatzmann","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pschatzmann.ch\/home\/2020\/09\/10\/arduino-blinking-sketch\/#article","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2020\/09\/10\/arduino-blinking-sketch\/"},"author":{"name":"pschatzmann","@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"headline":"How to Blink in Arduino &#8211; Alternative Designs","datePublished":"2020-09-10T09:10:43+00:00","dateModified":"2021-12-14T14:22:13+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2020\/09\/10\/arduino-blinking-sketch\/"},"wordCount":216,"commentCount":1,"publisher":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"image":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2020\/09\/10\/arduino-blinking-sketch\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2020\/09\/blink.gif","keywords":["Back to Basics"],"articleSection":["Arduino"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pschatzmann.ch\/home\/2020\/09\/10\/arduino-blinking-sketch\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pschatzmann.ch\/home\/2020\/09\/10\/arduino-blinking-sketch\/","url":"https:\/\/www.pschatzmann.ch\/home\/2020\/09\/10\/arduino-blinking-sketch\/","name":"How to Blink in Arduino - Alternative Designs - Phil Schatzmann","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2020\/09\/10\/arduino-blinking-sketch\/#primaryimage"},"image":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2020\/09\/10\/arduino-blinking-sketch\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2020\/09\/blink.gif","datePublished":"2020-09-10T09:10:43+00:00","dateModified":"2021-12-14T14:22:13+00:00","breadcrumb":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2020\/09\/10\/arduino-blinking-sketch\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pschatzmann.ch\/home\/2020\/09\/10\/arduino-blinking-sketch\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pschatzmann.ch\/home\/2020\/09\/10\/arduino-blinking-sketch\/#primaryimage","url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2020\/09\/blink.gif","contentUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2020\/09\/blink.gif","width":200,"height":200},{"@type":"BreadcrumbList","@id":"https:\/\/www.pschatzmann.ch\/home\/2020\/09\/10\/arduino-blinking-sketch\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pschatzmann.ch\/home\/"},{"@type":"ListItem","position":2,"name":"How to Blink in Arduino &#8211; Alternative Designs"}]},{"@type":"WebSite","@id":"https:\/\/www.pschatzmann.ch\/home\/#website","url":"https:\/\/www.pschatzmann.ch\/home\/","name":"Phil Schatzmann Consulting","description":"","publisher":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pschatzmann.ch\/home\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1","name":"pschatzmann","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","contentUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","width":305,"height":305,"caption":"pschatzmann"},"logo":{"@id":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png"}}]}},"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/1684","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/comments?post=1684"}],"version-history":[{"count":25,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/1684\/revisions"}],"predecessor-version":[{"id":1737,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/1684\/revisions\/1737"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media\/1699"}],"wp:attachment":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media?parent=1684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/categories?post=1684"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/tags?post=1684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}