{"id":94,"date":"2020-03-25T12:28:06","date_gmt":"2020-03-25T12:28:06","guid":{"rendered":"https:\/\/www.staff.ncl.ac.uk\/francisfranklin\/?page_id=94"},"modified":"2020-03-25T12:30:37","modified_gmt":"2020-03-25T12:30:37","slug":"arduino-lab","status":"publish","type":"page","link":"https:\/\/www.staff.ncl.ac.uk\/francisfranklin\/arduino-lab\/","title":{"rendered":"Arduino Lab"},"content":{"rendered":"<p><strong>ArduinoSim<\/strong> is a Python simulation of an Arduino Uno connected to two switches, two LEDs and a servo motor. It provides an easy way to learn how to multi-task, and was written with the following exercise in mind:<\/p>\n<ol>\n<li>When the top button is pressed, the green LED should blink;<\/li>\n<li>When the bottom button is pressed, the blue LED should blink;<\/li>\n<li>When both buttons are pressed, the green and blue LEDS should alternate; and<\/li>\n<li>Meanwhile, the servo should be sweeping back and forth smoothly across 180 degrees.<\/li>\n<\/ol>\n<div id=\"attachment_96\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-96\" src=\"http:\/\/www.staff.ncl.ac.uk\/francisfranklin\/files\/2020\/03\/Demo-400x300.gif\" alt=\"Virtual simulation of Arduino Uno connected to breadboad and servo circuit.\" width=\"400\" height=\"300\" class=\"size-full wp-image-96\" \/><p id=\"caption-attachment-96\" class=\"wp-caption-text\">Animation showing completed exercise. <a href=\"https:\/\/github.com\/FJFranklin\/wifi-py-rpi-car-controller\/blob\/master\/ArduinoSim\/images\/Demo-800x600.gif\"><em>(Click for larger version.)<\/em><\/a><\/p><\/div>\n<h2>Python<\/h2>\n<p>The following two files should be in your Python working folder (e.g., <code>H:\\\\Python<\/code>):<\/p>\n<ul>\n<li><a href=\"https:\/\/raw.githubusercontent.com\/FJFranklin\/wifi-py-rpi-car-controller\/master\/ArduinoSim\/ArduinoSim.py\">ArduinoSim.py<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/FJFranklin\/wifi-py-rpi-car-controller\/raw\/master\/ArduinoSim\/ArduinoSim.png\">ArduinoSim.png<\/a><\/li>\n<\/ul>\n<p>Do <strong>not<\/strong> edit these. You will need to create a third file &#8211; you can use <a href=\"https:\/\/raw.githubusercontent.com\/FJFranklin\/wifi-py-rpi-car-controller\/master\/ArduinoSim\/example.py\">example.py<\/a> as a template &#8211; in the same folder.<\/p>\n<p><strong>Note<\/strong>: The simulation is written for <strong>Python 3<\/strong>, and the Python module <strong>matplotlib<\/strong> must be installed.<\/p>\n<p>When you start the Python application and command window (e.g., IDLE, or IDLEX if you have it), you will need to set the working directory:<\/p>\n<pre>import os\r\nos.chdir('H:\\\\Python')<\/pre>\n<p>As with the Arduino, <code>setup()<\/code> runs once at the beginning, and then <code>loop()<\/code> is run repeatedly.<\/p>\n<p>The methods available for controlling the Arduino are:<\/p>\n<dl>\n<dt><code>delay(ms)<\/code><\/dt>\n<dd>Pause execution for <em>ms<\/em> milliseconds.<\/dd>\n<dt><code>digitalRead(number)<\/code><\/dt>\n<dd>Returns the current value of pin <em>number<\/em> as either HIGH or LOW. Note: The pin should be set as an INPUT.<\/dd>\n<dt><code>digitalWrite(number, state)<\/code><\/dt>\n<dd>Sets the current value of pin <em>number<\/em> as <em>state<\/em>, where <em>state<\/em> is either HIGH or LOW. Note: The pin should be set as an OUTPUT.<\/dd>\n<dt><code>micros()<\/code><\/dt>\n<dd>Returns the number of microseconds since program started.<\/dd>\n<dt><code>millis()<\/code><\/dt>\n<dd>Returns the number of milliseconds since program started.<\/dd>\n<dt><code>pinMode(number, mode)<\/code><\/dt>\n<dd>Specify that pin <em>number<\/em> should be used as an input (if <em>mode<\/em> is INPUT) or as an output (if <em>mode<\/em> is OUTPUT).<\/dd>\n<\/dl>\n<p><strong>Note:<\/strong> <em>number<\/em> is an integer in the range 0-13, corresponding to the digital pins on the Arduino.<\/p>\n<p>In addition, a servo controller can be created using, e.g.:<\/p>\n<pre>myServo = Servo()<\/pre>\n<p>and this has the usual Servo methods, most importantly:<\/p>\n<dl>\n<dt><code>myServo.attach(number)<\/code><\/dt>\n<dd>Attach the servo controller to pin <em>number<\/em>.<\/dd>\n<dt><code>myServo.write(angle)<\/code><\/dt>\n<dd>Set the servo to position <em>angle<\/em>, where 0 &lt;= <em>angle<\/em> &lt;= 180.<\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>ArduinoSim is a Python simulation of an Arduino Uno connected to two switches, two LEDs and a servo motor. It provides an easy way to learn how to multi-task, and was written with the following exercise in mind: When the &hellip; <a href=\"https:\/\/www.staff.ncl.ac.uk\/francisfranklin\/arduino-lab\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1692,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-94","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.staff.ncl.ac.uk\/francisfranklin\/wp-json\/wp\/v2\/pages\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.staff.ncl.ac.uk\/francisfranklin\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.staff.ncl.ac.uk\/francisfranklin\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.staff.ncl.ac.uk\/francisfranklin\/wp-json\/wp\/v2\/users\/1692"}],"replies":[{"embeddable":true,"href":"https:\/\/www.staff.ncl.ac.uk\/francisfranklin\/wp-json\/wp\/v2\/comments?post=94"}],"version-history":[{"count":4,"href":"https:\/\/www.staff.ncl.ac.uk\/francisfranklin\/wp-json\/wp\/v2\/pages\/94\/revisions"}],"predecessor-version":[{"id":99,"href":"https:\/\/www.staff.ncl.ac.uk\/francisfranklin\/wp-json\/wp\/v2\/pages\/94\/revisions\/99"}],"wp:attachment":[{"href":"https:\/\/www.staff.ncl.ac.uk\/francisfranklin\/wp-json\/wp\/v2\/media?parent=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}