// Pin assignment pre 7-segmentový displej
int digitPins[] = {8, 9, 10, 11}; // Piny pripojené k segmentom pre 4-ciferný displej
int segmentPins[] = {2, 3, 4, 5, 6, 7}; // Segmenty a ich piny
// Tlačidlá
const int buttonStartStopPin = 12; // Pin pre tlačidlo Start/Stop
const int buttonResetPin = 13; // Pin pre tlačidlo Reset
bool stopwatchRunning = false; // Stav stopiek
unsigned long startTime = 0; // Čas začiatku stopiek
unsigned long elapsedTime = 0; // Pretrvávajúci čas
unsigned long lastButtonPress = 0; // Čas posledného stlačenia tlačidla
// Segmentový kód pre číslice 0-9
int numToSegments[] = {
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111 // 9
};
void setup() {
// Nastavíme piny pre segmenty a digitálne výstupy
for (int i = 0; i < 4; i++) {
pinMode(digitPins[i], OUTPUT); // Piny pre jednotlivé cifry
}
for (int i = 0; i < 7; i++) {
pinMode(segmentPins[i], OUTPUT); // Segmenty A - G
}
pinMode(buttonStartStopPin, INPUT_PULLUP); // Tlačidlo Start/Stop
pinMode(buttonResetPin, INPUT_PULLUP); // Tlačidlo Reset
// Inicializujeme displeje
for (int i = 0; i < 4; i++) {
digitalWrite(digitPins[i], LOW);
}
}
void loop() {
// Detekcia tlačidla Start/Stop
if (digitalRead(buttonStartStopPin) == LOW && millis() - lastButtonPress > 200) {
stopwatchRunning = !stopwatchRunning;
if (stopwatchRunning) {
startTime = millis() - elapsedTime;
}
lastButtonPress = millis();
}
// Detekcia tlačidla Reset
if (digitalRead(buttonResetPin) == LOW && millis() - lastButtonPress > 200) {
elapsedTime = 0;
if (stopwatchRunning) {
startTime = millis();
}
lastButtonPress = millis();
}
// Ak stopky bežia, počítame čas
if (stopwatchRunning) {
elapsedTime = millis() - startTime;
}
// Zobrazenie času na 4-cifernom displeji
unsigned long seconds = elapsedTime / 1000;
unsigned long minutes = seconds / 60;
seconds = seconds % 60;
// Rozdelenie času na minúty a sekundy
int tensMinutes = minutes / 10;
int onesMinutes = minutes % 10;
int tensSeconds = seconds / 10;
int onesSeconds = seconds % 10;
// Zobrazenie hodnôt na displeji
displayDigit(0, tensMinutes);
displayDigit(1, onesMinutes);
displayDigit(2, tensSeconds);
displayDigit(3, onesSeconds);
}
// Funkcia na zobrazenie jednej cifry
void displayDigit(int digit, int num) {
// Nastavenie správneho čísla na segmenty
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], bitRead(numToSegments[num], i));
}
// Rozsvietenie správnej cifry
digitalWrite(digitPins[digit], HIGH);
delay(5); // Krátke oneskorenie pre stabilitu zobrazenia
digitalWrite(digitPins[digit], LOW);
}