{"id":2176,"date":"2022-01-31T20:38:10","date_gmt":"2022-02-01T01:38:10","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2176"},"modified":"2022-04-04T16:12:53","modified_gmt":"2022-04-04T20:12:53","slug":"messing-around-with-interrupts","status":"publish","type":"post","link":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2176","title":{"rendered":"Messing Around With Interrupts"},"content":{"rendered":"<h2>Messing Around With Interrupts:<\/h2>\n<h5>Functionality:<\/h5>\n<p>There are two different states that the system can be put into. These states are controlled by an interrupt connected to the switch. When the switch is high, the system goes into adjustment mode where the user can use the potentiometer to adjust the brightness of the LED. This adjustment is only possible while in this state. When the switch is low, the system returns to its normal state where there is a blinking mode and solid mode. These modes are controlled by a second interrupt tied to the button. While in the blinking state, the LED&#8217;s flash individually for 0.5s each in a sequence of red to yellow to green and back to red. In the solid state, all of the LED&#8217;s remain on. The brightness of the LED&#8217;s in this mode is also the same as that set in the previous adjustment state.<\/p>\n<h5>Code:<\/h5>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"eclipse\">\/*  Interrupt Assignment \r\n *  Judson Kyle \r\n *  judsonk \r\n *  01\/31\/2021 \r\n*\/\r\n#define POT_PIN     A0\r\n#define SWITCH      3\r\n#define BUTTON      2\r\n\r\n#define RED_LED     11\r\n#define YELLOW_LED  10\r\n#define GREEN_LED   9\r\n\r\n#define RED     0\r\n#define YELLOW  1\r\n#define GREEN   2\r\n\r\nvolatile bool ADJUST_STATE = false;\r\nvolatile bool BLINK = false;\r\n\r\nint potVal = 0;\r\ndouble ledBrightness;\r\n\r\nint currentLED = 0;\r\n\r\nint waitTime = 500; \/\/ms\r\nlong long startTime;\r\n\r\nint buttonDebounceTime = 300;\r\nlong long buttonDebounceStart = 0;\r\nvolatile bool BUTTON_DEBOUNCE = false;\r\nbool BUTTON_PREV_DEBOUNCE = false;\r\n\r\n\r\nvoid setup() {\r\n  \r\n  pinMode(POT_PIN, INPUT);\r\n  pinMode(SWITCH, INPUT);\r\n\r\n  \/\/Interrupts\r\n  attachInterrupt(digitalPinToInterrupt(BUTTON), handleButton, RISING);\r\n\r\n  \/\/LED's\r\n  pinMode(RED_LED, OUTPUT);\r\n  pinMode(YELLOW_LED, OUTPUT);\r\n  pinMode(GREEN_LED, OUTPUT);\r\n\r\n  Serial.begin(9600);\r\n\r\n}\r\n\r\nvoid handleButton() {\r\n  if (!BUTTON_DEBOUNCE) {\r\n    BUTTON_DEBOUNCE = true;\r\n    BLINK = !BLINK;\r\n  }\r\n}\r\n\r\nvoid loop() {\r\n\r\n  \/\/Read switch state\r\n  ADJUST_STATE = digitalRead(SWITCH);\r\n  Serial.println(ADJUST_STATE);\r\n  \r\n\/\/  Serial.print(\"DEBOUNCE: \\t\");\r\n\/\/  Serial.print(DEBOUNCE);\r\n\/\/\r\n\/\/  Serial.print(\"\\tdebounceStart: \\t\");\r\n\/\/  Serial.print((int)debounceStart);\r\n\/\/\r\n\/\/  Serial.print(\"\\tTime Elapsed: \\t\");\r\n\/\/  Serial.println(millis() - (int)debounceStart);\r\n\r\n  \/\/Process switch logic\r\n  if (ADJUST_STATE) {\r\n    potVal = analogRead(POT_PIN);\r\n    ledBrightness = (256*potVal)\/1024;\r\n    analogWrite(RED_LED, (int)ledBrightness);\r\n    analogWrite(YELLOW_LED, (int)ledBrightness);\r\n    analogWrite(GREEN_LED, (int)ledBrightness);\r\n  }\r\n\r\n  \/\/Change brightness of LED based off potentiometer switch when not in blink mode\r\n  if (BLINK) {\r\n    if (millis() - startTime &gt; waitTime) {\r\n      startTime = millis();\r\n      currentLED++;\r\n      if (currentLED &gt;= 3) {\r\n        currentLED = 0;\r\n      }\r\n    }\r\n    \r\n    switch(currentLED) {\r\n      case RED:\r\n        analogWrite(RED_LED, (int)ledBrightness);\r\n        analogWrite(YELLOW_LED, LOW);\r\n        analogWrite(GREEN_LED, LOW);\r\n        break;\r\n      case YELLOW:\r\n        analogWrite(RED_LED, LOW);\r\n        analogWrite(YELLOW_LED, (int)ledBrightness);\r\n        analogWrite(GREEN_LED, LOW);\r\n        break;\r\n      case GREEN:\r\n        analogWrite(RED_LED, LOW);\r\n        analogWrite(YELLOW_LED, LOW);\r\n        analogWrite(GREEN_LED, (int)ledBrightness);\r\n        break;\r\n      default:\r\n        break;\r\n    }\r\n  }\r\n  else {  \/\/If not in blinking mode make all LED's light up\r\n    analogWrite(RED_LED, (int)ledBrightness);\r\n    analogWrite(YELLOW_LED, (int)ledBrightness);\r\n    analogWrite(GREEN_LED, (int)ledBrightness);\r\n  }\r\n\r\n  \/\/Handle button debounce\r\n  if (BUTTON_DEBOUNCE &amp;&amp; !BUTTON_PREV_DEBOUNCE) {\r\n    buttonDebounceStart = millis();\r\n  }\r\n  if (abs(buttonDebounceStart - millis()) &gt; buttonDebounceTime) {\r\n    BUTTON_DEBOUNCE = false;\r\n  }\r\n  BUTTON_PREV_DEBOUNCE = BUTTON_DEBOUNCE;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h5>Images:<\/h5>\n<div style=\"width: 840px;\" class=\"wp-video\"><!--[if lt IE 9]><script>document.createElement('video');<\/script><![endif]-->\n<video class=\"wp-video-shortcode\" id=\"video-2176-1\" width=\"840\" height=\"961\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/01\/Interrupt_Assignment_Demo.mp4?_=1\" \/><a href=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/01\/Interrupt_Assignment_Demo.mp4\">https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/01\/Interrupt_Assignment_Demo.mp4<\/a><\/video><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Messing Around With Interrupts: Functionality: There are two different states that the system can be put into. These states are controlled by an interrupt connected to the switch. When the switch is high, the system goes into adjustment mode where the user can use the potentiometer to adjust the brightness of the LED. This adjustment &hellip; <a href=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2176\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Messing Around With Interrupts&#8221;<\/span><\/a><\/p>\n","protected":false},"author":30,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[10,5,1],"tags":[],"_links":{"self":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2176"}],"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\/30"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2176"}],"version-history":[{"count":7,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2176\/revisions"}],"predecessor-version":[{"id":2189,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2176\/revisions\/2189"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}