{"id":2134,"date":"2022-01-25T21:24:34","date_gmt":"2022-01-26T02:24:34","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2134"},"modified":"2022-01-26T12:27:03","modified_gmt":"2022-01-26T17:27:03","slug":"class-notes-25-jan-2022","status":"publish","type":"post","link":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2134","title":{"rendered":"Class Notes, 25 Jan 2022"},"content":{"rendered":"<h1>Class 3: 25 Jan &#8211; Intro to Interrupts<\/h1>\n<p>Office hours start next week on campus in A10 at 5pm.<\/p>\n<h2>Review assignments<\/h2>\n<p>Design for Dreaming \u2014 good?\u00a0 Bad?\u00a0 Socially uncomfortable?<\/p>\n<p>revisit tigoe\u2019s page \u2014 what was physical interaction and what was reaction?\u00a0 Do you see any themes in projects that aren\u2019t allowed? \u00a0 Is there something you\u2019re tired of seeing?<\/p>\n<p>I like theremins and have two.\u00a0 Making them is easy.\u00a0 Playing them is damned difficult.<\/p>\n<p>Dorit Chrysler playing The Swan:\u00a0 <a href=\"https:\/\/www.youtube.com\/watch?v=AS9LJK8i1VE\">https:\/\/www.youtube.com\/watch?v=AS9LJK8i1VE<\/a><\/p>\n<p>Clara Rockmore, also playing The Swan:\u00a0 <a href=\"https:\/\/www.youtube.com\/watch?v=XdFSU8sn3mo\">https:\/\/www.youtube.com\/watch?v=XdFSU8sn3mo<\/a><\/p>\n<p>Theremin playing a theremin:\u00a0 <a href=\"https:\/\/www.youtube.com\/watch?v=w5qf9O6c20o\">https:\/\/www.youtube.com\/watch?v=w5qf9O6c20o<\/a><\/p>\n<h2>Interrupts<\/h2>\n<p>We use interrupts in daily life:<\/p>\n<ul>\n<li>Asking a question in class<\/li>\n<li>Phone rings (skeumorphic!)<\/li>\n<li>Traffic light<\/li>\n<\/ul>\n<p>What can we do during an interruption?\u00a0 What do we do at a traffic light? During a lesson?<\/p>\n<p>What happens when multiple people raise their hands in class?\u00a0 What happens if the instructor is servicing a request and someone else raises their hand?\u00a0 Stop answering the first person?\u00a0 Delay the second person?<\/p>\n<p>Interrupt <strong>request<\/strong> vs interrupt <strong>service<\/strong>.<\/p>\n<p>Raising your hand is an interrupt request.\u00a0 The instructor responding is servicing the interrupt request.<\/p>\n<p>Explain how interrupts work\u00a0 in software\/hardware<\/p>\n<ul>\n<li>clock has to stop, so millis(), Serial, and other I\/O functions won&#8217;t work including reading\/writing to pins<\/li>\n<li>difficult to debug anything more complicated than changing a state variable<\/li>\n<li>really useful for state machines because you don&#8217;t have to call digitalRead() or analogRead()<\/li>\n<li>delay() timing is paused<\/li>\n<\/ul>\n<p>What if you don&#8217;t want an interrupt to happen?\u00a0 This is probably a bad thing, as a lot of what makes the Arduino work is interrupts you can&#8217;t see happen:<\/p>\n<p>noInterrupts() &#8212; disable interrupts, like putting your phone in &#8220;Do Not Disturb&#8221;&#8221;<\/p>\n<p>interrupts() &#8212; turns interrupts back on<\/p>\n<p>Interrupts can be hard to debug on Arduino.\u00a0 No access to the Serial console, difficult to do anything other than simple math\/logic operations.<\/p>\n<p>Compilation hides errors by not failing.\u00a0 Say you have this code for an interrupt handler:<\/p>\n<blockquote><p>void switchLed() {<\/p>\n<p>ledOn++;<\/p>\n<p>if (ledOn &gt; led4) {<\/p>\n<p>ledOn = led1;<\/p>\n<p>}<\/p>\n<p>}<\/p><\/blockquote>\n<p>Both of these compile without a warning\/error:<\/p>\n<blockquote><p>\/\/ good<\/p>\n<p>attachInterrupt(digitalPinToInterrupt(toggle_color), switchLed, RISING);<\/p>\n<p>\/\/ bad, there will be no interrupt service \u2014 can you see why?<\/p>\n<p>attachInterrupt(digitalPinToInterrupt(toggle_color), switchLeds, RISING);<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>Arduino models can support interrupts, but often on different pins:<\/p>\n<p><a href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/functions\/external-interrupts\/attachinterrupt\">https:\/\/www.arduino.cc\/reference\/en\/language\/functions\/external-interrupts\/attachinterrupt<\/a>\/<\/p>\n<p>Nick Gammon has an intensive, CS-focused page on interrupts:\u00a0 <a href=\"https:\/\/gammon.com.au\/interrupts\">https:\/\/gammon.com.au\/interrupts<\/a><\/p>\n<p>The simple example I showed in class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">int led1 = 8;\r\nint led2 = 9;\r\nint led3 = 10;\r\nint led4 = 11;\r\n\r\nint ledOn = 0;\r\n\r\n\r\nvolatile int toggle_color = 2;\r\nvolatile int toggle_speed = 3;\r\n\r\nint ledDelay = 250;\r\n\r\nvoid setup() {\r\n\r\n    pinMode(led1, OUTPUT);\r\n    pinMode(led2, OUTPUT);\r\n    pinMode(led3, OUTPUT);\r\n    pinMode(led4, OUTPUT);\r\n\r\n    pinMode(toggle_color, INPUT);\r\n    pinMode(toggle_speed, INPUT);\r\n    attachInterrupt(digitalPinToInterrupt(toggle_color), switchLed, RISING);\r\n    attachInterrupt(digitalPinToInterrupt(toggle_speed), changeSpeed, RISING);\r\n\r\n    ledOn = led1;\r\n    \r\n    Serial.begin(9600);\r\n}\r\n\r\nvoid switchLed() {\r\n    ledOn++;\r\n    if (ledOn &gt; led4) {\r\n        ledOn = led1;\r\n    }\r\n}\r\n\r\nvoid changeSpeed() {\r\n    ledDelay = ledDelay * 2;\r\n    if (ledDelay &gt; 2000) {\r\n        ledDelay = 250;\r\n    }\r\n   \r\n}\r\n\r\nvoid loop() {\r\n\r\n    digitalWrite(ledOn, HIGH);\r\n    delay(ledDelay);\r\n    digitalWrite(ledOn, LOW);\r\n    delay(ledDelay);\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h2>Assignment<\/h2>\n<p>Find some examples of interrupts in your daily life, complex and simple, post to Looking Outward.<\/p>\n<p>Reading: Make It So Forward, Chap 1, Chap 2.\u00a0 How did we think about the future in the past?\u00a0 Post a few notes to the blog (Assignment 2) and we&#8217;ll talk about these at the start of class.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Class 3: 25 Jan &#8211; Intro to Interrupts Office hours start next week on campus in A10 at 5pm. Review assignments Design for Dreaming \u2014 good?\u00a0 Bad?\u00a0 Socially uncomfortable? revisit tigoe\u2019s page \u2014 what was physical interaction and what was reaction?\u00a0 Do you see any themes in projects that aren\u2019t allowed? \u00a0 Is there something &hellip; <a href=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2134\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Class Notes, 25 Jan 2022&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2134"}],"collection":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2134"}],"version-history":[{"count":3,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2134\/revisions"}],"predecessor-version":[{"id":2139,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2134\/revisions\/2139"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}