// Currently a WOKWI project 3/9/22
// https://wokwi.com/projects/324335660798313044
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) {
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 blinkIt(bool _enabled) { // This is where the flashing takes place
// if (_enabled != _IS_INPUT_INVERTED) {
// switch (_flashState) {
// case begin_T1_time:
// digitalWrite(_PIN_TO_FLASH, (_IS_OUTPUT_INVERTED ? LOW : HIGH));
// _timerAcc = millis(); // Reset the timer
// _flashState = dwell_T1_time_begin_T2_time;
// case dwell_T1_time_begin_T2_time:
// if (millis() - _timerAcc < _T1_Preset)
// break;
// [[fallthrough]];
// // Begin T2 time:
// digitalWrite(_PIN_TO_FLASH, (_IS_OUTPUT_INVERTED ? HIGH : LOW));
// _timerAcc = millis(); // Reset the timer
// _flashState = dwell_T2_time;
// [[fallthrough]];
// case dwell_T2_time:
// if (millis() - _timerAcc < _T2_Preset)
// break;
// _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.
//
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, true, 647); // 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));
}