// Currently a WOKWI project 3/9/22
// https://wokwi.com/projects/325788822871736915
class SimpleFlasher {
const bool _IS_INPUT_INVERTED;
const uint8_t _PIN_TO_FLASH;
const bool _IS_OUTPUT_INVERTED;
const unsigned long _T1_Preset;
unsigned long _T2_Preset;
unsigned long _timerAcc;
enum : uint8_t {begin_T1_time, dwell_T1_time_begin_T2_time, dwell_T2_time}
_flashState;
public:
void blinkIt(bool _enabled) {
//
// Turns an output ON and OFF at a given rate based on millis() timing.
// ON time, OFF time, enabled state and output polarity are user selectable.
//
if (_enabled != _IS_INPUT_INVERTED) { // J-M-L version
switch (_flashState) {
case begin_T1_time:
digitalWrite(_PIN_TO_FLASH, (_IS_OUTPUT_INVERTED ? LOW : HIGH));
_timerAcc = millis(); // set the timer for _T1_Preset
_flashState = dwell_T1_time_begin_T2_time;
break;
case dwell_T1_time_begin_T2_time:
if (millis() - _timerAcc >= _T1_Preset) {
digitalWrite(_PIN_TO_FLASH, (_IS_OUTPUT_INVERTED ? HIGH : LOW));
_timerAcc = millis(); // Reset the timer for _T2_Preset
_flashState = dwell_T2_time;
}
break;
case dwell_T2_time:
if (millis() - _timerAcc >= _T2_Preset) _flashState = begin_T1_time;
break;
default:
_flashState = begin_T1_time;
} // End of switch
} else {
digitalWrite(_PIN_TO_FLASH, (_IS_OUTPUT_INVERTED ? LOW : HIGH));
_flashState = begin_T1_time;
}
} // end of blinkIt
void begin() {
//
// Set the selected pin as an output
//
pinMode(_PIN_TO_FLASH, OUTPUT);
} // end of begin
//
// Constructors with initialization list
//
// When T1 and T2 both specified
//
SimpleFlasher(bool inputInvertedIn, uint8_t outputPinIn, uint8_t outputInvertedIn,
unsigned long T1_PresetIn, unsigned long T2_PresetIn)
// Initialization list
: _IS_INPUT_INVERTED(inputInvertedIn), _PIN_TO_FLASH(outputPinIn),
_IS_OUTPUT_INVERTED(outputInvertedIn), _T1_Preset(T1_PresetIn), _T2_Preset(T2_PresetIn)
{
/* empty constructor body */
}
//
// If only T1 is specified T2 is set equal to T1
//
SimpleFlasher(bool inputInvertedIn, uint8_t outputPinIn, uint8_t outputInvertedIn,
unsigned long T1_PresetIn)
// Initialization list
: _IS_INPUT_INVERTED(inputInvertedIn), _PIN_TO_FLASH(outputPinIn),
_IS_OUTPUT_INVERTED(outputInvertedIn), _T1_Preset(T1_PresetIn)
{
_T2_Preset = _T1_Preset;
}
}; // end of class SimpleFlasher
//-----------------------------------------------------------------
const uint8_t orangeLED = 4;
const uint8_t greenLED = 2;
const uint8_t blueLED = 3;
const uint8_t yellowLED = 7;
const uint8_t orangeSwitch = A0;
const uint8_t greenSwitch = A1;
const uint8_t blueSwitch = A2;
const uint8_t yellowPot = A3;
// Following the form: input inverted, output pin, output inverted, T1 ms, T2 ms.
// T2 is optional; if T2 not included ON and OFF times will be equal.
// The input inverted option allows you to adapt to whether the controlling signal
// is normally true or normally false. The output inverted option allows you to
// use either common cathode or common anode LED wiring.
//
// Note! Designating the same output pin on multiple objects with will cause
// undesired effects! Check that all outputs specified are unique to
// their respective object.
SimpleFlasher orangeFlasher(false, orangeLED, false, 337, 2107); // Instantiate a flasher object
SimpleFlasher greenFlasher(true, greenLED, false, 100, 3000); // Instantiate a flasher object
SimpleFlasher blueFlasher(false, blueLED, false, 273); // Instantiate a flasher object
SimpleFlasher yellowFlasher(true, yellowLED, false, 200, 2100); // Instantiate a flasher object
void setup() {
Serial.begin(115200);
pinMode(orangeSwitch, INPUT_PULLUP);
pinMode(greenSwitch, INPUT_PULLUP);
pinMode(blueSwitch, INPUT_PULLUP);
// begin methods configure the outputs
orangeFlasher.begin();
greenFlasher.begin();
blueFlasher.begin();
yellowFlasher.begin();
}
void loop() {
orangeFlasher.blinkIt(bool(digitalRead(orangeSwitch)));
greenFlasher.blinkIt(bool(digitalRead(greenSwitch)));
blueFlasher.blinkIt(bool(digitalRead(blueSwitch)));
yellowFlasher.blinkIt(bool(analogRead(yellowPot) > 600 ? true : false));
// Serial.println(digitalRead(blueSwitch));
}