#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 columns, 2 rows
const int buttonPin = 2;
const int MAX_TIMES = 10; // Maximum number of times to store
unsigned long times[MAX_TIMES]; // Array to store elapsed times
int numTimes = 0; // Number of times stored
int buttonState = 0;
unsigned long startTime = 0;
unsigned long elapsedTime = 0;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.clear();
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
printTimes();
} else if (buttonState == HIGH) {
if (numTimes < MAX_TIMES) {
if (startTime == 0) {
startTime = millis();
Serial.println("Stopwatch started.");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Stopwatch started.");
} else {
unsigned long currentTime = millis();
elapsedTime = currentTime - startTime;
times[numTimes++] = elapsedTime;
Serial.print("Time ");
Serial.print(numTimes);
Serial.print(": ");
Serial.print(elapsedTime / 1000.0); // Convert milliseconds to seconds
Serial.println(" seconds");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time ");
lcd.print(numTimes);
lcd.print(": ");
lcd.print(elapsedTime / 1000.0); // Convert milliseconds to seconds
lcd.print(" sec");
startTime = 0;
}
} else {
Serial.println("Maximum number of times reached.");
}
}
}
void printTimes() {
Serial.println("All times:");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("All times:");
for (int i = 0; i < numTimes; i++) {
Serial.print("Time ");
Serial.print(i + 1);
Serial.print(": ");
Serial.print(times[i] / 1000.0); // Convert milliseconds to seconds
Serial.println(" seconds");
lcd.setCursor(0, i + 1);
lcd.print("Time ");
lcd.print(i + 1);
lcd.print(": ");
lcd.print(times[i] / 1000.0); // Convert milliseconds to seconds
lcd.print(" sec");
}
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
r1:1
r1:2