{"id":1619,"date":"2020-10-15T21:34:47","date_gmt":"2020-10-16T01:34:47","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/48-339\/f2020\/?p=1619"},"modified":"2020-10-15T21:37:13","modified_gmt":"2020-10-16T01:37:13","slug":"arduino-source-formatting-tool","status":"publish","type":"post","link":"https:\/\/courses.ideate.cmu.edu\/48-339\/f2020\/?p=1619","title":{"rendered":"Arduino source formatting tool"},"content":{"rendered":"<p>I&#8217;ve added a tool that shows up in the Post editor as the icon &#8220;{&#8230;}&#8221;.\u00a0\u00a0 To post the code below, I clicked on that icon, pasted in my code, turned off indentation, then selected C++, and hit &#8220;OK&#8221;.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">\/\/ -*-c++-*-\r\n\/*\r\n  The MIT License (MIT)\r\n\r\n  Copyright (c) 2017 J. Eric Townsend\r\n\r\n  Permission is hereby granted, free of charge, to any person obtaining a copy\r\n  of this software and associated documentation files (the \"Software\"), to deal\r\n  in the Software without restriction, including without limitation the rights\r\n  to use, copy, modify, merge, publish, distribute, sublicense, and\/or sell\r\n  copies of the Software, and to permit persons to whom the Software is\r\n  furnished to do so, subject to the following conditions:\r\n\r\n  The above copyright notice and this permission notice shall be included in\r\n  all copies or substantial portions of the Software.\r\n\r\n  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r\n  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r\n  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r\n  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r\n  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r\n  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r\n  THE SOFTWARE.\r\n*\/\r\n\r\n\/*\r\n  using interrupts to stop\/alter a process that's eating up the loop()\r\ncycle with delays\r\n\r\n *\/\r\n\r\nstatic const int potOnPin = A1;\r\nstatic const int potOffPin = A1;\r\n\r\n\r\nstatic const int togglePin = 2;\r\n\r\nstatic const int LED1Pin = 5;\r\nstatic const int LED2Pin = 6;\r\nstatic const int LED3Pin = 9;\r\n\r\nint LEDOnDuration = 1000;  \/\/ milliseconds\r\nint LEDOffDuration = 1000;  \/\/ milliseconds\r\nlong LEDOnTime = 0;\r\nlong LEDOffTime = 0;;\r\nbool LEDState = false;\r\nbool LEDStatePrev = false;\r\nint toggleState = 0;\r\n\r\nbool flagState = false;\r\nconst bool isInterrupt = true;\r\n\r\n\/\/ TODO: how do we stop the for() loops so we can do another analogRead()?\r\nvoid SwitchPressed()\r\n{\r\n    \/\/ we can set the delay to 0 and fall out of the loops\r\n  \/\/LEDOnDuration = 0;\r\n  \/\/LEDOffDuration = 0;\r\n    \/\/ or we can modify a flag variable and use that to exit the loop\r\n       flagState = true;\r\n}\r\n\r\nvoid setup() {\r\n  Serial.begin(115200);\r\n\r\n  pinMode(potOnPin, INPUT);\r\n  pinMode(potOffPin, INPUT);\r\n  pinMode(togglePin, INPUT);\r\n\r\n  pinMode(LED1Pin, OUTPUT);\r\n  pinMode(LED2Pin, OUTPUT);\r\n  pinMode(LED3Pin, OUTPUT);\r\n\r\n  if (isInterrupt) {\r\n    attachInterrupt (digitalPinToInterrupt(togglePin), SwitchPressed, CHANGE);\r\n  }\r\n}\r\n\r\nvoid loop() {\r\n\r\n  if (!isInterrupt) {\r\n    flagState = digitalRead(togglePin);\r\n  }\r\n\r\n  if (flagState) {\r\n    Serial.println(\"flagState true\");\r\n  }\r\n\r\n  \/\/ take input from the pots to determine on\/off times\r\n  int pot = analogRead(potOnPin);\r\n  if (pot != LEDOnDuration) {\r\n   \/\/Serial.print(\"on: \");\r\n    \/\/Serial.println(pot);\r\n    LEDOnDuration = map(pot, 0, 1024, 0, 255);\r\n  }\r\n  pot = analogRead(potOffPin);\r\n  if (pot != LEDOffDuration) {\r\n   \/\/ Serial.print(\"off: \");\r\n   \/\/ Serial.println(pot);\r\n    LEDOffDuration = map(pot, 0, 1024, 0, 255);\r\n  }\r\n\r\n\r\n  \/\/ now let's write some really bad code\r\n  \/\/ turn the LEDs on\r\n  for (int i = 0; i &lt; 255; i++) {\r\n      if (flagState) {\r\n        break;\r\n  \/\/ is more reliable than setting i = 256;\r\n          \/\/ if we set flagState back to false here we\r\n          \/\/ won't make a change in the next loop where we\r\n          \/\/ turn LEDs off\r\n      }\r\n      else {\r\n          analogWrite(LED1Pin, i);\r\n          analogWrite(LED2Pin, i);\r\n          analogWrite(LED3Pin, i);\r\n          delay(LEDOnDuration);\r\n      }\r\n  }\r\n  \/\/ turn the LEDs off\r\n  for (int i = 255; i &gt; 0; i--) {\r\n      if (flagState) {\r\n          break;\r\n  \/\/ is more reliable than setting i = 256;\r\n          \/\/ if we set flagState back to false here we\r\n          \/\/ won't make a change in the next loop where we\r\n          \/\/ turn LEDs on\r\n      }\r\n      else {\r\n          analogWrite(LED1Pin, i);\r\n          analogWrite(LED2Pin, i);\r\n          analogWrite(LED3Pin, i);\r\n          delay(LEDOffDuration);\r\n      }\r\n  }\r\n\r\n  \/\/ this is the proper place to turn off the flagState.\r\n  \/\/ We could do this with just a statement and never check\r\n  \/\/ its status, but using a test makes debugging easier\r\n  if (flagState == true) {\r\n      flagState = false;\r\n      \/\/ because we could add this\r\n      Serial.println(\"flagState was on, now turned off\");\r\n  }\r\n\r\n}\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve added a tool that shows up in the Post editor as the icon &#8220;{&#8230;}&#8221;.\u00a0\u00a0 To post the code below, I clicked on that icon, pasted in my code, turned off indentation, then selected C++, and hit &#8220;OK&#8221;. &nbsp; \/\/ -*-c++-*- \/* The MIT License (MIT) Copyright (c) 2017 J. Eric Townsend Permission is hereby &hellip; <a href=\"https:\/\/courses.ideate.cmu.edu\/48-339\/f2020\/?p=1619\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Arduino source formatting tool&#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\/f2020\/index.php?rest_route=\/wp\/v2\/posts\/1619"}],"collection":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/f2020\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/f2020\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/f2020\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/f2020\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1619"}],"version-history":[{"count":5,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/f2020\/index.php?rest_route=\/wp\/v2\/posts\/1619\/revisions"}],"predecessor-version":[{"id":1624,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/f2020\/index.php?rest_route=\/wp\/v2\/posts\/1619\/revisions\/1624"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/f2020\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/f2020\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/f2020\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}