{"id":123,"date":"2015-09-23T03:56:16","date_gmt":"2015-09-23T03:56:16","guid":{"rendered":"http:\/\/www.aaronparsekian.com\/?p=123"},"modified":"2015-09-23T03:56:16","modified_gmt":"2015-09-23T03:56:16","slug":"3rd-assignment-digital-and-analog-labs","status":"publish","type":"post","link":"http:\/\/www.aaronparsekian.com\/?p=123","title":{"rendered":"3rd Assignment &#8211; Digital and Analog Labs"},"content":{"rendered":"<p>This week we started to use the Arduino to input and output analog signals.<\/p>\n<p>Beginning with a simple pushbutton to turn on an LED:<\/p>\n<p><a href=\"http:\/\/www.aaronparsekian.com\/wp-content\/uploads\/2015\/09\/button_pin2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-126\" src=\"http:\/\/www.aaronparsekian.com\/wp-content\/uploads\/2015\/09\/button_pin2-300x225.jpg\" alt=\"button_pin2\" width=\"300\" height=\"225\" srcset=\"http:\/\/www.aaronparsekian.com\/wp-content\/uploads\/2015\/09\/button_pin2-300x225.jpg 300w, http:\/\/www.aaronparsekian.com\/wp-content\/uploads\/2015\/09\/button_pin2-1024x768.jpg 1024w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><code> void setup() {<br \/>\npinMode(2, INPUT); \/\/ set the switch pin to be an input<br \/>\npinMode(3, OUTPUT); \/\/ set the yellow LED pin to be an output<br \/>\npinMode(4, OUTPUT); \/\/ set the red LED pin to be an output<br \/>\n}<br \/>\nvoid loop() {<br \/>\n\/\/ read the switch input:<br \/>\nif (digitalRead(2) == HIGH) {<br \/>\n\/\/ if the switch is closed:<br \/>\ndigitalWrite(3, HIGH); \/\/ turn on the yellow LED<br \/>\ndigitalWrite(4, LOW); \/\/ turn off the red LED<br \/>\n}<br \/>\nelse {<br \/>\n\/\/ if the switch is open:<br \/>\ndigitalWrite(3, LOW); \/\/ turn off the yellow LED<br \/>\ndigitalWrite(4, HIGH); \/\/ turn on the red LED<br \/>\n}<br \/>\n}<br \/>\n<\/code><br \/>\nNext we worked with analog inputs:<\/p>\n<p><code><br \/>\nconst int ledPin = 9; \/\/ pin that the LED is attached to<br \/>\nint analogValue = 0; \/\/ value read from the pot<br \/>\nint brightness = 0; \/\/ PWM pin that the LED is on.<br \/>\nvoid setup() {<br \/>\n\/\/ initialize serial communications at 9600 bps:<br \/>\nSerial.begin(9600);<br \/>\n\/\/ declare the led pin as an output:<br \/>\npinMode(ledPin, OUTPUT);<br \/>\n}<br \/>\nvoid loop() {<br \/>\nanalogValue = analogRead(A0); \/\/ read the pot value<br \/>\nbrightness = analogValue \/4; \/\/divide by 4 to fit in a byte<br \/>\nanalogWrite(ledPin, brightness); \/\/ PWM the LED with the brightness value<br \/>\nSerial.println(brightness); \/\/ print the brightness value back to the serial monitor<br \/>\n}<br \/>\n<\/code><br \/>\nHere is the code outputting to a potentiometer:<\/p>\n<p><iframe loading=\"lazy\" title=\"IMG_0848\" src=\"https:\/\/player.vimeo.com\/video\/140141837?dnt=1&amp;app_id=122963\" width=\"660\" height=\"371\" frameborder=\"0\" allow=\"autoplay; fullscreen; picture-in-picture; clipboard-write\"><\/iframe><\/p>\n<p>And here it is with a CdS Photoresistor:<\/p>\n<p><iframe loading=\"lazy\" title=\"IMG_0851\" src=\"https:\/\/player.vimeo.com\/video\/140141843?dnt=1&amp;app_id=122963\" width=\"660\" height=\"371\" frameborder=\"0\" allow=\"autoplay; fullscreen; picture-in-picture; clipboard-write\"><\/iframe><\/p>\n<p>&nbsp;<\/p>\n<p>Finally I decided to work a little with outputting pulse width modulation to an LED, controlled via a sliding pot. Here is the simple code:<\/p>\n<p>&nbsp;<\/p>\n<p><code><br \/>\nconst int potPin = A0;<br \/>\nconst int ledPin = 3;<\/code><\/p>\n<p><code><br \/>\nint sensor = 0;<br \/>\nint output = 0;<\/code><\/p>\n<p><code><br \/>\nvoid setup() {<br \/>\n}<\/code><\/p>\n<p><code><br \/>\nvoid loop() {<br \/>\nsensor = analogRead(potPin);<br \/>\noutput = map(sensor, 0, 1023, 0, 255);<br \/>\nanalogWrite(ledPin, output);<br \/>\n}<\/code><\/p>\n<p>Here is what it looks like on my oscilloscope while &#8220;dimming&#8221; \ud83d\ude42<\/p>\n<p><iframe loading=\"lazy\" title=\"IMG_0953\" src=\"https:\/\/player.vimeo.com\/video\/140141280?dnt=1&amp;app_id=122963\" width=\"660\" height=\"371\" frameborder=\"0\" allow=\"autoplay; fullscreen; picture-in-picture; clipboard-write\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This week we started to use the Arduino to input and output analog signals. Beginning with a simple pushbutton to turn on an LED: void setup() { pinMode(2, INPUT); \/\/ set the switch pin to be an input pinMode(3, OUTPUT); \/\/ set the yellow LED pin to be an output pinMode(4, OUTPUT); \/\/ set the &hellip; <a href=\"http:\/\/www.aaronparsekian.com\/?p=123\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">3rd Assignment &#8211; Digital and Analog Labs<\/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":[8,3],"tags":[],"class_list":["post-123","post","type-post","status-publish","format-standard","hentry","category-fall-2015","category-pcomp"],"_links":{"self":[{"href":"http:\/\/www.aaronparsekian.com\/index.php?rest_route=\/wp\/v2\/posts\/123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.aaronparsekian.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.aaronparsekian.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.aaronparsekian.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.aaronparsekian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=123"}],"version-history":[{"count":1,"href":"http:\/\/www.aaronparsekian.com\/index.php?rest_route=\/wp\/v2\/posts\/123\/revisions"}],"predecessor-version":[{"id":127,"href":"http:\/\/www.aaronparsekian.com\/index.php?rest_route=\/wp\/v2\/posts\/123\/revisions\/127"}],"wp:attachment":[{"href":"http:\/\/www.aaronparsekian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.aaronparsekian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.aaronparsekian.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}