Hi,
I am using the code below to display the current time on one line and a stopwatch on another line of an LCD screen.
When I press a button attacjed to D2 I want to pause the time (which it does) and when I press D2 again I want the time to carry on from where it left off (which it doesn't). Currently when I press D2 to carry on with the timer it resets the time to zero and starts counting.
What I would like to do is store the time in a variable when D2 is pressed and when D2 is pressed again it would take the stored variable time and add it to the zero count.
Is this possible and if so please could somebody help me with the code required to do this.
// include the library code:
#include <LiquidCrystal.h>
#include <Bounce2.h>
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
// 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 = 2;
// Initiate a Bounce object
Bounce startDebouncer1 = Bounce();
const byte resetButton = 3;
// Initiate another Bounce object
Bounce resetDebouncer2 = Bounce();
void setup() {
lcd.begin(16, 2);
Serial.begin(9600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
//rtc.adjust(DateTime(__DATE__, __TIME__)); //this syncs the time to the computer
/*
*
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
*/
// pinMode(LED_BUILTIN, OUTPUT);
pinMode(ledPin, OUTPUT);
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
// clear screen for the next loop:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press Red Button");
}
bool startState = LOW;
bool resetState = LOW;
unsigned long startMillis;
unsigned long currentMillis;
unsigned long elapsedMillis;
void loop() {
DateTime now = rtc.now();
char dateBuffer[12];
sprintf(dateBuffer,"%02u:%02u:%02u ",now.hour(),now.minute(),now.second());
//Serial.println(dateBuffer);
// 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)
{ lcd.clear();
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
currentMillis = millis();
elapsedMillis = (currentMillis - startMillis);
lcd.setCursor(0, 0);
lcd.print("Time ");
lcd.print(dateBuffer);
lcd.setCursor(0, 1);
lcd.print("SW (hh:mm:ss:ms)");
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)
{
digitalWrite(LED_BUILTIN, LOW); // turn the LED off (LOW is the voltage level)
// clear screen for the next loop:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press Red Button");
lcd.setCursor(0, 1);
lcd.print("Press A-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;
}
Thanks