{"id":10,"date":"2011-08-14T18:00:37","date_gmt":"2011-08-14T18:00:37","guid":{"rendered":"http:\/\/adam.finzel.co.uk\/?p=10"},"modified":"2016-01-24T19:35:32","modified_gmt":"2016-01-24T19:35:32","slug":"arduino-project-1-led-binary-counter","status":"publish","type":"post","link":"http:\/\/adam.finzel.co.uk\/?p=10","title":{"rendered":"Arduino Project 1 &#8211; LED Binary Counter"},"content":{"rendered":"<p>I&#8217;ve always had an interest in electronics although haven&#8217;t been good at it. \u00a0A couple of my work mates bought\u00a0<a href=\"http:\/\/www.arduino.cc\/\">http:\/\/www.arduino.cc\/<\/a>\u00a0and I had to buy one too. \u00a0This is my first very simple project, a LED binary counter.<\/p>\n<p><iframe loading=\"lazy\" src=\"http:\/\/www.youtube.com\/embed\/BkY7GGew2yc?hl=en&amp;fs=1\" width=\"425\" height=\"349\" frameborder=\"0\"><\/iframe><\/p>\n<p>The circuit is boring, each LED is plugged up to a different digital out.<\/p>\n<p>&nbsp;<\/p>\n<p>The code is a little clever but still simple. Below is a function that converts a number to turn on one of 4 LEDS on. It works by dividing the number by 8,4,2, and 1 in that order. If it is divisible by that number then we turn that light on and deduct it from the original number.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;pre class=&quot;Plum_Code_Box&quot;&gt;&lt;code class=&quot;&quot;&gt;void convertIntToBinary(int number) {\r\nint divisible;\r\ndivisible = 8;&amp;lt;\/code&amp;gt;\r\n\r\nif (number \/ 8) {\r\nnumber = number - 8;\r\ndigitalWrite(LEDVAL8, HIGH);\r\n} else {\r\ndigitalWrite(LEDVAL8, LOW);\r\n}\r\n\r\nif (number \/ 4) {\r\nnumber = number - 4 ;\r\ndigitalWrite(LEDVAL4, HIGH);\r\n} else {\r\ndigitalWrite(LEDVAL4, LOW);\r\n}\r\n\r\nif (number \/ 2) {\r\nnumber = number - 2 ;\r\ndigitalWrite(LEDVAL2, HIGH);\r\n} else {\r\ndigitalWrite(LEDVAL2, LOW);\r\n}\r\n\r\nif (number \/ 1) {\r\nnumber = number - 1 ;\r\ndigitalWrite(LEDVAL1, HIGH);\r\n} else {\r\ndigitalWrite(LEDVAL1, LOW);\r\n}\r\n\r\n\/\/digitalWrite(number, HIGH);\r\n}&lt;\/code&gt;\r\n&lt;\/pre&gt;\r\n<\/pre>\n<p>The main part of the code links up the LEDs and increments our number between 0 and 16(the max we can do with 4 LEDs)<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;pre class=&quot;Plum_Code_Box&quot;&gt;&lt;code class=&quot;&quot;&gt;#define LEDVAL1 4\r\n#define LEDVAL2 5\r\n#define LEDVAL4 6\r\n#define LEDVAL8 7&amp;lt;\/code&amp;gt;\r\n\r\nint counter;\r\n\r\nvoid setup() {\r\npinMode(LEDVAL1, OUTPUT);\r\npinMode(LEDVAL2, OUTPUT);\r\npinMode(LEDVAL4, OUTPUT);\r\npinMode(LEDVAL8, OUTPUT);\r\n}\r\n\r\nvoid loop() {\r\ncounter = counter + 1;\r\nconvertIntToBinary(counter);\r\n\r\nif (counter == 16) {\r\ncounter = -1;\r\n}\r\n\r\ndelay(1000);\r\n}&lt;\/code&gt;&lt;\/pre&gt;\r\n<\/pre>\n<p>The full code:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;pre class=&quot;Plum_Code_Box&quot;&gt;&lt;code class=&quot;&quot;&gt;#define LEDVAL1 4\r\n#define LEDVAL2 5\r\n#define LEDVAL4 6\r\n#define LEDVAL8 7&amp;lt;\/code&amp;gt;\r\n\r\nint counter;\r\n\r\nvoid setup() {\r\npinMode(LEDVAL1, OUTPUT);\r\npinMode(LEDVAL2, OUTPUT);\r\npinMode(LEDVAL4, OUTPUT);\r\npinMode(LEDVAL8, OUTPUT);\r\n}\r\n\r\nvoid loop() {\r\ncounter = counter + 1;\r\nconvertIntToBinary(counter);\r\n\r\nif (counter == 16) {\r\ncounter = -1;\r\n}\r\n\r\ndelay(1000);\r\n}\r\n\r\nvoid convertIntToBinary(int number) {\r\nint divisible;\r\ndivisible = 8;\r\n\r\nif (number \/ 8) {\r\nnumber = number - 8;\r\ndigitalWrite(LEDVAL8, HIGH);\r\n} else {\r\ndigitalWrite(LEDVAL8, LOW);\r\n}\r\n\r\nif (number \/ 4) {\r\nnumber = number - 4 ;\r\ndigitalWrite(LEDVAL4, HIGH);\r\n} else {\r\ndigitalWrite(LEDVAL4, LOW);\r\n}\r\n\r\nif (number \/ 2) {\r\nnumber = number - 2 ;\r\ndigitalWrite(LEDVAL2, HIGH);\r\n} else {\r\ndigitalWrite(LEDVAL2, LOW);\r\n}\r\n\r\nif (number \/ 1) {\r\nnumber = number - 1 ;\r\ndigitalWrite(LEDVAL1, HIGH);\r\n} else {\r\ndigitalWrite(LEDVAL1, LOW);\r\n}\r\n\r\n\/\/digitalWrite(number, HIGH);\r\n}&lt;\/code&gt;\r\n\t\t\t\t\t\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve always had an interest in electronics although haven&#8217;t been good at it. \u00a0A couple of my work mates bought\u00a0http:\/\/www.arduino.cc\/\u00a0and I had to buy one too. \u00a0This is my first very simple project, a LED binary counter. The circuit is boring, each LED is plugged up to a different digital out. &nbsp; The code is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-arduino"],"_links":{"self":[{"href":"http:\/\/adam.finzel.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/adam.finzel.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/adam.finzel.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/adam.finzel.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/adam.finzel.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10"}],"version-history":[{"count":8,"href":"http:\/\/adam.finzel.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/10\/revisions"}],"predecessor-version":[{"id":61,"href":"http:\/\/adam.finzel.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/10\/revisions\/61"}],"wp:attachment":[{"href":"http:\/\/adam.finzel.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/adam.finzel.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/adam.finzel.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}