Hi Champs
i was working on a project for a stopwatch that shows millis and have successfully adjusted it with the help of the awesome guys here
currently i am trying to add two press buttons for it one for start to be pressed once to enable counting ,then to be ressed once to stop counting ,also a reset button to reset the count back to zeros once it is pressed
here is the code that i have made so far
#include "RTClib.h"
//Setup the Real Time Clock, DS3231
RTC_DS3231 rtc;
// Arduino pin used to monitor the SQW 1Hz output from the DS3231.
// Pin 2 is an "interrupt-capable" pin on the Arduino Uno. Note
// that different Arduino boards have specific pins which are
// inerrupt capable. Refer to:
// www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
// for a complete list.
const byte SQWinput = 2; // Must be External Interrupt
volatile uint32_t MillisecondsAtStartOfSecond = 0;
unsigned long secondss=0 ;
unsigned long minutess=0 ;
unsigned long hourss =0;
boolean passed_second=true;
boolean start=true;
const char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
// The SQW pin, when set to 1 Hz mode, has a falling edge at the beginning of every second.
void SQWFallingISR()
{
MillisecondsAtStartOfSecond = millis();
}
void setup ()
{
Serial.begin(115200); // Set Serial Monitor to 115200
delay(200);
pinMode(SQWinput, INPUT_PULLUP);
if (! rtc.begin())
{
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (rtc.lostPower())
{
Serial.println("RTC lost power, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// 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(2022, 4, 03, 21, 14, 0));
}
// Configure SQW pin on the DS3231 to output a 1Hz squarewave
rtc.writeSqwPinMode(DS3231_SquareWave1Hz);
// Get MillisecondsAtStartOfSecond on the falling edge of SQW.
attachInterrupt(digitalPinToInterrupt(SQWinput), SQWFallingISR, FALLING);
}
void loop ()
{
DateTime now = rtc.now();
unsigned long ms = millis();
noInterrupts();
// How long since the second started?
ms -= MillisecondsAtStartOfSecond;
interrupts();
displayTime (now, ms);
Serial.println("stopwatch ");
Serial.print(ms);
Serial.print(':');
Serial.print(secondss);
Serial.print(':');
Serial.print(minutess);
Serial.print(':');
Serial.println(hourss);
Serial.println("Clock ");
}
void displayTime(DateTime &now, unsigned long ms)
{
ms = ms % 1000; // Ignore full seconds
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
if (now.minute() < 10)
Serial.print('0');
Serial.print(now.minute(), DEC);
Serial.print(':');
if (now.second() < 10)
Serial.print('0');
Serial.print(now.second(), DEC);
// Serial.println();
Serial.print(':');
if (ms < 100)
Serial.print('0');
if (ms < 10)
Serial.print('0');
Serial.print(ms, DEC);
Serial.println();
if(ms>998) {
secondss++;
}
if(secondss>59) {
secondss=0;
minutess++;
}
if(minutess>59) {
minutess=0;
hourss++;}
}