/*
Demo program for 'Simple_LED_Flasher'
Demonstrates various combinations of inverted/non-inverted inputs
and outputs and one and two time value entries.
Demonstrates use of getCycleComplete() boolean by
1. incrementing a counter and showing the counter
value on the TM1637 display
2. Flashing an LED X number of times by counting cycles complete
Demonstrates runtime change of timer values using method setTime().
*/
#include "Simple_LED_Flasher.h"
#include <TM1637.h>
const int CLK = 6; // TM1637 control pins
const int DIO = 5; //
TM1637 tm(CLK, DIO); // Grove four-digit seven-segment display module
/*
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 RED_LED = 9;
const int WHITE_LED = 8;
const int ORANGE_SWITCH = A0;
const int GREEN_SWITCH = A1;
const int BLUE_SWITCH = A2;
const int YELLOW_POT = A3;
const int RED_BUTTON = A5;
const int WHITE_BUTTON = 12;
/*
Variables
*/
int cycleCounter;
bool flash_flag;
int yellowButtonLastState;
int programmed_flash_preset = 3;
int programmed_flash_count;
/* Following the form: input inverted true/false, output pin, output inverted
true/false, T1 ms, T2 ms. T2 is optional; if T2 not included ON and OFF times
will be equal based on 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 goes 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().
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 six flasher objects
Input not inverted, output not inverted, T1 < T2
*/
Simple_LED_Flasher orange_flasher(false, ORANGE_LED, false, 1337, 2107);
/*
Input inverted, output not inverted, T1 < T2
*/
Simple_LED_Flasher green_flasher(true, GREEN_LED, false, 300, 3000);
/*
Input not inverted, output not inverted, symmetrical on/off times
*/
Simple_LED_Flasher blue_flasher(false, BLUE_LED, false, 573);
/*
Input inverted, output inverted, T1 > T2
*/
Simple_LED_Flasher yellow_flasher(true, YELLOW_LED, true, 300, 200);
/*
Input not inverted, output inverted, symmetrical on/off times
Demonstrates use of cycle complete
*/
Simple_LED_Flasher red_blink(false, RED_LED, true, 500);
/*
Demonstrate use of setTime() feature. Start out with symmetrical
on/off times.
*/
Simple_LED_Flasher white_flasher(false, WHITE_LED, false, 2500);
void setup() {
Serial.begin(115200);
pinMode(ORANGE_SWITCH, INPUT_PULLUP);
pinMode(GREEN_SWITCH, INPUT_PULLUP);
pinMode(BLUE_SWITCH, INPUT_PULLUP);
pinMode(RED_BUTTON, INPUT_PULLUP);
pinMode(WHITE_BUTTON, INPUT_PULLUP);
tm.init(); // Start the four-digit display
tm.set(BRIGHT_TYPICAL); // Set display brightness
}
void loop() {
static bool redLast; // change of state detector
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
if (blue_flasher.getCycleComplete()) cycleCounter++;
if (orange_flasher.getCycleComplete()) cycleCounter--;
/*
Blink the red LEDs programmed_flash_preset times after the switch is made.
The switch logic incorporates change of state detection so only one
sequence is triggered for each switch activation.
*/
if (digitalRead(RED_BUTTON) == LOW && redLast == true) {
programmed_flash_count = programmed_flash_preset; // load the counter
}
redLast = digitalRead(RED_BUTTON);
if (red_blink.getCycleComplete()) {
programmed_flash_count--; // decrement the counter
}
red_blink.blink_it(programmed_flash_count > 0); // test counter for completion
/*
Change the timing on the white led
*/
if (digitalRead(WHITE_BUTTON) == LOW) {
white_flasher.setTime(200);
}
/*
Display a counter value as evidence of operation
*/
tm.display(0, (cycleCounter / 1000) % 10);
tm.display(1, (cycleCounter / 100) % 10);
tm.display(2, (cycleCounter / 10) % 10);
tm.display(3, cycleCounter % 10);
}