
{"id":394,"date":"2018-10-02T15:19:20","date_gmt":"2018-10-02T15:19:20","guid":{"rendered":"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/?page_id=394"},"modified":"2018-10-02T15:22:43","modified_gmt":"2018-10-02T15:22:43","slug":"task-4-1-2-solution","status":"publish","type":"page","link":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/microcontrollers\/mbed-os-2\/courses\/embedded-systems-in-context-level-4\/topic-4-analogue-input\/task-4-1-2-solution\/","title":{"rendered":"Task 4.1.2 Solution"},"content":{"rendered":"<p><a href=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/mbed-os-2\/courses\/embedded-systems-in-context-level-4\/topic-4-analogue-input\/\">Back<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-395\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/Task412Solution.png\" alt=\"\" width=\"860\" height=\"1141\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/Task412Solution.png 860w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/Task412Solution-226x300.png 226w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/Task412Solution-768x1019.png 768w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/Task412Solution-772x1024.png 772w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/Task412Solution-753x999.png 753w\" sizes=\"auto, (max-width: 860px) 100vw, 860px\" \/><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"theme:xcode lang:c++ decode:true\">#include \"mbed.h\"\r\n#define kRED    4\r\n#define kYELLOW 2\r\n#define kGREEN  1\r\n\r\n\r\n\/\/Global objects\r\nBusOut binaryOutput(D5, D6, D7);\r\n\r\nDigitalIn SW1(D3);\r\nDigitalIn SW2(D4);\r\n\r\nAnalogIn AIN(A0);\r\nfloat fVin = 0.0;\r\n\r\n\/\/Main function\r\nint main() {\r\n   \r\n   \r\n   while(1) {\r\n      \r\n      \/\/Read ADC\r\n      fVin = AIN;\r\n      \r\n      \/\/Write to terminal\r\n      \/\/3 decimal places, fieldwidth=5\r\n      printf(\"Analog input = %6.4f\\n\", fVin);\r\n      \r\n      if (fVin &lt; 0.4f)  {\r\n         binaryOutput = kGREEN;\r\n      } else if (fVin &lt; 0.6f) {\r\n         binaryOutput = kYELLOW;\r\n      } else {\r\n         binaryOutput = kRED;\r\n      }\r\n      \r\n      \/\/Wait\r\n      wait(0.1);\r\n      \r\n   } \/\/end while(1)\r\n} \/\/end main<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Back &nbsp; #include &#8220;mbed.h&#8221; #define kRED 4 #define kYELLOW 2 #define kGREEN 1 \/\/Global objects BusOut binaryOutput(D5, D6, D7); DigitalIn SW1(D3); DigitalIn SW2(D4); AnalogIn AIN(A0); float fVin = 0.0; \/\/Main function int main() { while(1) { \/\/Read ADC fVin = AIN; \/\/Write to terminal \/\/3 decimal places, fieldwidth=5 printf(&#8220;Analog input = %6.4f\\n&#8221;, fVin); if (fVin&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/microcontrollers\/mbed-os-2\/courses\/embedded-systems-in-context-level-4\/topic-4-analogue-input\/task-4-1-2-solution\/\">Continue reading <span class=\"screen-reader-text\">Task 4.1.2 Solution<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":371,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-394","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/comments?post=394"}],"version-history":[{"count":2,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/394\/revisions"}],"predecessor-version":[{"id":398,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/394\/revisions\/398"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/371"}],"wp:attachment":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/media?parent=394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}