/*
Name: Exam1_Lab2_egg_timer_PROF_SOLUTION.ino
Created: 10/4/2022 5:21:54 PM
Author: nbliz
*/
/*2. Your task is to create an egg timer using the 4 - digit 7 - segment display
and three buttons that perform the operations below :
//NOTE - COULD HAVE USED CLOCK IN LAB3-3 AND COULD DOWN
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 BIG MISTAKE - SHOULD HAVE USED HW03 MM:SS TIMER AS STARTING POINT
MODIFY THE TIMER TO COUNT DOWN RATHER THAN COUNT UP
ADD CHECK_PB FUNCTION TO CHECK 3 PUSH BUTTONS AND SET THE MINUTES AND SECONDS THERE
NO NEED TO START 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; //Instantiate a seven segment controller object
// globals
byte PB_12 = A0;
byte PB_65 = A1;
byte PB_RESET = A2;
bool PRESSED = LOW;
int minutes = 0; //
int seconds = 0; //
const int TIME_TICK = 1000; // set for 1000, test at faster rate
// 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 };
void setup() {
const byte numDigits = 4;
const byte digitPins[] = { d1, d2, d3, d4 };
const byte segmentPins[] = { a, b, c, d, e, f, g, dp };
bool resistorsOnSegments = true; // 'true' means resistors are on segment pins
const byte hardwareConfig = COMMON_CATHODE; // See README.md for options
bool updateWithDelays = false; // Default 'false' is Recommended
bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
bool disableDecPoint = false; // Use 'true' if your decimal point doesn't exist or isn't connected
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(90);
sevseg.refreshDisplay(); // Must run repeatedly
Serial.begin(9600);
pinMode(PB_12, INPUT_PULLUP);
pinMode(PB_65, INPUT_PULLUP);
pinMode(PB_RESET, INPUT_PULLUP);
}
void loop() { // three function calls
Check_PB(); // added this function
displayTime();
waitOneSecondWhileRefreshingTheTime();
updateTime();
}
void displayTime() {
/* Purpose: combines minutes and seconds into one number
and display using setNumber()
*/
unsigned long timeToDisplay = seconds + minutes * 100; // must multiply minutes by 100 to get into proper decade
sevseg.setNumber(timeToDisplay, 2); // select segment 2 for decimal pt
}
void waitOneSecondWhileRefreshingTheTime() {
/* Purpose: continuously refresh the display using refreshDisplay() lib call while
waiting for one second (1000 msec)
*/
unsigned long futureTime = millis() + TIME_TICK; // timetick second in future
while (millis() < futureTime) {
sevseg.refreshDisplay(); // refresh the display repeatedly
}
}
// checks which pushbutton is pressed and sets minutes and seconds
void Check_PB() {
if (digitalRead(PB_12) == PRESSED) {
minutes = 0; //
seconds = 12; //
Serial.print("PB_12 PRESSED");
}
if (digitalRead(PB_65) == PRESSED) {
minutes = 1; //
seconds = 5; //
Serial.print("PB_65 PRESSED");
}
if (digitalRead(PB_RESET) == PRESSED) {
minutes = 0; //
seconds = 0; //
Serial.print("PB_RESET PRESSED");
}
// print the time for troubleshooting
Serial.print("Setting mm= ");
Serial.print(minutes);
Serial.print(": ss= ");
Serial.println(seconds);
}
void updateTime() {
/* Purpose: decrement seconds and decrements minutes if seconds = 60
*/
if ((minutes == 0) && (seconds == 0)) {
// do nothing
}
else {
//decrement seconds
seconds--;
if (seconds < 0) { // special case for seconds = 0
seconds = 59;
minutes--; // decrement minutes
}
}
// print the time for troubleshooting
Serial.print("counting down mm= ");
Serial.print(minutes);
Serial.print(": ss= ");
Serial.println(seconds);
}
// END OF PROGRAM