/** FlashingLedMethods.ino 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 "millisDelay.h" int pin; // compiler defaults to 0 if not otherwise set bool pinHigh; // compiler defaults to false if not otherwise set, is the pin High or Low millisDelay pinToggleDelay; unsigned long PIN_TOGGLE_DELAY_MS = 1000; // on/delay in mS == 1sec millisDelay stopTogglingDelay; unsigned long STOP_TOGGLING_DELAY_MS = 8000; // stop flashing in 8sec void startTogglingPin() { pinToggleDelay.start(PIN_TOGGLE_DELAY_MS); // start led flash delay digitalWrite(pin, HIGH); // turn led on pinHigh = true; // on } void stopTogglingPin() { pinToggleDelay.stop(); // stop the led flash delay digitalWrite(pin, LOW); // turn led off pinHigh = false; // off } void togglePinInit(int pinNo) { pin = pinNo; pinMode(pin, OUTPUT); // set output stopTogglingPin(); } void handleTogglingPin() { if (pinToggleDelay.justFinished()) { pinToggleDelay.restart(); // restart it pinHigh = !pinHigh; // toggle pinHigh if (pinHigh) { digitalWrite(pin, HIGH); // turn on } else { digitalWrite(pin, LOW); // turn off } } } void setup() { togglePinInit(13); // built in led startTogglingPin(); stopTogglingDelay.start(STOP_TOGGLING_DELAY_MS); } void loop() { handleTogglingPin(); // check if led should be toggled if (stopTogglingDelay.justFinished()) { stopTogglingPin(); } }