{"id":78,"date":"2019-10-22T08:01:40","date_gmt":"2019-10-22T08:01:40","guid":{"rendered":"https:\/\/starthardware.org\/en\/?p=78"},"modified":"2019-10-22T08:01:41","modified_gmt":"2019-10-22T08:01:41","slug":"unit-10-for-loop-and-the-led-calculator","status":"publish","type":"post","link":"https:\/\/starthardware.org\/en\/unit-10-for-loop-and-the-led-calculator\/","title":{"rendered":"Unit 10 \u2013 For-Loop and the LED calculator"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>A great thing in programming are loops like the for-loop. It repeats tasks like blinking an LED a specific number.<\/p><\/blockquote>\n\n\n\n<p>For the following project, you can still use the code from the <a href=\"https:\/\/starthardware.org\/en\/2019\/10\/22\/unit-8-its-alive\/\">previous unit<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"800\" src=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-circuit-led-blink-1024x800.gif\" alt=\"Arduino Blink Circuit\" class=\"wp-image-60\" srcset=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-circuit-led-blink-1024x800.gif 1024w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-circuit-led-blink-300x235.gif 300w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-circuit-led-blink-768x600.gif 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The for-loop is repeated for a specific number. It looks like that:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (StartValue; Test; FollowUp){ \n\n}<\/code><\/pre>\n\n\n\n<p>We can simply put the commands which should be repeated into the curly brackets. This should be the blinking of our LED:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (StartValue; Test; FollowUp){ \n  digitalWrite(ledPin, HIGH);\n  delay(200);\n  digitalWrite(ledPin, LOW);\n  delay(200);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">StartValue<\/h3>\n\n\n\n<p>For the start value, we create a new variable. Fortunately, we can simply do that in the same line:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>for (int counter=1; Test; FollowUp){<\/code><\/pre>\n\n\n\n<p>So, it is an integer variable (<em>int<\/em>) with the name&nbsp;<em>counter<\/em>&nbsp;which equals 1.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">FollowUp<\/h3>\n\n\n\n<p>Every time the for-loop is repeated the variable&nbsp;<em>counter<\/em>&nbsp;should be increased by 1. So we write as FollowUp:<em>counter = counter +1<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>for (int counter=1; Test; counter = counter+1){<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Test<\/h3>\n\n\n\n<p>The LED should blink for three times and we have a variable which counts up every time the LED blinks. Somehow we have to check, if our variable larger then 3 to stop the for-loop. No problem! That is what the Test is for:&nbsp;<em>counter&lt;4<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>for (int counter=1; counter&lt;4; counter = counter+1){<\/code><\/pre>\n\n\n\n<p>Now we only have to ad a long break after our for-loop. Otherwise the program would directly start over and we wouldn\u2019t see a change. Let\u2019s put in a break of 2 seconds. You already know, how to to this, no? \ud83d\ude42<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int ledPin = 9;\n\nvoid setup(){\n  pinMode(ledPin,OUTPUT);\n}\n\nvoid loop(){\n  for (int counter=1; counter&lt;4; counter = counter+1){\n    digitalWrite(ledPin, HIGH);\n    delay(200);\n    digitalWrite(ledPin, LOW);\n    delay(200);\n  }\n  delay(2000);\n}<\/code><\/pre>\n\n\n\n<p>Upload it to your Arduino.<\/p>\n\n\n\n<p>Does it work? Perfekt! It blinks three times and than it stops for two seconds. Change the number in the Test-Argument to 10. What happens?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int ledPin = 9;\n\nvoid setup(){\n  pinMode(ledPin,OUTPUT);\n}\n\nvoid loop(){\n  for (int counter=1; counter&lt;10; counter = counter+1){\n    digitalWrite(ledPin, HIGH);\n    delay(200);\n    digitalWrite(ledPin, LOW);\n    delay(200);\n  }\n  delay(2000);\n}<\/code><\/pre>\n\n\n\n<p>Hm, it just blinks nine times. It is because&nbsp;<em>less than<\/em>&nbsp;means 1 to 9, not 1 to 10. Change the&nbsp;<em>less than (&lt;)<\/em>&nbsp;to&nbsp;<em>less or equal (&lt;=)<\/em>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int ledPin = 9;\n\nvoid setup(){\n  pinMode(ledPin,OUTPUT);\n}\n\nvoid loop(){\n  for (int counter=1; counter&lt;=10; counter = counter+1){\n    digitalWrite(ledPin, HIGH);\n    delay(200);\n    digitalWrite(ledPin, LOW);\n    delay(200);\n  }\n  delay(2000);\n}<\/code><\/pre>\n\n\n\n<p>Now it blinks ten times, no? Let\u2019s build a little calculator with it. At first, we create the variable&nbsp;<em>myResult<\/em>&nbsp;and send it to the for-loop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int ledPin = 9;\nint myResult;\n\nvoid setup(){\n  pinMode(ledPin,OUTPUT);\n}\n\nvoid loop(){\n  for (int counter=1; counter&lt;=myResult; counter = counter+1){\n    digitalWrite(ledPin, HIGH);\n    delay(200);\n    digitalWrite(ledPin, LOW);\n    delay(200);\n  }\n  delay(2000);\n}<\/code><\/pre>\n\n\n\n<p>And now we need the program to do the calculation. I really don\u2019t want to do everything by my own \ud83d\ude09<br>Let\u2019s start with: 2 + 3<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int ledPin = 9;\nint myResult;\n\nvoid setup(){\n  pinMode(ledPin,OUTPUT);\n}\n\nvoid loop(){\n  myResult = 2 + 3;\n  for (int counter=1; counter&lt;=myResult; counter = counter+1){\n    digitalWrite(ledPin, HIGH);\n    delay(200);\n    digitalWrite(ledPin, LOW);\n    delay(200);\n  }\n  delay(2000);\n}<\/code><\/pre>\n\n\n\n<p>Upload the program to your Arduino and count the numbers of blinks. Now, calculate some other problems like 4 + 7 or 329 + 12. Well done!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A great thing in programming are loops like the for-loop. It repeats tasks like blinking an LED a specific number. For the following project, you can still use the code from the previous unit. The for-loop is repeated for a specific number. It looks like that: We can simply put the commands which should be&hellip;&nbsp;<a href=\"https:\/\/starthardware.org\/en\/unit-10-for-loop-and-the-led-calculator\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Unit 10 \u2013 For-Loop and the LED calculator<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":80,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[2],"tags":[],"class_list":["post-78","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v18.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Unit 10 \u2013 For-Loop and the LED calculator - StartHardware - Tutorials for Arduino<\/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:\/\/starthardware.org\/en\/unit-10-for-loop-and-the-led-calculator\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Unit 10 \u2013 For-Loop and the LED calculator - StartHardware - Tutorials for Arduino\" \/>\n<meta property=\"og:description\" content=\"A great thing in programming are loops like the for-loop. It repeats tasks like blinking an LED a specific number. For the following project, you can still use the code from the previous unit. The for-loop is repeated for a specific number. It looks like that: We can simply put the commands which should be&hellip;&nbsp;Read More &raquo;Unit 10 \u2013 For-Loop and the LED calculator\" \/>\n<meta property=\"og:url\" content=\"https:\/\/starthardware.org\/en\/unit-10-for-loop-and-the-led-calculator\/\" \/>\n<meta property=\"og:site_name\" content=\"StartHardware - Tutorials for Arduino\" \/>\n<meta property=\"article:published_time\" content=\"2019-10-22T08:01:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-10-22T08:01:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-led-calculator.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1340\" \/>\n\t<meta property=\"og:image:height\" content=\"754\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Stefan Hermann\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/starthardware.org\/en\/#website\",\"url\":\"https:\/\/starthardware.org\/en\/\",\"name\":\"StartHardware - Tutorials for Arduino\",\"description\":\"Arduino, Electronics, Fun\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/starthardware.org\/en\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/starthardware.org\/en\/unit-10-for-loop-and-the-led-calculator\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-led-calculator.jpg\",\"contentUrl\":\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-led-calculator.jpg\",\"width\":1340,\"height\":754,\"caption\":\"Arduino LED Calculator\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/starthardware.org\/en\/unit-10-for-loop-and-the-led-calculator\/#webpage\",\"url\":\"https:\/\/starthardware.org\/en\/unit-10-for-loop-and-the-led-calculator\/\",\"name\":\"Unit 10 \u2013 For-Loop and the LED calculator - StartHardware - Tutorials for Arduino\",\"isPartOf\":{\"@id\":\"https:\/\/starthardware.org\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/starthardware.org\/en\/unit-10-for-loop-and-the-led-calculator\/#primaryimage\"},\"datePublished\":\"2019-10-22T08:01:40+00:00\",\"dateModified\":\"2019-10-22T08:01:41+00:00\",\"author\":{\"@id\":\"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59\"},\"breadcrumb\":{\"@id\":\"https:\/\/starthardware.org\/en\/unit-10-for-loop-and-the-led-calculator\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/starthardware.org\/en\/unit-10-for-loop-and-the-led-calculator\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/starthardware.org\/en\/unit-10-for-loop-and-the-led-calculator\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/starthardware.org\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Unit 10 \u2013 For-Loop and the LED calculator\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59\",\"name\":\"Stefan Hermann\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/starthardware.org\/en\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5b5a74ee1d07024fd1eff9b1f7137108089169010a93afaee907b9325ee579a6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5b5a74ee1d07024fd1eff9b1f7137108089169010a93afaee907b9325ee579a6?s=96&d=mm&r=g\",\"caption\":\"Stefan Hermann\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Unit 10 \u2013 For-Loop and the LED calculator - StartHardware - Tutorials for Arduino","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:\/\/starthardware.org\/en\/unit-10-for-loop-and-the-led-calculator\/","og_locale":"en_US","og_type":"article","og_title":"Unit 10 \u2013 For-Loop and the LED calculator - StartHardware - Tutorials for Arduino","og_description":"A great thing in programming are loops like the for-loop. It repeats tasks like blinking an LED a specific number. For the following project, you can still use the code from the previous unit. The for-loop is repeated for a specific number. It looks like that: We can simply put the commands which should be&hellip;&nbsp;Read More &raquo;Unit 10 \u2013 For-Loop and the LED calculator","og_url":"https:\/\/starthardware.org\/en\/unit-10-for-loop-and-the-led-calculator\/","og_site_name":"StartHardware - Tutorials for Arduino","article_published_time":"2019-10-22T08:01:40+00:00","article_modified_time":"2019-10-22T08:01:41+00:00","og_image":[{"width":1340,"height":754,"url":"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-led-calculator.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Written by":"Stefan Hermann","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/starthardware.org\/en\/#website","url":"https:\/\/starthardware.org\/en\/","name":"StartHardware - Tutorials for Arduino","description":"Arduino, Electronics, Fun","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/starthardware.org\/en\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https:\/\/starthardware.org\/en\/unit-10-for-loop-and-the-led-calculator\/#primaryimage","inLanguage":"en-US","url":"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-led-calculator.jpg","contentUrl":"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-led-calculator.jpg","width":1340,"height":754,"caption":"Arduino LED Calculator"},{"@type":"WebPage","@id":"https:\/\/starthardware.org\/en\/unit-10-for-loop-and-the-led-calculator\/#webpage","url":"https:\/\/starthardware.org\/en\/unit-10-for-loop-and-the-led-calculator\/","name":"Unit 10 \u2013 For-Loop and the LED calculator - StartHardware - Tutorials for Arduino","isPartOf":{"@id":"https:\/\/starthardware.org\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/starthardware.org\/en\/unit-10-for-loop-and-the-led-calculator\/#primaryimage"},"datePublished":"2019-10-22T08:01:40+00:00","dateModified":"2019-10-22T08:01:41+00:00","author":{"@id":"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59"},"breadcrumb":{"@id":"https:\/\/starthardware.org\/en\/unit-10-for-loop-and-the-led-calculator\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/starthardware.org\/en\/unit-10-for-loop-and-the-led-calculator\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/starthardware.org\/en\/unit-10-for-loop-and-the-led-calculator\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/starthardware.org\/en\/"},{"@type":"ListItem","position":2,"name":"Unit 10 \u2013 For-Loop and the LED calculator"}]},{"@type":"Person","@id":"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59","name":"Stefan Hermann","image":{"@type":"ImageObject","@id":"https:\/\/starthardware.org\/en\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/5b5a74ee1d07024fd1eff9b1f7137108089169010a93afaee907b9325ee579a6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5b5a74ee1d07024fd1eff9b1f7137108089169010a93afaee907b9325ee579a6?s=96&d=mm&r=g","caption":"Stefan Hermann"}}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts\/78","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/comments?post=78"}],"version-history":[{"count":1,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts\/78\/revisions"}],"predecessor-version":[{"id":79,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts\/78\/revisions\/79"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/media\/80"}],"wp:attachment":[{"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/media?parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/categories?post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/tags?post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}