1#include <LedControl.h>
2#include <Wire.h>
3#include <RTClib.h>
4
5LedControl lc = LedControl(10, 11, 12, 1);
6
7RTC_DS1307 rtc;
8
9int resetPin = A2;
10int buzzerPin = A0;
11int ledPin = 13;
12unsigned long previousMillis = 0;
13unsigned long currentMillis = 0;
14unsigned long elapsedTime = 0;
15bool timingStarted = false;
16bool buttonState = HIGH;
17unsigned long debounceDelay = 20;
18
19void setup() {
20 Serial.begin(9600);
21 rtc.begin();
22 Wire.begin();
23 lc.shutdown(0, false);
24 lc.setIntensity(0, 8);
25 lc.clearDisplay(0);
26 displayTime(0, 0, 0, 0);
27
28 pinMode(resetPin, INPUT_PULLUP);
29 pinMode(buzzerPin, OUTPUT);
30 pinMode(ledPin, OUTPUT);
31
32
33
34}
35
36void writeToSD(const String& data) {
37 Wire.beginTransmission(0x8);
38 Wire.write(data.c_str());
39 int result = Wire.endTransmission();
40
41 if (result == 0) {
42 Serial.println("Transfer to SD: " + data);
43 } else {
44 Serial.println("Error writing to SD");
45 }
46}
47
48void loop() {
49 currentMillis = millis();
50
51 if (digitalRead(resetPin) == LOW) {
52
53
54
55 delay(50);
56 while (digitalRead(resetPin) == LOW);
57
58 elapsedTime = currentMillis - previousMillis;
59 String formattedTime = formatTime(elapsedTime);
60 DateTime now = rtc.now();
61 String formattedDateTime = formatDateTime(now);
62 String fullData = formattedTime + " " + formattedDateTime;
63 writeToSD(fullData);
64
65 previousMillis = currentMillis;
66 timingStarted = true;
67
68 beepAndBlink();
69 }
70
71 if (timingStarted) {
72 elapsedTime = currentMillis - previousMillis;
73 int hours = elapsedTime / 3600000;
74 int minutes = (elapsedTime / 60000) % 60;
75 int seconds = (elapsedTime / 1000) % 60;
76 int milliseconds = elapsedTime % 1000 / 10;
77
78 displayTime(hours, minutes, seconds, milliseconds);
79 }
80}
81
82String formatDateTime(DateTime now) {
83 char dateTime[25];
84
85 sprintf(dateTime, "(%02d/%02d/%04d %02dh%02d)", now.day(), now.month(), now.year(), now.hour(), now.minute());
86 return String(dateTime);
87}
88
89String formatTime(unsigned long ms) {
90 unsigned long totalSeconds = ms / 1000;
91 unsigned long totalMinutes = totalSeconds / 60;
92 unsigned long hours = totalMinutes / 60;
93 unsigned long minutes = totalMinutes % 60;
94 unsigned long seconds = totalSeconds % 60;
95 unsigned long centiseconds = (ms % 1000) / 10;
96
97 char buffer[11];
98 sprintf(buffer, "[%02lu:%02lu:%02lu:%02lu]", hours, minutes, seconds, centiseconds);
99 return String(buffer);
100}
101
102void displayTime(int hours, int minutes, int seconds, int milliseconds) {
103 lc.setDigit(0, 7, (hours / 10) % 10, false);
104 lc.setDigit(0, 6, hours % 10, true);
105 lc.setDigit(0, 5, (minutes / 10) % 10, false);
106 lc.setDigit(0, 4, minutes % 10, true);
107 lc.setDigit(0, 3, (seconds / 10) % 10, false);
108 lc.setDigit(0, 2, seconds % 10, true);
109 lc.setDigit(0, 1, (milliseconds / 10) % 10, false);
110 lc.setDigit(0, 0, milliseconds % 10, false);
111}
112
113void beepAndBlink() {
114 for (int i = 0; i < 2; ++i) {
115 analogWrite(buzzerPin, 1000);
116 digitalWrite(ledPin, HIGH);
117 delay(100);
118 analogWrite(buzzerPin, 0);
119 digitalWrite(ledPin, LOW);
120 delay(100);
121 }
122}