Hi All
I am having to ask here because the original author of the project is not responding to troubleshooting questions.
I have a Stopwatch sketch that works almost perfectly except that the start/stop button restarts from 00:00:00 instead of carrying on from when it was stopped.
Here is the code I have:
// include the library code:
#include <LiquidCrystal.h>
#include <Bounce2.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 6, rw = 5, en = 4, d4 = 8, d5 = 9, d6 = 10, d7 = 11;
//LiquidCrystal(rs, rw, enable, d4, d5, d6, d7)
LiquidCrystal lcd(rs, rw, en, d4, d5, d6, d7);
const byte ledPin = 13;
const byte startButton = 7;
// Instantiate a Bounce object
Bounce startDebouncer1 = Bounce();
const byte resetButton = 3;
// Instantiate another Bounce object
Bounce resetDebouncer2 = Bounce();
void setup() {
pinMode(startButton, INPUT_PULLUP);
// After setting up the button, setup the Bounce instance :
startDebouncer1.attach(startButton);
startDebouncer1.interval(5); // interval in ms
pinMode(resetButton, INPUT_PULLUP);
// After setting up the button, setup the Bounce instance :
resetDebouncer2.attach(resetButton);
resetDebouncer2.interval(5); // interval in ms
pinMode(ledPin, OUTPUT);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.setCursor(0, 0);
// Print a message to the LCD.
lcd.print("*** StopWatch **");
lcd.setCursor(0, 1);
lcd.print("*** Project ***");
delay(1500);
lcd.setCursor(0, 0);
// Print a message to the LCD.
lcd.print("***Door Mick**");
lcd.setCursor(0, 1);
lcd.print("*Dankjewel Bart*");
// clear screen for the next loop
delay(1500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("S - Start/Stop");
lcd.setCursor(0, 1);
lcd.print("R - Reset");
}
bool startState = LOW;
bool resetState = LOW;
unsigned long startMillis;
unsigned long currentMillis;
unsigned long elapsedMillis;
void loop() {
// Update the Bounce instances :
startDebouncer1.update();
if ( startDebouncer1.fell() ) { // Call code if button transitions from HIGH to LOW
startState = !startState; // Toggle start button state
startMillis = millis();
}
if (startState)
{
currentMillis = millis();
elapsedMillis = (currentMillis - startMillis);
lcd.setCursor(0, 0);
lcd.print("SW (hh:mm:ss:ms)");
lcd.setCursor(0, 1);
lcd.print(" ");
unsigned long durMS = (elapsedMillis%1000); //Milliseconds
unsigned long durSS = (elapsedMillis/1000)%60; //Seconds
unsigned long durMM = (elapsedMillis/(60000))%60; //Minutes
unsigned long durHH = (elapsedMillis/(3600000)); //Hours
durHH = durHH % 24;
String durMilliSec = timeMillis(durHH, durMM, durSS,durMS);
lcd.setCursor(0, 1);
lcd.print(durMilliSec);
delay(150);
}
resetDebouncer2.update();
if (resetDebouncer2.fell())
{
resetState = HIGH;
}
if (resetState)
{
// clear screen for the next loop:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("S - Start/Stop");
lcd.setCursor(0, 1);
lcd.print("R - Reset");
delay(100);
resetState = LOW;
}
}
String timeMillis(unsigned long Hourtime,unsigned long Mintime,unsigned long Sectime,unsigned long MStime)
{
String dataTemp = "";
if (Hourtime < 10)
{
dataTemp = dataTemp + "0" + String(Hourtime)+ "h:";
}
else{
dataTemp = dataTemp + String(Hourtime)+ "h:";
}
if (Mintime < 10)
{
dataTemp = dataTemp + "0" + String(Mintime)+ "m:";
}
else{
dataTemp = dataTemp + String(Mintime)+ "m:";
}
if (Sectime < 10)
{
dataTemp = dataTemp + "0" + String(Sectime)+ "s:";
}
else{
dataTemp = dataTemp + String(Sectime)+ "s:";
}
dataTemp = dataTemp + String(MStime);
return dataTemp;
}
Being a bit of a long term newbie, this is just beyond me at the moment but I hope to learn something with this.