{"id":72,"date":"2019-10-22T07:44:47","date_gmt":"2019-10-22T07:44:47","guid":{"rendered":"https:\/\/starthardware.org\/en\/?p=72"},"modified":"2019-10-22T15:34:09","modified_gmt":"2019-10-22T15:34:09","slug":"unit-9-how-to-use-variables","status":"publish","type":"post","link":"https:\/\/starthardware.org\/en\/unit-9-how-to-use-variables\/","title":{"rendered":"Unit 9 \u2013 How to use Variables"},"content":{"rendered":"\n<p>A very important thing in programming are variables. I guess you already know it from math class. If you say x=10 it means that everywhere x can be replaced by 10.<\/p>\n\n\n\n<p>Thing of variables as little boxes or containers in which we can store information like numbers.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1280\" height=\"299\" src=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-variables.png\" alt=\"Arduino Variables\" class=\"wp-image-73\" srcset=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-variables.png 1280w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-variables-300x70.png 300w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-variables-768x179.png 768w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-variables-1024x239.png 1024w\" sizes=\"auto, (max-width: 1280px) 100vw, 1280px\" \/><\/figure>\n\n\n\n<p>You can choose your own variable name. A variable could read Tim, Sarah or Eagles. Of course it makes more sense to name after it\u2019s purpose.<\/p>\n\n\n\n<p>Let\u2019s see the programming code of the last example, again:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void setup(){\n  pinMode(9,OUTPUT);\n}\n\nvoid loop(){\n  digitalWrite(9, HIGH);\n  delay(200);\n  digitalWrite(9, LOW);\n  delay(200);\n}<\/code><\/pre>\n\n\n\n<p>The LED was connected to pin 9. Because of that, every command here gets a 9. But what would happen, if we would connect it to pin 8?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Circuit<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"934\" src=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/variable-used.jpg\" alt=\"Arduino Variable used to assign another digital pin\" class=\"wp-image-74\" srcset=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/variable-used.jpg 1200w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/variable-used-300x234.jpg 300w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/variable-used-768x598.jpg 768w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/variable-used-1024x797.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n\n\n\n<p>Of course, we would need to change every entry in the commands.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void setup(){\n  pinMode(8,OUTPUT);\n}\n\nvoid loop(){\n  digitalWrite(8, HIGH);\n  delay(200);\n  digitalWrite(8, LOW);\n  delay(200);\n}<\/code><\/pre>\n\n\n\n<p>Well, in this short example it is not a big deal, but if you are writing larger programs, it could happen, that you forget a change. It would be better to just use a variable.<\/p>\n\n\n\n<p>This variable should store the pin number of the LED, so let\u2019s name it ledPin. The capital letter for the new word makes it easier to read. Variable names must not contain blanks.<\/p>\n\n\n\n<p>This is how we create a variable (Programmers call it we declare it):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>int ledPin = 8;<\/code><\/pre>\n\n\n\n<p>The word int is short for integer. That means that this variable will only contain countable numbers like 1, 2, 3, 4 \u2026 When we create a variable we always have to tell Arduino, of what type it is.<\/p>\n\n\n\n<p>We need the variable to be 8, because this is the pin where our LED is connected to. The equal sign sets our variable to 8.<\/p>\n\n\n\n<p>Now, we declared an integer variable with the name ledPin and we set it to 8. Everywhere, where we now write ledPin, it is replaced by 8.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int ledPin = 8;\n\nvoid setup(){\n  pinMode(ledPin,OUTPUT);\n}\n\nvoid loop(){\n  digitalWrite(ledPin, HIGH);\n  delay(200);\n  digitalWrite(ledPin, LOW);\n  delay(200);\n}<\/code><\/pre>\n\n\n\n<p>Now, put the cable back from pin 8 to pin 9. You need to change the program. Do you have an idea, how? Of course you only need to change one single number. That&#8217;s programming \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  digitalWrite(ledPin, HIGH);\n  delay(200);\n  digitalWrite(ledPin, LOW);\n  delay(200);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A very important thing in programming are variables. I guess you already know it from math class. If you say x=10 it means that everywhere x can be replaced by 10. Thing of variables as little boxes or containers in which we can store information like numbers. You can choose your own variable name. A&hellip;&nbsp;<a href=\"https:\/\/starthardware.org\/en\/unit-9-how-to-use-variables\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Unit 9 \u2013 How to use Variables<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":77,"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-72","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 9 \u2013 How to use Variables - 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-9-how-to-use-variables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Unit 9 \u2013 How to use Variables - StartHardware - Tutorials for Arduino\" \/>\n<meta property=\"og:description\" content=\"A very important thing in programming are variables. I guess you already know it from math class. If you say x=10 it means that everywhere x can be replaced by 10. Thing of variables as little boxes or containers in which we can store information like numbers. You can choose your own variable name. A&hellip;&nbsp;Read More &raquo;Unit 9 \u2013 How to use Variables\" \/>\n<meta property=\"og:url\" content=\"https:\/\/starthardware.org\/en\/unit-9-how-to-use-variables\/\" \/>\n<meta property=\"og:site_name\" content=\"StartHardware - Tutorials for Arduino\" \/>\n<meta property=\"article:published_time\" content=\"2019-10-22T07:44:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-10-22T15:34:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-variable-variables3.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1185\" \/>\n\t<meta property=\"og:image:height\" content=\"667\" \/>\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=\"2 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-9-how-to-use-variables\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-variable-variables3.jpg\",\"contentUrl\":\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-variable-variables3.jpg\",\"width\":1185,\"height\":667,\"caption\":\"Arduino Variables\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/starthardware.org\/en\/unit-9-how-to-use-variables\/#webpage\",\"url\":\"https:\/\/starthardware.org\/en\/unit-9-how-to-use-variables\/\",\"name\":\"Unit 9 \u2013 How to use Variables - StartHardware - Tutorials for Arduino\",\"isPartOf\":{\"@id\":\"https:\/\/starthardware.org\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/starthardware.org\/en\/unit-9-how-to-use-variables\/#primaryimage\"},\"datePublished\":\"2019-10-22T07:44:47+00:00\",\"dateModified\":\"2019-10-22T15:34:09+00:00\",\"author\":{\"@id\":\"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59\"},\"breadcrumb\":{\"@id\":\"https:\/\/starthardware.org\/en\/unit-9-how-to-use-variables\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/starthardware.org\/en\/unit-9-how-to-use-variables\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/starthardware.org\/en\/unit-9-how-to-use-variables\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/starthardware.org\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Unit 9 \u2013 How to use Variables\"}]},{\"@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 9 \u2013 How to use Variables - 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-9-how-to-use-variables\/","og_locale":"en_US","og_type":"article","og_title":"Unit 9 \u2013 How to use Variables - StartHardware - Tutorials for Arduino","og_description":"A very important thing in programming are variables. I guess you already know it from math class. If you say x=10 it means that everywhere x can be replaced by 10. Thing of variables as little boxes or containers in which we can store information like numbers. You can choose your own variable name. A&hellip;&nbsp;Read More &raquo;Unit 9 \u2013 How to use Variables","og_url":"https:\/\/starthardware.org\/en\/unit-9-how-to-use-variables\/","og_site_name":"StartHardware - Tutorials for Arduino","article_published_time":"2019-10-22T07:44:47+00:00","article_modified_time":"2019-10-22T15:34:09+00:00","og_image":[{"width":1185,"height":667,"url":"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-variable-variables3.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Written by":"Stefan Hermann","Est. reading time":"2 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-9-how-to-use-variables\/#primaryimage","inLanguage":"en-US","url":"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-variable-variables3.jpg","contentUrl":"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-variable-variables3.jpg","width":1185,"height":667,"caption":"Arduino Variables"},{"@type":"WebPage","@id":"https:\/\/starthardware.org\/en\/unit-9-how-to-use-variables\/#webpage","url":"https:\/\/starthardware.org\/en\/unit-9-how-to-use-variables\/","name":"Unit 9 \u2013 How to use Variables - StartHardware - Tutorials for Arduino","isPartOf":{"@id":"https:\/\/starthardware.org\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/starthardware.org\/en\/unit-9-how-to-use-variables\/#primaryimage"},"datePublished":"2019-10-22T07:44:47+00:00","dateModified":"2019-10-22T15:34:09+00:00","author":{"@id":"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59"},"breadcrumb":{"@id":"https:\/\/starthardware.org\/en\/unit-9-how-to-use-variables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/starthardware.org\/en\/unit-9-how-to-use-variables\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/starthardware.org\/en\/unit-9-how-to-use-variables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/starthardware.org\/en\/"},{"@type":"ListItem","position":2,"name":"Unit 9 \u2013 How to use Variables"}]},{"@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\/72","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=72"}],"version-history":[{"count":1,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts\/72\/revisions"}],"predecessor-version":[{"id":76,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts\/72\/revisions\/76"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/media\/77"}],"wp:attachment":[{"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/media?parent=72"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/categories?post=72"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/tags?post=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}