/** Toggle.cpp by Matthew Ford, 2020/01/27 (c)2019 Forward Computing and Control Pty. Ltd. NSW, Australia www.forward.com.au */ // this sketch runs on Uno and most other Arduino boards // it uses millisDelay from https://www.forward.com.au/pfod/ArduinoProgramming/TimingDelaysInArduino.html // download and install the millisDelay library https://www.forward.com.au/pfod/ArduinoProgramming/millisDelay.zip #include "Arduino.h" // for all the 'standard' Arduino stuff like OUTPUT #include "millisDelay.h" #include "Toggle.h" #include "DebugIO.h" // uncomment the #define DEBUG and call setDebugPtr() in DebugIO, if you want to debug this file #define DEBUG // call this to start toggling void Toggle::startTogglingPin() { if (pin < 0) { return; //pin not set } if (debugPtr) { // will skip debugPtr if either i) debugPtr is not defined OR setDebugPtr() has not been called debugPtr->print(F("start Toggling Pin:")); debugPtr->println(pin); } pinToggleDelay.start(PIN_TOGGLE_DELAY_MS); // start led flash delay digitalWrite(pin, HIGH); // turn led on pinHigh = true; // on } // call this to stop toggling void Toggle::stopTogglingPin() { if (pin < 0) { return; //pin not set } if (debugPtr) { // will skip debugPtr if either i) debugPtr is not defined OR setDebugPtr() has not been called debugPtr->print(F("stop Toggling Pin:")); debugPtr->println(pin); } pinToggleDelay.stop(); // stop the led flash delay digitalWrite(pin, LOW); // turn led off pinHigh = false; // off } void Toggle::togglePinInit(int pinNo) { #ifdef DEBUG debugPtr = getDebugPtr(); // will return null if setDebugPtr() has not been called #endif if (debugPtr) { // will skip debugPtr if either i) debugPtr is not defined OR setdebugPtr() has not been called debugPtr->print(F("Initializing Toggling for pin : ")); debugPtr->println(pinNo); } pin = pinNo; if (pin >= 0) { pinMode(pin, OUTPUT); // set output stopTogglingPin(); } } // call this from loop() to check if pin should toggle void Toggle::handleTogglingPin() { // this get called often so be careful about debugPtr prints here if (pin < 0) { return; //pin not set } if (pinToggleDelay.justFinished()) { pinToggleDelay.restart(); // restart it pinHigh = !pinHigh; // toggle pinHigh if (debugPtr) { // will skip debugPtr if either i) debugPtr is not defined OR setDebugPtr() has not been called debugPtr->print(F("pin:")); debugPtr->print(pin); debugPtr->println(pinHigh ? " H" : " L"); } if (pinHigh) { digitalWrite(pin, HIGH); // turn on } else { digitalWrite(pin, LOW); // turn off } } } /* setTogglingSpeed call this to change the flash speed @param mSDelay -- the new delay in milliseconds */ void Toggle::setTogglingSpeed(unsigned long mSDelay) { if (debugPtr) { // will skip debugPtr if either i) debugPtr is not defined OR setDebugPtr() has not been called debugPtr->print(F("setTogglingSpeed for pin:")); debugPtr->print(pin); debugPtr->print(F(" to ")); debugPtr->print(mSDelay); debugPtr->println(F("mS")); } PIN_TOGGLE_DELAY_MS = mSDelay; if (pinToggleDelay.isRunning()) { // restart it now with new delay pinToggleDelay.start(PIN_TOGGLE_DELAY_MS); } }