hi, I am new to Arduino.
I was trying to write a program to act as an electronic stopwatch, showing passed time on an LCD display. The green key starts and stops the stopwatch. The red key stops the stopwatch, and if it is running resets the time value to 0. And it doesn't work somehow. When I press the green button for the first time: the stopwatch starts, when I press the green button for the second time, it stops, but when I press the button for the third time it resets and starts from 0. What am I doing wrong? Are there too many "ifs"? Here is my code.
#include <LiquidCrystal_I2C.h>
#define GREEN_BUTTON 4
#define RED_BUTTON 2
LiquidCrystal_I2C lcd(0x27, 16, 2);
unsigned long startTime = 0;
unsigned long stopTime = 0;
unsigned long timePassed = 0;
bool isRunning = false;
int greenState;
int redState;
int lastGreenState = LOW;
int lastRedState = LOW;
unsigned long lastDebounceTimeGreen = 0;
unsigned long lastDebounceTimeRed = 0;
unsigned long debounceDelay = 100; // Adjust as needed
void setup() {
lcd.init();
lcd.backlight();
displayInfo();
pinMode(GREEN_BUTTON, INPUT_PULLUP);
pinMode(RED_BUTTON, INPUT_PULLUP);
}
void displayInfo() {
lcd.setCursor(0, 0);
lcd.print("Start/Stop->GREEN");
lcd.setCursor(0, 1);
lcd.print("Reset->RED");
lcd.setCursor(0, 0);
}
void displayTime(unsigned long time) {
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.print(time);
lcd.print("ms");
}
void loop() {
int readingGreen = digitalRead(GREEN_BUTTON);
if (readingGreen != lastGreenState) {
lastDebounceTimeGreen = millis();
}
if((millis()-lastDebounceTimeGreen) > debounceDelay){
if(readingGreen != greenState){
greenState = readingGreen;
if(greenState == LOW){
if(isRunning == false){
startTime = millis();
isRunning = true;
if(timePassed == 0){
lcd.clear();
}
}
else{
stopTime = millis();
displayTime(timePassed);
isRunning = false;
}
}
}
}
lastGreenState = readingGreen;
int readingRed = digitalRead(RED_BUTTON);
if (readingRed != lastRedState) {
lastDebounceTimeRed = millis();
}
if ((millis()- lastDebounceTimeRed) > debounceDelay){
if(readingRed != redState){
delay(1000);
redState = readingRed;
if(redState == LOW){
if(timePassed == 0){
displayInfo();
}
else{
displayTime(timePassed);
}
timePassed = 0;
isRunning = false;
}
}
}
lastRedState = readingRed;
if (isRunning) {
timePassed = millis() - startTime;
displayTime(timePassed);
}
}