{"id":102,"date":"2011-11-19T19:29:29","date_gmt":"2011-11-19T18:29:29","guid":{"rendered":"http:\/\/jcoder.me\/blog\/?p=102"},"modified":"2011-11-19T19:29:29","modified_gmt":"2011-11-19T18:29:29","slug":"netduino-scripted-lights","status":"publish","type":"post","link":"https:\/\/jcoder.me\/blog\/2011\/11\/19\/netduino-scripted-lights\/","title":{"rendered":"Netduino Scripted Lights"},"content":{"rendered":"<p>Playing around with a FEZ Panda II is a lot of fun, especially when it comes to LED lights (and when it&#8217;s getting dark outside). Sure, you can make them simply blink, or maybe progam a more complex light-switching pattern, but you always end up with a lot of code to address the LEDs and wait some time between changing their state.<\/p>\n<p>To make it easier to work with an LED, I&#8217;ve written a small wrapper class some time ago as described in <a title=\"Netduino Switching Lights (with interaction)\" href=\"http:\/\/jcoder.me\/blog\/2011\/07\/03\/netduino-switching-lights-with-interaction\/\">this posting<\/a>, but I really wanted to come around having to write a lot of (non-reusable) code just to make some LEDs blink.<\/p>\n<p>My current solution: a very simple, <strong>char-based scripting module<\/strong> to control the LEDs. This is how it works:<\/p>\n<ul>\n<li>You define some simple script like <code>\"R2 r2\"<\/code> or <code>\"GH4 YZ4 a2\"<\/code><\/li>\n<li>Based on the <code>ILights<\/code> interface, you create an instance of <code>LEDLights<\/code><\/li>\n<li>An outer loop defines how often the sequence should be executed<\/li>\n<li>The inner loop iterates over the sequence and passes each char to the mini engine<\/li>\n<li>The lights start blinking, or whatever you told them to do<\/li>\n<\/ul>\n<p>Actually, some of these steps could be encapsulated and the pin configuration for the LEDs is currently hard-coded,\u00a0but for the beginning I wanted to keep the things as easy as possible.<\/p>\n<p><strong>See it in action<\/strong><\/p>\n<p><iframe loading=\"lazy\" src=\"http:\/\/player.vimeo.com\/video\/32369069\" frameborder=\"0\" width=\"500\" height=\"281\"><\/iframe><\/p>\n<p><strong>How does the script work?<\/strong><\/p>\n<p>In short: an <strong>uppercase letter<\/strong> switches the LED <strong>on<\/strong>, a <strong>lowercase letter<\/strong> switches it <strong>off<\/strong>, and a <strong>numerical letter pauses<\/strong> between two actions. I recently got some additional LEDs so I added support for 6 LEDs (please look up the pin configuration in the source code).<\/p>\n<ul>\n<li>r \/ R : red LED no. 1<\/li>\n<li>s \/ S : red LED no. 2<\/li>\n<li>y \/ Y : yellow LED no. 1<\/li>\n<li>z \/ Z : yellow LED no. 2<\/li>\n<li>g \/ G : green LED no. 1<\/li>\n<li>h \/ H : green LED no. 2<\/li>\n<li>a \/ A : all LEDs at once<\/li>\n<li>1, 2, 3, &#8230;, 9 : pause for 500ms, 1000ms, 1500ms, &#8230;, 4500ms<\/li>\n<\/ul>\n<p>All other characters are currently ignored, so you can add spaces to separate the parts of the script which makes it easier to read the script sequence.<\/p>\n<p><strong>Some examples<\/strong><\/p>\n<p><code>R2 r2<\/code> &#8211; the red LEDs is switched on, then waits for 1000ms, then \u00a0the LED is switched off and the script waits again for 1000ms<\/p>\n<p><code>R1Y1G4 r1y1g4<\/code> &#8211; the red, yellow and green LED is switched with short delay, then switched off again<\/p>\n<p><code>zY2 yZ2<\/code> &#8211; the two yellow LEDs blink alternately<\/p>\n<p><strong>Tipps, tricks and ideas<\/strong><\/p>\n<p>When defining a sequence make sure that you add pauses, because otherwise the LEDs fill flicker based on the Netduino&#8217;s maximum speed.<br \/>\nIt&#8217;s easy to forget the pause at the end of the script, which makes the sequence look strange when it gets repeated.<br \/>\nOf course, you can switch off an LED at the beginning of the script, even if it was not switched off before. This is very useful if the sequence has a complex overall pattern and an LED must stay switched on at the end of the script.<\/p>\n<p>Some ideas to extend this project:<\/p>\n<ul>\n<li>Store more than one sequence in the program and add an IR-receiver or simply a button to switch the sequences<\/li>\n<li>Read the sequences from an SD card<\/li>\n<li>Connect the Netduino to the network and add either a webserver to the program to enter the sequence or fetch it from a website (or even from Twitter)<\/li>\n<\/ul>\n<p><strong>Source Code<\/strong><\/p>\n<p>For the LED class please look at <a title=\"Netduino Switching Lights (with interaction)\" href=\"http:\/\/jcoder.me\/blog\/2011\/07\/03\/netduino-switching-lights-with-interaction\/\">this posting<\/a>.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic interface ILights\r\n{\r\n    void LightCommand(char c);\r\n}\r\n\r\npublic class LEDLights : ILights\r\n{\r\n    private int baseDelay = 500;\r\n\r\n    private LED led_R = new LED(GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di1);\r\n    private LED led_Y = new LED(GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di0);\r\n    private LED led_G = new LED(GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di8);\r\n    private LED led_S = new LED(GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di5);\r\n    private LED led_Z = new LED(GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di3);\r\n    private LED led_H = new LED(GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di2);\r\n\r\n    public void LightCommand(char c)\r\n    {\r\n        switch (c)\r\n        {\r\n            case 'r': led_R.Off(); break;\r\n            case 'R': led_R.On(); break;\r\n            case 'y': led_Y.Off(); break;\r\n            case 'Y': led_Y.On(); break;\r\n            case 'g': led_G.Off(); break;\r\n            case 'G': led_G.On(); break;\r\n            case 's': led_S.Off(); break;\r\n            case 'S': led_S.On(); break;\r\n            case 'z': led_Z.Off(); break;\r\n            case 'Z': led_Z.On(); break;\r\n            case 'h': led_H.Off(); break;\r\n            case 'H': led_H.On(); break;\r\n            case 'a': led_R.Off(); led_Y.Off(); led_G.Off(); led_S.Off(); led_Z.Off(); led_H.Off(); break;\r\n            case 'A': led_R.On(); led_Y.On(); led_G.On(); led_S.On(); led_Z.On(); led_H.On(); break;\r\n            case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': Sleep(c); break;\r\n        }\r\n    }\r\n\r\n    protected void Sleep(char c)\r\n    {\r\n        int num = c - '1' + 1;\r\n        int delay = num * baseDelay;\r\n        Thread.Sleep(delay);\r\n    }\r\n}\r\n\r\npublic class Program\r\n{\r\n    public static void Main()\r\n    {\r\n        string sequence = &quot;R2 r2;\r\n        \/\/ process sequence\r\n        ILights lights = new LEDLights();\r\n        \/\/ switch off all lights at the beginning\r\n        lights.LightCommand('a');\r\n        \/\/ outer loop\r\n        bool looping = true;\r\n        while (looping)\r\n        {\r\n            for (int i = 0; i &lt; sequence.Length; i++)\r\n            {\r\n                char c = sequence&#x5B;i];\r\n                lights.LightCommand(c);\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Playing around with a FEZ Panda II is a lot of fun, especially when it comes to LED lights (and when it&#8217;s getting dark outside). Sure, you can make them simply blink, or maybe progam a more complex light-switching pattern, &hellip; <a href=\"https:\/\/jcoder.me\/blog\/2011\/11\/19\/netduino-scripted-lights\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,6,4],"tags":[8,7,23,51,18],"class_list":["post-102","post","type-post","status-publish","format-standard","hentry","category-coding","category-netduino","category-netmf","tag-c","tag-fez","tag-lights","tag-netduino","tag-panda-ii"],"_links":{"self":[{"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/posts\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":6,"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":109,"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/posts\/102\/revisions\/109"}],"wp:attachment":[{"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}