I’ve added a tool that shows up in the Post editor as the icon “{…}”. To post the code below, I clicked on that icon, pasted in my code, turned off indentation, then selected C++, and hit “OK”.
// -*-c++-*- /* The MIT License (MIT) Copyright (c) 2017 J. Eric Townsend Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* using interrupts to stop/alter a process that's eating up the loop() cycle with delays */ static const int potOnPin = A1; static const int potOffPin = A1; static const int togglePin = 2; static const int LED1Pin = 5; static const int LED2Pin = 6; static const int LED3Pin = 9; int LEDOnDuration = 1000; // milliseconds int LEDOffDuration = 1000; // milliseconds long LEDOnTime = 0; long LEDOffTime = 0;; bool LEDState = false; bool LEDStatePrev = false; int toggleState = 0; bool flagState = false; const bool isInterrupt = true; // TODO: how do we stop the for() loops so we can do another analogRead()? void SwitchPressed() { // we can set the delay to 0 and fall out of the loops //LEDOnDuration = 0; //LEDOffDuration = 0; // or we can modify a flag variable and use that to exit the loop flagState = true; } void setup() { Serial.begin(115200); pinMode(potOnPin, INPUT); pinMode(potOffPin, INPUT); pinMode(togglePin, INPUT); pinMode(LED1Pin, OUTPUT); pinMode(LED2Pin, OUTPUT); pinMode(LED3Pin, OUTPUT); if (isInterrupt) { attachInterrupt (digitalPinToInterrupt(togglePin), SwitchPressed, CHANGE); } } void loop() { if (!isInterrupt) { flagState = digitalRead(togglePin); } if (flagState) { Serial.println("flagState true"); } // take input from the pots to determine on/off times int pot = analogRead(potOnPin); if (pot != LEDOnDuration) { //Serial.print("on: "); //Serial.println(pot); LEDOnDuration = map(pot, 0, 1024, 0, 255); } pot = analogRead(potOffPin); if (pot != LEDOffDuration) { // Serial.print("off: "); // Serial.println(pot); LEDOffDuration = map(pot, 0, 1024, 0, 255); } // now let's write some really bad code // turn the LEDs on for (int i = 0; i < 255; i++) { if (flagState) { break; // is more reliable than setting i = 256; // if we set flagState back to false here we // won't make a change in the next loop where we // turn LEDs off } else { analogWrite(LED1Pin, i); analogWrite(LED2Pin, i); analogWrite(LED3Pin, i); delay(LEDOnDuration); } } // turn the LEDs off for (int i = 255; i > 0; i--) { if (flagState) { break; // is more reliable than setting i = 256; // if we set flagState back to false here we // won't make a change in the next loop where we // turn LEDs on } else { analogWrite(LED1Pin, i); analogWrite(LED2Pin, i); analogWrite(LED3Pin, i); delay(LEDOffDuration); } } // this is the proper place to turn off the flagState. // We could do this with just a statement and never check // its status, but using a test makes debugging easier if (flagState == true) { flagState = false; // because we could add this Serial.println("flagState was on, now turned off"); } }