// NBlizard 9/28/2022 Exam Lab pt 2 REWORKED
/*
Name: Exam1_Lab_pt_2_egg_timer.ino
Created: 9/26/2022 10:07:19 PM
Author: nblizard
*/
/*
2. Your task is to create an egg timer using the 4 - digit 7 - segment display
and three buttons that perform the operations below :
Operation:
When the program starts the display reads “0.00” and waits for the user to press a button.
Button1:
If the user pressed button1, the display reads “0.12”(zero minutes and 12 seconds).
Button2:
If the user pressed button2, the display reads “1.05”(one minute and 5 seconds).
In both cases, the timer then counts down in seconds constantly displaying the time without glitches.
When the time reaches “0.00” it stops and waits for the user to press another button.
Button3 is the reset button. It works like this :
If the timer is not running, the display continues to show “0.00” and waits for the user to press a button.
If the timer is running, the display immediately resets to “0.00” and waits for the user to press a button.
*/
// start egg timer algorithm from scratch
// then incorporate 4dig 7segment LED array and calls to show countdown time
// load and instantiate seveg library and object
#include "SevSeg.h"
SevSeg sevseg;
// globals
byte BUTTON_1 = A0;
byte BUTTON_2 = A1;
byte BUTTON_3 = A2;
bool PRESSED = LOW;
bool BUTTON_1_STATE = !PRESSED; // high is not pressede
bool BUTTON_2_STATE = !PRESSED;
bool BUTTON_3_STATE = !PRESSED;
const byte DELAY_BUTTON = 100;
int timeToDisplay = 0; // this is the initial target time to display, global to pass thru functs.
int countDown = 0; // this is the count down timer, displayed and refreshed
// setup for 4dig x 7 seg display
const byte a = 2;
const byte b = 3;
const byte c = 4;
const byte d = 5;
const byte e = 6;
const byte f = 7;
const byte g = 8;
const byte dp = 9;
const byte d4 = 10;
const byte d3 = 11;
const byte d2 = 12;
const byte d1 = 13;
const bool ON = HIGH;
const bool OFF = !ON;
const byte digitPins[] = { d1, d2, d3, d4 };
const byte segmentPins[] = { a, b, c, d, e, f, g, dp } ;
byte size_DIGITS = sizeof(digitPins);
byte size_SEGMENTS = sizeof(segmentPins);
void setup() {
// setup pushbuttons
pinMode(BUTTON_1, INPUT_PULLUP); // avoids need for pullup resistor, must connect to ground
pinMode(BUTTON_2, INPUT_PULLUP);
pinMode(BUTTON_3, INPUT_PULLUP);
// initialize 7 seg display
//setup 4dig x 7 seg display for sevseg.h
const byte numDigits = 4;
const bool resistorsOnSegments = true; // should be true
const bool hardwareConfig = COMMON_CATHODE; // "attrs": { "digits": "4", "common": "cathode" }
const bool updateWithDelays = false;
const bool leadingZeros = false;
const bool disableDecPoint = false;
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins,
resistorsOnSegments, updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(255);
// set pinmodes to drive 7seg display
for (int j = 2; j <= 13; j++) {
pinMode(j, OUTPUT) ;
}
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly
// initialize time to display
timeToDisplay = 0; // made global for refresh display
countDown = 0;
// initialize display
//clear display
//sevseg.blank(); // had to comment this out to allow display of 0.00 time
sevseg.setNumber(countDown, 2) ; // for 2 digits
sevseg.refreshDisplay();
delay(5);
//
// read state of button 1
BUTTON_1_STATE = digitalRead(BUTTON_1);
// debounce button
debounceWhileRefreshing (DELAY_BUTTON);
BUTTON_2_STATE = digitalRead(BUTTON_2);
// debounce button
debounceWhileRefreshing (DELAY_BUTTON);
BUTTON_3_STATE = digitalRead(BUTTON_3);
// debounce button
debounceWhileRefreshing (DELAY_BUTTON);
if (BUTTON_1_STATE == PRESSED) {
timeToDisplay = Button_1_SetTimer();
// reset state
BUTTON_1_STATE = !PRESSED;
// call countdowntimer
//Serial.println(timeToDisplay);
Count_Down_Timer(timeToDisplay);
}
if (BUTTON_2_STATE == PRESSED) {
timeToDisplay = Button_2_SetTimer();
// reset state
BUTTON_2_STATE = !PRESSED;
// call countdowntimer
//Serial.println(timeToDisplay);
Count_Down_Timer(timeToDisplay);
}
// display timeToDisplay
displayTimeToDisplay(timeToDisplay); // timeToDisplay is global
}
// function for Button_1
int Button_1_SetTimer () {
// set display to 12
int timeToDisplay = 12;
return timeToDisplay;
}
// function for Button_2
int Button_2_SetTimer () {
// set display to
int timeToDisplay = 105; // remember this is minutes:seconds, parse out seconds
return timeToDisplay;
}
// function to count down from initial timeToDisplay to zero
//
void Count_Down_Timer (int displayTime) {
countDown = displayTime;
for ( int countDown = displayTime; countDown >= 0; countDown--) {
// set the time delay
unsigned long futureTime = millis() + 1000; // adding 1 second to future time
// now do nothing for 1 second
while ( millis() < futureTime) {
// special case if 100 (1 minute)
if (countDown == 99) {
countDown = 59;
}
// refresh display
sevseg.setNumber(countDown, 2) ; // for 2 digits
sevseg.refreshDisplay();
//check button3 for reset
if (Check_Button_3()) { // if reset, set counDown to 0 and break
Serial.println("Reset");
countDown = 0;
sevseg.setNumber(countDown, 2) ; // for 2 digits
sevseg.refreshDisplay();
break;
}
}
//Serial.println(countDown);
}
}
// this function continously checks for reset, it must be called in countdowntimer
bool Check_Button_3 () {
// continue to read button 3 for reset
bool Reset_Flag = false;
BUTTON_3_STATE = digitalRead(BUTTON_3);
debounceWhileRefreshing (DELAY_BUTTON);
if (BUTTON_3_STATE == PRESSED) {
Reset_Flag = true;
}
return Reset_Flag;
}
void initializeDisplay( bool lightsOn ) {
// initialize display
// to light display
// set digit pins low
// set segment pins high
//
if (lightsOn) { // turn display on
for (int j = 0; j < size_DIGITS; j++) {
digitalWrite(digitPins[j], OFF);
}
for (int j = 0; j < size_SEGMENTS; j++) {
digitalWrite(segmentPins[j], ON);
}
sevseg.refreshDisplay(); // needed for each pass
delay(5);
}
else { // turn display off
// to turnoff display
// set digit pins high
// set segment pins low
for (int j = 0; j < size_DIGITS; j++) {
digitalWrite(digitPins[j], ON);
}
for (int j = 0; j < size_SEGMENTS; j++) {
digitalWrite(segmentPins[j], OFF);
}
sevseg.refreshDisplay(); // needed for each pass
delay(5);
}
} // end initializeDisplay
// display timeToDisplay (global)
void displayTimeToDisplay( int countDownTime) {
//timeToDisplay = 9999; // test
sevseg.setNumber(countDownTime, 2) ; // for 2 digits
sevseg.refreshDisplay();
} // end displayTimeToDisplay
// debounce button routine
// void debounceWhileRefreshing (int delayTime) {
// // unsigned long futureTime = millis() + delayTime;
// // while (millis() < futureTime) {
// // displayTimeToDisplay(countDown);
// // sevseg.refreshDisplay();
// // }
// } // end debounceWhileRefreshing
//debounce function (delay in while loop without actually calling delay function -which pauses arduino)
void debounceWhileRefreshing(int delayTime) {
unsigned long futureTime = millis() + delayTime; // this sets delay time to be in future
while (millis() < futureTime) {
sevseg.refreshDisplay();
}
}