{"id":111,"date":"2019-10-23T07:07:50","date_gmt":"2019-10-23T07:07:50","guid":{"rendered":"https:\/\/starthardware.org\/en\/?p=111"},"modified":"2019-10-23T07:07:52","modified_gmt":"2019-10-23T07:07:52","slug":"unit-16-one-button-piano","status":"publish","type":"post","link":"https:\/\/starthardware.org\/en\/unit-16-one-button-piano\/","title":{"rendered":"Unit 16 \u2013 One-Button-Piano"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Do you want to build a real musical instrument? Then let\u2019s go!<\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Parts<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"428\" src=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/16_1_parts-1024x428.png\" alt=\"\" class=\"wp-image-112\" srcset=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/16_1_parts-1024x428.png 1024w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/16_1_parts-300x125.png 300w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/16_1_parts-768x321.png 768w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/16_1_parts.png 1280w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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=\"1024\" height=\"803\" src=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-one-button-piano-1024x803.jpg\" alt=\"\" class=\"wp-image-113\" srcset=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-one-button-piano-1024x803.jpg 1024w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-one-button-piano-300x235.jpg 300w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-one-button-piano-768x602.jpg 768w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-one-button-piano.jpg 1362w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Now, we will create a piano with only one button. The piano should always play a tone when the button is pressed. If it is released the tone should mute.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int speakerPin = 9;\nint buttonPin = 11;\n\nvoid setup() {\n  pinMode(buttonPin, INPUT);\n}\n\nvoid loop() {\n  if (digitalRead(buttonPin)==HIGH){\n    tone(speakerPin, 440);\n  } else {\n    noTone(speakerPin);\n  }\n}<\/code><\/pre>\n\n\n\n<p>But how can we play a melody with it? In the last unit we almost had a solution. Do you remember? We used the for-loop to play sounds out of an array. What if the button would do the counting, not the for-loop? But we need to use a trick to do it. It is not enough to ask:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (digitalRead(buttonPin)==HIGH){\n ton = ton + 1;\n}<\/code><\/pre>\n\n\n\n<p>If we would do that, the if statement would be true as long as the button is pressed. It would count much to fast. So we need a construction to detect if the button is pressed and just count up by one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Boolean variables<\/h2>\n\n\n\n<p>Variables of the boolean type only know two conditions: true and false. If we would have a variable named buttonPressed, it could store the current state of the button.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>With the programming start the button is not pressed. The variable buttonPressed is set to false.<\/li><li>Now, we don\u2019t just ask if digitalRead(buttonPin) == HIGH, but even if buttonPressed == false. This can be done with the double et-character (&amp;&amp;).<\/li><li>If both conditions are true, buttonPressed is set to true.<\/li><li>If the button is not pressed, buttonPressed is set to false.<\/li><\/ol>\n\n\n\n<p>Our program looks like that:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>boolean buttonPressed = false;\n\nint speakerPin = 9;\nint buttonPin = 11;\n\nvoid setup() {\n  pinMode(buttonPin, INPUT);\n}\n\nvoid loop() {\n  if ((digitalRead(buttonPin)==HIGH)&amp;&amp;(buttonPressed==false)){\n    buttonPressed=true;\n\n\n  } \n  if (digitalRead(buttonPin)==LOW){\n    buttonPressed=false;\n\n\n  }\n}<\/code><\/pre>\n\n\n\n<p>Great! Now we can use the counter variable, too. If it get\u2019s to large, we have to reset it back to 0. This is done by the third if-statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int tonesTotal = 27;\nint theTone = 0;\n\nboolean buttonPressed = false;\n\nint speakerPin = 9;\nint buttonPin = 11;\n\nvoid setup() {\n  pinMode(buttonPin, INPUT);\n}\n\nvoid loop() {\n  if ((digitalRead(buttonPin)==HIGH)&amp;&amp;(buttonPressed==false)){\n    buttonPressed=true;\n\n    theTone = theTone +1;\n  } \n  if (digitalRead(buttonPin)==LOW){\n    buttonPressed=false;\n\n  }\n  if (theTone>= tonesTotal){\n    ton=0;\n  }\n}<\/code><\/pre>\n\n\n\n<p>Und nun brauchen wir nur noch die Tonausgabe hinzuzuf\u00fcgen:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int frequencies[] = {\n  262, 294, 330, 349, 392, 392, 440, 440, 440, 440, 392, 440, 440, 440, 440, 392, 349, 349, 349, 349, 330, 330, 392, 392, 392, 392, 262};\n\nint tonesTotal = 27;\nint theTone = 0;\n\nboolean buttonPressed = false;\n\nint speakerPin = 9;\nint buttonPin = 11;\n\nvoid setup() {\n  pinMode(buttonPin, INPUT);\n}\n\nvoid loop() {\n  if ((digitalRead(buttonPin)==HIGH)&amp;&amp;(buttonPressed==false)){\n    buttonPressed=true;\n    tone(speakerPin, frequencies[theTone]);\n    theTone = theTone +1;\n  } \n  if (digitalRead(buttonPin)==LOW){\n    buttonPressed=false;\n    noTone(speakerPin);\n  }\n  if (theTone >= tonesTotal){\n    theTone = 0;\n  }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Do you want to build a real musical instrument? Then let\u2019s go! Parts Circuit Now, we will create a piano with only one button. The piano should always play a tone when the button is pressed. If it is released the tone should mute. But how can we play a melody with it? In the&hellip;&nbsp;<a href=\"https:\/\/starthardware.org\/en\/unit-16-one-button-piano\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Unit 16 \u2013 One-Button-Piano<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":114,"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-111","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 16 \u2013 One-Button-Piano - 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-16-one-button-piano\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Unit 16 \u2013 One-Button-Piano - StartHardware - Tutorials for Arduino\" \/>\n<meta property=\"og:description\" content=\"Do you want to build a real musical instrument? Then let\u2019s go! Parts Circuit Now, we will create a piano with only one button. The piano should always play a tone when the button is pressed. If it is released the tone should mute. But how can we play a melody with it? In the&hellip;&nbsp;Read More &raquo;Unit 16 \u2013 One-Button-Piano\" \/>\n<meta property=\"og:url\" content=\"https:\/\/starthardware.org\/en\/unit-16-one-button-piano\/\" \/>\n<meta property=\"og:site_name\" content=\"StartHardware - Tutorials for Arduino\" \/>\n<meta property=\"article:published_time\" content=\"2019-10-23T07:07:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-10-23T07:07:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/one-button-piano.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1429\" \/>\n\t<meta property=\"og:image:height\" content=\"804\" \/>\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-16-one-button-piano\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/one-button-piano.jpg\",\"contentUrl\":\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/one-button-piano.jpg\",\"width\":1429,\"height\":804,\"caption\":\"Arduino project one button piaano\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/starthardware.org\/en\/unit-16-one-button-piano\/#webpage\",\"url\":\"https:\/\/starthardware.org\/en\/unit-16-one-button-piano\/\",\"name\":\"Unit 16 \u2013 One-Button-Piano - StartHardware - Tutorials for Arduino\",\"isPartOf\":{\"@id\":\"https:\/\/starthardware.org\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/starthardware.org\/en\/unit-16-one-button-piano\/#primaryimage\"},\"datePublished\":\"2019-10-23T07:07:50+00:00\",\"dateModified\":\"2019-10-23T07:07:52+00:00\",\"author\":{\"@id\":\"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59\"},\"breadcrumb\":{\"@id\":\"https:\/\/starthardware.org\/en\/unit-16-one-button-piano\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/starthardware.org\/en\/unit-16-one-button-piano\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/starthardware.org\/en\/unit-16-one-button-piano\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/starthardware.org\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Unit 16 \u2013 One-Button-Piano\"}]},{\"@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 16 \u2013 One-Button-Piano - 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-16-one-button-piano\/","og_locale":"en_US","og_type":"article","og_title":"Unit 16 \u2013 One-Button-Piano - StartHardware - Tutorials for Arduino","og_description":"Do you want to build a real musical instrument? Then let\u2019s go! Parts Circuit Now, we will create a piano with only one button. The piano should always play a tone when the button is pressed. If it is released the tone should mute. But how can we play a melody with it? In the&hellip;&nbsp;Read More &raquo;Unit 16 \u2013 One-Button-Piano","og_url":"https:\/\/starthardware.org\/en\/unit-16-one-button-piano\/","og_site_name":"StartHardware - Tutorials for Arduino","article_published_time":"2019-10-23T07:07:50+00:00","article_modified_time":"2019-10-23T07:07:52+00:00","og_image":[{"width":1429,"height":804,"url":"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/one-button-piano.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-16-one-button-piano\/#primaryimage","inLanguage":"en-US","url":"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/one-button-piano.jpg","contentUrl":"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/one-button-piano.jpg","width":1429,"height":804,"caption":"Arduino project one button piaano"},{"@type":"WebPage","@id":"https:\/\/starthardware.org\/en\/unit-16-one-button-piano\/#webpage","url":"https:\/\/starthardware.org\/en\/unit-16-one-button-piano\/","name":"Unit 16 \u2013 One-Button-Piano - StartHardware - Tutorials for Arduino","isPartOf":{"@id":"https:\/\/starthardware.org\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/starthardware.org\/en\/unit-16-one-button-piano\/#primaryimage"},"datePublished":"2019-10-23T07:07:50+00:00","dateModified":"2019-10-23T07:07:52+00:00","author":{"@id":"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59"},"breadcrumb":{"@id":"https:\/\/starthardware.org\/en\/unit-16-one-button-piano\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/starthardware.org\/en\/unit-16-one-button-piano\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/starthardware.org\/en\/unit-16-one-button-piano\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/starthardware.org\/en\/"},{"@type":"ListItem","position":2,"name":"Unit 16 \u2013 One-Button-Piano"}]},{"@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\/111","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=111"}],"version-history":[{"count":1,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts\/111\/revisions"}],"predecessor-version":[{"id":115,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts\/111\/revisions\/115"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/media\/114"}],"wp:attachment":[{"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/media?parent=111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/categories?post=111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/tags?post=111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}