#include <SevSeg.h>
SevSeg sevseg; // Instantiate a seven segment controller object
const int switch1Pin = A2; // Pin connected to switch 1 (red signal)
const int switch2Pin = A3; // Pin connected to switch 2 (green signal)
int firstCount = 0; // Duration for the first countdown
int secondCount = 0; // Duration for the second countdown
unsigned long switch1PressTime = 0;
unsigned long switch2PressTime = 0;
bool switch1WasOn = false;
bool switch2WasOn = false;
bool countdownFirstActive = false;
bool countdownSecondActive = false;
bool recountFirst = false;
bool recountSecond = false;
unsigned long lastSerialPrintTime = 0;
const unsigned long serialPrintInterval = 1000; // 1 second interval for serial prints
void setup() {
// Define the connections
byte numDigits = 6;
byte digitPins[] = {9, 10, 11, 12, A0, A1}; // Digit 1 to 6
byte segmentPins[] = {2, 3, 4, 5, 6, 7, 8}; // A, B, C, D, E, F, G
bool resistorsOnSegments = true; // 'false' means resistors are on digit pins
byte hardwareConfig = COMMON_ANODE; // Your display configuration
bool updateWithDelays = false; // Default 'false' is recommended
bool leadingZeros = false; // Use 'true' if you'd like to keep leading zeros
bool disableDecPoint = true; // Use 'true' if your decimal point doesn't exist or isn't connected
pinMode(switch1Pin, INPUT_PULLUP);
pinMode(switch2Pin, INPUT_PULLUP);
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(90); // Set brightness (0-100)
Serial.begin(9600);
}
void loop() {
// Read switch states
bool switch1On = digitalRead(switch1Pin) == LOW;
bool switch2On = digitalRead(switch2Pin) == LOW;
// Handle switch 1 (red signal)
if (switch1On) {
if (!switch1WasOn) {
// Switch was just turned on, start counting duration
switch1PressTime = millis();
switch1WasOn = true;
recountFirst = true; // Start recounting
} else if (recountFirst) {
// Print duration every second
if (millis() - lastSerialPrintTime >= serialPrintInterval) {
lastSerialPrintTime = millis();
unsigned long pressDuration = (millis() - switch1PressTime) / 1000;
Serial.print("Switch1 ON: Start recounting, now is ");
Serial.print(pressDuration);
Serial.println(" seconds");
}
}
} else if (switch1WasOn) {
// Switch was just turned off, save duration
unsigned long pressDuration = (millis() - switch1PressTime) / 1000; // Convert to seconds
if (recountFirst) {
firstCount = pressDuration; // Set new duration
Serial.print("Switch1 OFF: Recounted to ");
Serial.print(firstCount);
Serial.println(" seconds");
recountFirst = false; // Stop recounting
} else {
if (pressDuration < firstCount) {
firstCount = 0; // Reset duration if released faster than recorded duration
}
}
countdownFirstActive = false; // Stop countdown if switch is off
switch1WasOn = false;
}
// Handle switch 2 (green signal)
if (switch2On) {
if (!switch2WasOn) {
// Switch was just turned on, start counting duration
switch2PressTime = millis();
switch2WasOn = true;
recountSecond = true; // Start recounting
} else if (recountSecond) {
// Print duration every second
if (millis() - lastSerialPrintTime >= serialPrintInterval) {
lastSerialPrintTime = millis();
unsigned long pressDuration = (millis() - switch2PressTime) / 1000;
Serial.print("Switch2 ON: Start recounting, now is ");
Serial.print(pressDuration);
Serial.println(" seconds");
}
}
} else if (switch2WasOn) {
// Switch was just turned off, save duration
unsigned long pressDuration = (millis() - switch2PressTime) / 1000; // Convert to seconds
if (recountSecond) {
secondCount = pressDuration; // Set new duration
Serial.print("Switch2 OFF: Recounted to ");
Serial.print(secondCount);
Serial.println(" seconds");
recountSecond = false; // Stop recounting
} else {
if (pressDuration < secondCount) {
secondCount = 0; // Reset duration if released faster than recorded duration
}
}
countdownSecondActive = false; // Stop countdown if switch is off
switch2WasOn = false;
}
// Update displays
updateDisplay();
// Refresh display
sevseg.refreshDisplay();
}
void updateDisplay() {
char displayBuffer[7] = " "; // Initialize buffer with spaces
// Handle countdown for switch 1 (red signal)
if (switch1WasOn) {
if (countdownFirstActive) {
int countdown = firstCount - (millis() - switch1PressTime) / 1000; // Calculate remaining time
if (countdown <= 0) {
countdown = 0;
countdownFirstActive = false;
displayBuffer[0] = ' ';
displayBuffer[1] = ' ';
displayBuffer[2] = 'x'; // 'x' in the third position for firstCount
} else {
displayCountdown(countdown, 0, 2); // Display countdown on digits 1 to 3
}
} else {
displayBuffer[0] = ' ';
displayBuffer[1] = ' ';
displayBuffer[2] = 'x'; // 'x' in the third position for firstCount
}
}
// Handle countdown for switch 2 (green signal)
if (switch2WasOn) {
if (countdownSecondActive) {
int countdown = secondCount - (millis() - switch2PressTime) / 1000; // Calculate remaining time
if (countdown <= 0) {
countdown = 0;
countdownSecondActive = false;
displayBuffer[3] = ' ';
displayBuffer[4] = ' ';
displayBuffer[5] = 'x'; // 'x' in the sixth position for secondCount
} else {
displayCountdown(countdown, 3, 5); // Display countdown on digits 4 to 6
}
} else {
displayBuffer[3] = ' ';
displayBuffer[4] = ' ';
displayBuffer[5] = 'x'; // 'x' in the sixth position for secondCount
}
}
// Clear display when neither switch is on
if (!switch1WasOn && !switch2WasOn) {
sevseg.setChars(" "); // Clear display
} else {
sevseg.setChars(displayBuffer);
}
}
void displayCountdown(int count, int startDigit, int endDigit) {
char buffer[7] = " "; // Initialize buffer with spaces
snprintf(buffer + startDigit, endDigit - startDigit + 1, "%3d", count); // Format count into the buffer
sevseg.setChars(buffer);
}