/*
WOKWI demo program for 'Simple_LED_Flasher' with counted repetitions
Demonstrates:
- various combinations of inverted/non-inverted inputs
and outputs
- one and two time value entries.
- runtime change of timer values
- use of getCycleComplete() boolean to increment a counter
(value presented on TM1637 display)
- blinking an LED a preset number of times and runtime change of
the preset number
- use of getRepsComplete() to flash an LED
*/
#include "Simple_LED_Flasher.h"
#include <TM1637.h> // Grove 4-digit display driver
const int CLK = 6; // TM1637 control pins
const int DIO = 5; //
TM1637 tm(CLK, DIO); // Grove four-digit seven-segment display module
/*
Logic equivalents for input and output inversion/non-inversion
?? Move this to header ??
*/
const bool INPUT_INVERTED = true;
const bool INPUT_NON_INVERTED = false;
const bool TRIGGER_ON_LOW = false;
const bool TRIGGER_ON_HIGH = true;
const bool OUTPUT_INVERTED = true;
const bool OUTPUT_NON_INVERTED = false;
/*
I/O pin assignments
*/
const int ORANGE_LED = 4;
const int GREEN_LED = 2;
const int BLUE_LED = 3;
const int YELLOW_LED = 7;
const int WHITE_LED = 8;
const int BLACK_LED = 10;
const int RED_LED = LED_BUILTIN;
const int MAGENTA_LED = 9;
const int ORANGE_SWITCH = A0;
const int GREEN_SWITCH = A1;
const int BLUE_SWITCH = A2;
const int YELLOW_POT = A3;
const int BLACK_BUTTON = 11;
const int WHITE_BUTTON = 12;
/*
Variables
*/
int cycleCounter; // for presentation on the 4-digit display
/* The constructor parameters are:
input inverted true/false, output pin, output inverted true/false,
t1 ms, t2 ms.
t2 is optional, if t2 is not included ON and OFF times will be set
to the value given for t1. 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 wire the LED either
common cathode or common anode.
The method [name].getCycleComplete() gives access to a boolean which is true
for one scan at the end of each on/off cycle. This does not happen if the
cycle is terminated by the input condition going false.
The method [name].setTime() allows runtime changes to t1 and t2. The change
takes effect immediately upon execution of setTime().
The method [name].setReps() sets the number of times a given output will
flash when the input undergoes the correct state change.
Note! Designating the same output pin on multiple objects may cause
undesired effects! Check that all outputs specified are unique to
their respective object.
Instantiate seven flasher objects
*/
uint32_t Simple_LED_Flasher::m_scratchpad_millis; // One copy of millis for all.
/*
On and off time both specified
*/
Simple_LED_Flasher orange_flasher(INPUT_NON_INVERTED, ORANGE_LED, OUTPUT_NON_INVERTED, 237, 510);
/*
On and off time both specified
*/
Simple_LED_Flasher green_flasher(INPUT_NON_INVERTED, GREEN_LED, OUTPUT_INVERTED, 300, 3000);
/*
Symmetrical on/off times
*/
Simple_LED_Flasher blue_flasher(INPUT_INVERTED, BLUE_LED, OUTPUT_INVERTED, 573);
/*
On and off time both specified
*/
Simple_LED_Flasher yellow_flasher(INPUT_INVERTED, YELLOW_LED, OUTPUT_NON_INVERTED, 300, 400);
/*
Demonstrates use of setTime() feature. Start out with symmetrical
on / off times.
*/
Simple_LED_Flasher white_flasher(INPUT_NON_INVERTED, WHITE_LED, OUTPUT_NON_INVERTED, 2500);
/*
Black flasher demonstrates flashing a set number of times.
*/
Simple_LED_Flasher black_flasher(TRIGGER_ON_HIGH, BLACK_LED, OUTPUT_INVERTED, 200, 400);
/*
Symmetrical on/off times
*/
Simple_LED_Flasher magenta_flasher(INPUT_NON_INVERTED, MAGENTA_LED, OUTPUT_NON_INVERTED, 1000);
void setup() {
//Serial.begin(115200);
pinMode(ORANGE_SWITCH, INPUT_PULLUP);
pinMode(GREEN_SWITCH, INPUT_PULLUP);
pinMode(BLUE_SWITCH, INPUT_PULLUP);
pinMode(WHITE_BUTTON, INPUT_PULLUP);
pinMode(BLACK_BUTTON, INPUT_PULLUP);
pinMode(RED_LED, OUTPUT);
black_flasher.setReps(3);
tm.init(); // Start the four-digit display
tm.set(BRIGHT_TYPICAL); // Set display brightness
}
void loop() {
orange_flasher.setMillis();
orange_flasher.blink_it(bool(digitalRead(ORANGE_SWITCH)));
green_flasher.blink_it(bool(digitalRead(GREEN_SWITCH)));
blue_flasher.blink_it(bool(digitalRead(BLUE_SWITCH)));
yellow_flasher.blink_it(analogRead(YELLOW_POT) > 600 ? true : false);
white_flasher.blink_it(true); // no conditional since purpose is to demo time change
magenta_flasher.blinkNTimes(black_flasher.getRepsComplete());
if (blue_flasher.getCycleComplete()) cycleCounter++;
/*
Blink the black LED a preset number of times after the switch
is made or unmade. The library incorporates change-of-state detection
so only one sequence is triggered for each switch activation.
*/
black_flasher.blinkNTimes(digitalRead(BLACK_BUTTON));
// Change the timing on the white led
if (digitalRead(WHITE_BUTTON) == LOW) {
white_flasher.setTime(300, 400); // Alter white flasher timing
if (!black_flasher.setReps(cycleCounter) ) { // Set black flasher number of repetitions
// to the current cycleCounter value.
digitalWrite(RED_LED, HIGH); // turn on red LED if setReps() passed a zero to black_flasher
}
}
if (cycleCounter > 9) {
cycleCounter = 0;
white_flasher.setTime(1000, 1500); // Alter white flasher timing
digitalWrite(RED_LED, LOW);
}
/*
Display a counter value as evidence of operation
*/
int divisor = 1000;
for (uint8_t i = 0; i < 4; i++) {
tm.display(i, (cycleCounter / divisor) % 10);
divisor /= 10;
}
}
Red = zero repetitions attempted V
Start repetitions
Set repetitions count
Magenta = repetitions complete V
Counter increments on blue transition