{"id":2177,"date":"2022-01-31T21:28:36","date_gmt":"2022-02-01T02:28:36","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2177"},"modified":"2022-01-31T21:28:36","modified_gmt":"2022-02-01T02:28:36","slug":"fun-with-interrupts-james-kyle","status":"publish","type":"post","link":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2177","title":{"rendered":"Fun With Interrupts &#8211; James Kyle"},"content":{"rendered":"<p><span style=\"text-decoration: underline\"><strong>How it works:<\/strong><\/span><\/p>\n<p>I played around with buttons switches and potentiometers to change light blinking patterns and came up with this circuit. The switch has an interrupt which changes whether all lights blink at the same time or different times (a blink mode). The button changes which light blinks in the event that they are not blinking at the same time. The potentiometer changes the speed at which the lights blink.<\/p>\n<p>I had some trouble with getting the switch interrupt to work more than once and I ended up having to place it before the button interrupt which I assume prioritizes it.<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-decoration: underline\"><strong>Code:<\/strong><\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">\r\nint toggle_color = 3;\r\nint GREEN_LED = 7;\r\nint YELLOW_LED = 6;\r\nint RED_LED = 5;\r\nint POT = A0;\r\nint blink_rate = 250;\r\nint toggle_blinkMode = 2;\r\n\r\n\r\n\/\/LED blinking variables\r\nvolatile int LED_pin = 5;\r\nvolatile bool individual_blink;\r\n\r\n\r\n\/\/Debounce variables\r\nvolatile unsigned long last_press = 0;\r\nint wait_time = 50;\r\n\r\n\r\n\r\n\r\nvoid setup() {\r\n\r\n  Serial.begin(9600);\r\n\r\n  \/\/Initializing Pins\r\n  pinMode(GREEN_LED, OUTPUT);\r\n  pinMode(YELLOW_LED, OUTPUT);\r\n  pinMode(RED_LED, OUTPUT);\r\n\r\n  pinMode(toggle_color, INPUT_PULLUP);\r\n  pinMode(POT, INPUT);\r\n\r\n\r\n  \/\/Interrupt initialization\r\n  attachInterrupt(digitalPinToInterrupt(toggle_blinkMode), blinkMode, CHANGE);\r\n  attachInterrupt(digitalPinToInterrupt(toggle_color), switchLED, CHANGE);\r\n\r\n\r\n}\r\n\r\nvoid switchLED() {\r\n\r\n  \/\/Turn off previous light\r\n  digitalWrite(LED_pin, LOW);\r\n\r\n  \/\/Debounce\r\n  if ((millis() - last_press) &gt;= wait_time) {\r\n    LED_pin++;\r\n    last_press = millis();\r\n  }\r\n\r\n  \/\/Go back to first light\r\n  if (LED_pin &gt; 7) {\r\n    LED_pin = 5;\r\n  }\r\n\r\n\r\n}\r\n\r\nvoid blinkMode() {\r\n\r\n  if (individual_blink) {\r\n    individual_blink = 0;\r\n  } else {\r\n    individual_blink = 1;\r\n  }\r\n\r\n}\r\n\r\nvoid loop() {\r\n\r\n  blink_rate = map(analogRead(POT), 0, 255, 100, 500);\r\n\r\n  if (individual_blink) {\r\n    digitalWrite(LED_pin, HIGH);\r\n    delay(blink_rate);\r\n    digitalWrite(LED_pin, LOW);\r\n    delay(blink_rate);\r\n  } else {\r\n    digitalWrite(RED_LED, HIGH);\r\n    digitalWrite(GREEN_LED, HIGH);\r\n    digitalWrite(YELLOW_LED, HIGH);\r\n    delay(blink_rate);\r\n    digitalWrite(RED_LED, LOW);\r\n    digitalWrite(GREEN_LED, LOW);\r\n    digitalWrite(YELLOW_LED, LOW);\r\n    delay(blink_rate);\r\n  }\r\n\r\n  bool value = individual_blink;\r\n  Serial.println(value);\r\n\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How it works: I played around with buttons switches and potentiometers to change light blinking patterns and came up with this circuit. The switch has an interrupt which changes whether all lights blink at the same time or different times (a blink mode). The button changes which light blinks in the event that they are &hellip; <a href=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2177\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Fun With Interrupts &#8211; James Kyle&#8221;<\/span><\/a><\/p>\n","protected":false},"author":29,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[],"_links":{"self":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2177"}],"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\/29"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2177"}],"version-history":[{"count":3,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2177\/revisions"}],"predecessor-version":[{"id":2190,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2177\/revisions\/2190"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}