/*#include <SevSeg.h>
SevSeg sevseg;
const int button1Pin = 2;
const int button2Pin = 3;
const int button3Pin = 5;
const int buzzerPin = 6;
int currentNumber = 0;
bool countingDown = false;
bool isPaused = false;
unsigned long lastButtonPressTime = 0;
unsigned long previousMillis = 0;
const unsigned long debounceDelay = 200;
const long interval = 1000;
void setup() {
byte numDigits = 2;
byte digitPins[] = {A3, A4};
byte segmentPins[] = {10, 11, 12, 13, A0, A1, A2};
sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins);
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
pinMode(button3Pin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
// Button 1: increment
if (digitalRead(button1Pin) == LOW) {
if (currentMillis - lastButtonPressTime > debounceDelay) {
currentNumber = min(99, currentNumber + 1);
lastButtonPressTime = currentMillis;
}
}
// Button 2: decrement
if (digitalRead(button2Pin) == LOW) {
if (currentMillis - lastButtonPressTime > debounceDelay) {
currentNumber = max(0, currentNumber - 1);
lastButtonPressTime = currentMillis;
}
}
// Button 3: start/stop countdown
if (digitalRead(button3Pin) == LOW) {
if (currentMillis - lastButtonPressTime > debounceDelay) {
if (!countingDown) {
countingDown = true;
isPaused = false;
} else {
isPaused = !isPaused;
}
lastButtonPressTime = currentMillis;
}
}
// Countdown logic with non-blocking delay
if (countingDown && !isPaused) {
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (currentNumber > 0) {
currentNumber--;
} else {
tone(buzzerPin, 655, 300);
countingDown = false;
}
}
}
sevseg.setNumber(currentNumber);
sevseg.refreshDisplay();
}*/
#include <SevSeg.h>
SevSeg sevseg;
const int button1Pin = 2;
const int button2Pin = 3;
const int button3Pin = 5;
const int buzzerPin = 6;
const int latchPin = 8;
const int clockPin = 7;
const int dataPin = 9;
const int buttonPin = 4;
int currentNumber = 0;
bool countingDown = false;
bool isPaused = false;
unsigned long lastButtonPressTime = 0;
unsigned long previousMillis = 0;
const unsigned long debounceDelay1 = 200;
const long interval = 1000;
int buttonState = HIGH;
int lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay2 = 50;
bool isLetterH = false;
const byte letterL = ~0b00001110;
const byte letterH = ~0b00110111;
void setup() {
byte numDigits = 2;
byte digitPins[] = {A3, A4};
byte segmentPins[] = {10, 11, 12, 13, A0, A1, A2};
sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins);
// Optionally force showing leading zeros if supported by the library.
// sevseg.setShowLeadingZeros(true);
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
pinMode(button3Pin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
displayPattern(letterL);
}
void loop() {
unsigned long currentMillis = millis();
if (digitalRead(button1Pin) == LOW) {
if (currentMillis - lastButtonPressTime > debounceDelay1) {
currentNumber = min(99, currentNumber + 1);
lastButtonPressTime = currentMillis;
}
}
if (digitalRead(button2Pin) == LOW) {
if (currentMillis - lastButtonPressTime > debounceDelay1) {
currentNumber = max(0, currentNumber - 1);
lastButtonPressTime = currentMillis;
}
}
if (digitalRead(button3Pin) == LOW) {
if (currentMillis - lastButtonPressTime > debounceDelay1) {
if (!countingDown) {
countingDown = true;
isPaused = false;
} else {
isPaused = !isPaused;
}
lastButtonPressTime = currentMillis;
}
}
if (countingDown && !isPaused) {
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (currentNumber > 0) {
currentNumber--;
} else {
//tone(buzzerPin, 660, 250);
countingDown = false;
}
}
}
// Format currentNumber as two digits (with a leading zero if needed)
char displayStr[3];
sprintf(displayStr, "%02d", currentNumber);
sevseg.setChars(displayStr);
sevseg.refreshDisplay();
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = currentMillis;
}
if ((currentMillis - lastDebounceTime) > debounceDelay2) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
isLetterH = !isLetterH;
displayPattern(isLetterH ? letterH : letterL);
}
}
}
lastButtonState = reading;
}
void displayPattern(byte pattern) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, pattern);
digitalWrite(latchPin, HIGH);
}