Devices & Components
Arduino Nano
8 Digits LED Seven Segment Display
DS1307 RTC module
Push Button
SD card module
Hardware & Tools
Soldering kit
Software & Tools
ChatGPT
Project description
Code
Master
cpp
Master code for stopwatch.
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(); // Initialize the I2C bus 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 // Uncomment the following line to adjust the RTC date and time 33 // rtc.adjust(DateTime(2024, 03, 11, 19, 49, 0)); 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 // Uncomment below for reset debug message 53 // Serial.println("Reset"); 54 // Wait for button release to avoid bouncing 55 delay(50); 56 while (digitalRead(resetPin) == LOW); // Active wait until the button is released 57 58 elapsedTime = currentMillis - previousMillis; 59 String formattedTime = formatTime(elapsedTime); // Converts elapsedTime to HH:MM:SS:cc format 60 DateTime now = rtc.now(); // Retrieve current date and time from the RTC module 61 String formattedDateTime = formatDateTime(now); // Convert date and time to a formatted string 62 String fullData = formattedTime + " " + formattedDateTime; // Concatenate the time string and the date/time string separated by a comma 63 writeToSD(fullData); 64 65 previousMillis = currentMillis; // Reset the timer 66 timingStarted = true; // Start timing 67 // Emit a sound and blink the LED twice 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 // Formatting the date in the format DD/MM/YYYY HH:MM:SS 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); // Tenth of an hour 104 lc.setDigit(0, 6, hours % 10, true); // Hour 105 lc.setDigit(0, 5, (minutes / 10) % 10, false);// Tenth of a minute 106 lc.setDigit(0, 4, minutes % 10, true); // Minute 107 lc.setDigit(0, 3, (seconds / 10) % 10, false);// Tenth of a second 108 lc.setDigit(0, 2, seconds % 10, true); // Second 109 lc.setDigit(0, 1, (milliseconds / 10) % 10, false);// Tenth of a millisecond 110 lc.setDigit(0, 0, milliseconds % 10, false); // Millisecond 111} 112 113void beepAndBlink() { 114 for (int i = 0; i < 2; ++i) { 115 analogWrite(buzzerPin, 1000); // Use a PWM value to produce a sound 116 digitalWrite(ledPin, HIGH); // Turn on the LED 117 delay(100); // Keep the sound and the light on for 100 ms 118 analogWrite(buzzerPin, 0); // Stop the sound 119 digitalWrite(ledPin, LOW); // Turn off the LED 120 delay(100); // Keep the state off for 100 ms 121 } 122}
Slave
cpp
Slave code for the recorder.
1#include <Wire.h> 2#include <SPI.h> 3#include <SD.h> 4 5int measurementCount = 0; // Global variable to track the number of measurements 6 7void setup() { 8 Wire.begin(0x08); // This Nano's address as an I2C slave 9 Wire.onReceive(receiveEvent); // Function called upon receiving I2C data 10 SPI.begin(); // Initialize the SPI bus 11 SD.begin(); // Initialize the SD card module 12 Serial.begin(9600); 13 Serial.println("SD Recorder ready."); 14} 15 16void loop() { 17 // Background processing 18} 19 20void receiveEvent(int howMany) { 21 String receivedData; 22 while (Wire.available()) { 23 char c = Wire.read(); 24 receivedData += c; 25 } 26 27 measurementCount++; // Increment the measurement counter 28 String message = "Measurement " + String(measurementCount) + " -> " + receivedData; 29 // Uncomment the line below for debug information 30 // Serial.println("Received via I2C: " + message); 31 writeToSD(message); 32} 33 34void writeToSD(const String& data) { 35 File myFile = SD.open("data.txt", FILE_WRITE); 36 if (myFile) { 37 Serial.println("Writing to SD: " + data); 38 myFile.println(data); 39 myFile.close(); 40 } else { 41 Serial.println("Error opening file on SD."); 42 } 43}
Downloadable files
Schematic
The wiring diagram.
Sans titre.png

Comments
Only logged in users can leave comments