// Setup using Robotdyn 4 digit 7 segment display
// Include the library: Display-master.h
#include <TM1637Display.h>
#include <toneAC.h>
// Define the display connections pins:
#define CLK 12 // sets TM1637 7 segment display CLK connection pin
#define DIO 11 // sets TM1637 7 segment display DIO connection pin
// Create display object of type TM1637Display:
TM1637Display display = TM1637Display(CLK, DIO);
// Encoder variables
#define DEBOUNCE 10 //Minimum time in mSec between rotary clicks being recognised
volatile int counter = 0, aState, aLastState ;
volatile unsigned long lastChange = 0 ;
bool changed = false;
unsigned long lastButtonPress = 0;
int enc_dt = 3; // Sets rotary encoder DT pin variable
int pinEnc_clk = 2; // Sets rotary encoder clock pin variable
int col_select = 4; // Sets rotary encoder PB switch pin variable
void encoderInterrupt() {
aState = digitalRead(pinEnc_clk); // Reads the "current" state of the pinEnc_clk
// If the previous and the current state of the pinEnc_clk are different, that means a Pulse has occured
if (aState != aLastState) {
// If the enc_dt state is different to the pinEnc_clk state, that means the encoder is rotating clockwise
if ((millis() - lastChange) > DEBOUNCE) { //Ignore repeated bounce of contacts
changed = true; //Set flag to say change has happened
if (digitalRead(enc_dt) != aState ) {
counter ++;
} else {
counter --;
}
}
}
lastChange = millis();
aLastState = aState; // Updates the previous state of the pinEnc_clk with the current state
}
int Statestart; // initialises start button variable
int Statestop; // initialises stop button variable
int Statepause; // initialises pause button variable
int Stateset; // initialises sepinMosfett button variable
int Stateselect = HIGH; // initialises encoder PB
int Set = HIGH; // initialise st routing status variable
int Statecolumn = HIGH; // initialises column select variable
int minsec = LOW; // initialises select display column variable
int old_minsec; // stores current value of minsec
// Global variables
int Mins = 2; // initialises minutes global variable
int Secs = 30; // initialises seconds global variable
int Total_time; // initialises total exposure time in S variable
// pin labels
int pinStart = 5; // assigns names to digital pins start timer PB
int pinPause_timer = 6; // assigns names to digital pins pause timer PB
int pinStop = 7; // assigns names to digital pins stop and reset timer PB
int pinSet = 8; // assigns names to digital pins enter / exit timer pinSet mode
int pinGreen_led = 13; // assigns names to digital pins Output to Green LED
int pinMosfet = 14; // assigns names to digital pins output to pinMosfet module
int pinRed_led = 15; // assigns names to digital pins Output to Red LED
void setup() {
// setup interrupt service for rotary encoder
pinMode(pinEnc_clk, INPUT_PULLUP); //Assigns data direction for digtal pins
pinMode(enc_dt, INPUT_PULLUP); //Assigns data direction for digtal pins
pinMode(col_select,INPUT_PULLUP); //Assigns data direction for digtal pins
// Reads the initial state of the pinEnc_clk
aLastState = digitalRead(pinEnc_clk);
attachInterrupt(digitalPinToInterrupt(pinEnc_clk), encoderInterrupt, CHANGE);
// initialise serial link:
Serial.begin (9600); // Initialises serial coms port at 9600 baud for debugging
// Clear the display and initialise:
display.clear();
delay(1000);
display.setBrightness(7); // Set the display brightness:
display.showNumberDecEx(Secs, 0b11100000, false, 4); // Display stored seconds with colon
display.showNumberDecEx(Mins, 0b11100000, false, 2); // Display stored minutes with colon
// setup input and output pins
pinMode(pinStart, INPUT_PULLUP); //Assigns data direction for digtal pins
pinMode(pinStop, INPUT_PULLUP); //Assigns data direction for digtal pins
pinMode(pinPause_timer, INPUT_PULLUP); //Assigns data direction for digtal pins
pinMode(pinSet, INPUT_PULLUP); //Assigns data direction for digtal pins
pinMode(pinMosfet, OUTPUT); //Assigns data direction for digtal pins
pinMode(pinGreen_led, OUTPUT); //Assigns data direction for digtal pins
pinMode(pinRed_led, OUTPUT); //Assigns data direction for digtal pins
digitalWrite(pinGreen_led, HIGH); // assigins starting value of green LED ouptut - forces it on
// indicates exposure unit is powered on and safe to open lid
// test sounder
toneAC(2000); // Generate tone at 2kHz
delay(1000); // Play tone for 1 second
toneAC(0); // turn off tone
Serial.println("Set up complete");
}
///
void loop() { // Main program loops around reading state of start and set buttons
Set_time();
/*
Statestart = digitalRead(pinStart); // read status of start switch
Stateset = digitalRead(pinSet); // read status of timer setting switch
if (Statestart == LOW) {
Exposure(); // On start button press run Exposure function
}
if (Stateset == LOW) {
Set_time(); // On set button press run Set_time function
}
*/
}
//////
void Exposure() // Countdown timer function
// Function to switch on UV light for required time as set in Mins & Secs.
// Detection of Stop and pause operations
// operates in 2 second units to cause colon to flash on and off at 1 second intervals
// uses data stored in Mins and Secs as starting point.
{
Total_time = (((60 * Mins) + Secs)); // Total time in S
int TMins = Mins; // local counter variable Minutes
int TSecs = Secs; // local counter variable Seconds
// Serial.println("Total time"); // debugging serial connection
// Serial.println(Total_time); // debugging serial connection
display.setBrightness(7); // set display on and set brightness
display.showNumberDecEx(Secs, 0b11100000, false, 4); // Display set seconds with colon
display.showNumberDecEx(Mins, 0b11100000, false, 2); // Display set minutes with colon
digitalWrite(pinMosfet, HIGH); // switch pinMosfet on for UV LEDs and
digitalWrite(pinRed_led, HIGH); // switch red LED on
digitalWrite(pinGreen_led, LOW); // switch green LED off
do { // countdown timing and time remaining display loop
//1st second
delay (1000); // wait 1 second
TSecs = TSecs - 1; // calculate new remaining time
if (TSecs < 0) { // manipulation to range seconds to 0 - 59 per minute
TSecs = 59;
TMins = TMins - 1;
}
display.showNumberDec(TSecs, false, 4); // display remaining seconds without colon
display.showNumberDec(TMins, false, 2); // display remaining minutes without colon
Statestop = digitalRead(pinStop); // reads status of stop switch
if (Statestop == LOW) { // handles stop command, exits function and resets timer to original setting
// in Mins and Secs
digitalWrite(pinMosfet, LOW); // Turns off pinMosfet and UV lights
digitalWrite(pinRed_led, LOW); // switch red LED off
digitalWrite(pinGreen_led, HIGH); // turns on green LED
display.showNumberDecEx(Secs, 0b11100000, false, 4); // display set seconds with colon
display.showNumberDecEx(Mins, 0b11100000, false, 2); // display set minutes with colon
return; // exits from function to main proggram
}
Statepause = digitalRead(pinPause_timer); // reads sstatus of pause switch
if (Statepause == LOW) { // pause / resume command handler
digitalWrite(pinMosfet, LOW); // Turns off pinMosfet and UV lights
digitalWrite(pinRed_led, LOW); // switch red LED off
digitalWrite(pinGreen_led, HIGH); // turns on green LED
display.showNumberDecEx(TSecs, 0b11100000, false, 4); // display remaining seconds with colon
display.showNumberDecEx(TMins, 0b11100000, false, 2); // display remaining minutes with colon
do { // handles resume (start switch) detection and
// handles stop switch detection during pause mode
Statestart = digitalRead(pinStart); // reads status of start switch and effects resume of timer
Statestop = digitalRead(pinStop); // handles stop command, when given during pause mode exits function and resets
// timer to original setting in Mins and Secs
if (Statestop == LOW) { // reads status of stop switch
digitalWrite(pinMosfet, LOW); // Turns off pinMosfet and UV lights
digitalWrite(pinRed_led, LOW); // switch red LED off
digitalWrite(pinGreen_led, HIGH); // turns on green LED
display.showNumberDecEx(Secs, 0b11100000, false, 4); // display set seconds with colon
display.showNumberDecEx(Mins, 0b11100000, false, 2); // display set minutes with colon
return; //exits from function to main program
}
delay (100); // wait 100mS
} while (Statestart == HIGH); // efffects pause
digitalWrite(pinMosfet, HIGH); // switch pinMosfet on for UV LEDs
digitalWrite(pinRed_led, HIGH); // switch red LED on
digitalWrite(pinGreen_led, LOW); // switch green LED off
}
//2nd second
delay (1000); // wait 1 second
TSecs = TSecs - 1; // calculate new remaining time
if (TSecs < 0) { // manipulation to range seconds to 0 - 59 per minute
TSecs = 59;
TMins = TMins - 1;
}
display.showNumberDecEx(TSecs, 0b11100000, false, 4); // display remaining seconds without colon
display.showNumberDecEx(TMins, 0b11100000, false, 2); // display remaining minutes without colon
Statestop = digitalRead(pinStop); // reads status of stop switch
if (Statestop == LOW) { // handles stop command, exits function and resets timer to original setting
// in Mins and Secs
digitalWrite(pinMosfet, LOW); // Turns off pinMosfet and UV lights
digitalWrite(pinRed_led, LOW); // switch red LED off
digitalWrite(pinGreen_led, HIGH); // turns on green LED
display.showNumberDecEx(Secs, 0b11100000, false, 4); // display set seconds with colon
display.showNumberDecEx(Mins, 0b11100000, false, 2); // display set minutes with colon
return; // exits from timer function to main proggram
}
Statepause = digitalRead(pinPause_timer); // reads status of pause switch
if (Statepause == LOW) { // pause / resume command handler
digitalWrite(pinMosfet, LOW); // Turns off pinMosfet and UV lights
digitalWrite(pinRed_led, LOW); // switch red LED off
digitalWrite(pinGreen_led, HIGH); // turns on green LED
display.showNumberDecEx(TSecs, 0b11100000, false, 4); // display remaining seconds with colon
display.showNumberDecEx(TMins, 0b11100000, false, 2); // display remaining minutes with colon
do { // handles resume (start switch) detection and
// handles stop switch detection during pause mode
Statestart = digitalRead(pinStart); // reads status of start switch and effects resume of timer
Statestop = digitalRead(pinStop); // handles stop command, when given during pause mode exits function and resets
// timer to original setting in Mins and Secs
if (Statestop == LOW) { // reads stsatus of stop switch
digitalWrite(pinMosfet, LOW); // Turns off pinMosfet and UV lights
digitalWrite(pinRed_led, LOW); // switch red LED off
digitalWrite(pinGreen_led, HIGH); // turns on green LED
display.showNumberDecEx(Secs, 0b11100000, false, 4); // display set seconds with colon
display.showNumberDecEx(Mins, 0b11100000, false, 2); // display set minutes with colon
return; // exits from timer function to main proggram
}
delay (100); // wait 100mS
} while (Statestart == HIGH); // efffects pause
digitalWrite(pinMosfet, HIGH); // switch pinMosfet on for UV LEDs
digitalWrite(pinRed_led, HIGH); // switch red LED on
digitalWrite(pinGreen_led, LOW); // switch green LED off
}
} while (((60 * TMins) + TSecs) > 0); //Stops countdown timer at 0 seconds
digitalWrite(pinMosfet, LOW); // Turns off pinMosfet and UV lights
digitalWrite(pinRed_led, LOW); // switch red LED off
digitalWrite(pinGreen_led, HIGH); // turns on green LED
display.showNumberDecEx(Secs, 0b11100000, false, 4); // display set seconds with colon
display.showNumberDecEx(Mins, 0b11100000, false, 2); // display set minutes with colon
for (int t = 1; t <= 5; t++) {
toneAC(2000); // Generate tone at 5kHz
delay(1000); // plays tone for 1 second
toneAC(0); // turns tone off
delay(1000); // delays 1 second
}
Statestart = HIGH;
} // countdown timer function ends - returns to main program
//////////////
void Set_time() // Function to set minutes and seconds that timer is to run for
{ // Uses rotary encoder attached to pins 2 and 3
// Local variables
int SSecs = Secs; // Initialises local variable and sets it equal to current Secs value
int SMins = Mins; // Initialises local variable and sets it equal to current Mins value
//int Sel = HIGH; // sets selct PB variable to high state
//int Last_Sel =!Sel; // sets Last PB press variable to opposite of current press
if (changed) {
changed = false;
Serial.print(" Counter: "); Serial.println(counter);
SSecs = SSecs + counter;
counter = 0;
//Need to add minutes if greater >59 or substract if less than 0
if (SSecs > 59){
SMins=SMins+1;
SSecs = 0;
}
if (SSecs < 0 ){
if (SMins > 0 ) {
SMins=SMins-1;
SSecs = 59;
} else {
SMins = 0;
SSecs = 0;
}
}
SSecs = constrain(SSecs, 0, 59);
SMins = constrain(SMins, 0, 59);
Serial.print("Time : ");Serial.print(SMins);
Serial.print(":");Serial.print(SSecs);
Secs = SSecs;
Mins = SMins;
display.showNumberDecEx(SSecs, 0b11100000, false, 4); // display set seconds with colon
}
/*
do {
Stateset = digitalRead(pinSet);
Statestop = digitalRead(pinStop);
} while (Stateset == HIGH);
*/
display.showNumberDecEx(Secs, 0b11100000, false, 4); // display set seconds with colon
display.showNumberDecEx(Mins, 0b11100000, false, 2); // display set minutes with colon
}