/******************************************************************************
Filename: StopWatch.ino
Developer(s): Nelson Guevara- Popayán Colombia
Corporación Universitaria Autónoma del Cauca
Language: C
Abstract: This file provides the code for a StopWatch with 3 buttons: Start, Stop and Reset.
************************************************************************************/
/***********************************************************************************
USER INCLUDES
***********************************************************************************/
#include <LiquidCrystal_I2C.h>
/***********************************************************************************
CONSTANT DEFINITIONS
***********************************************************************************/
#define START_BUTTON 26
#define STOP_BUTTON 27
#define RESET_BUTTON 12
/***********************************************************************************
GLOBAL DECLARATION
***********************************************************************************/
unsigned long startTime = 0;
unsigned long elapsedTime = 0;
unsigned long pausedTime = 0;
unsigned long centiseconds = 0;
unsigned long totalSeconds = 0;
unsigned long milliseconds = 0;
unsigned long seconds = 0;
unsigned long minutes = 0;
bool isRunning = false;
bool Stop_flag = false;
/* LCD */
/* Create an LCD object */
LiquidCrystal_I2C lcd(0x27, 16, 2);
/* TIMER */
/* Create an TIMER object */
hw_timer_t *timer = NULL;
/***********************************************************************************
GLOBAL FUNCTION DECLARATION
***********************************************************************************/
void IRAM_ATTR Start_Button_ISR();
void IRAM_ATTR Stop_Button_ISR();
void IRAM_ATTR Reset_Button_ISR();
void IRAM_ATTR Timer_ISR();
void startStopwatch();
void stopStopwatch();
void resetStopwatch();
/*******************************************************************************
Function that sets up external interrupt
*******************************************************************************/
void IRAM_ATTR Start_Button_ISR()
{
if(!isRunning)
{
timerAlarmEnable(timer);
startStopwatch();
}
}
/**********************************************************************************************
Function that sets up external interrupt
**********************************************************************************************/
void IRAM_ATTR Stop_Button_ISR()
{
if(isRunning)
{
stopStopwatch();
}
}
/**********************************************************************************************
Function that sets up external interrupt
**********************************************************************************************/
void IRAM_ATTR Reset_Button_ISR()
{
resetStopwatch();
}
/**********************************************************************************************
Function that sets up internal interrupt
**********************************************************************************************/
void IRAM_ATTR Timer_ISR()
{
if(!Stop_flag)
{
isRunning = true;
}
}
/*******************************************************************************
startStopwatch function
*******************************************************************************/
void startStopwatch()
{
startTime = millis();
}
/*******************************************************************************
stopStopwatch function
*******************************************************************************/
void stopStopwatch()
{
pausedTime += millis() - startTime;
isRunning = false;
Stop_flag = true;
}
/*******************************************************************************
resetStopwatch function
*******************************************************************************/
void resetStopwatch()
{
startTime = 0;
pausedTime = 0;
elapsedTime = 0;
centiseconds = 0;
totalSeconds = 0;
milliseconds = 0;
seconds = 0;
minutes = 0;
isRunning = true;
Stop_flag = true;
startStopwatch();
}
/*********************************************************************************
Function in arduino structure to configure initial parameters of
microcontroller and peripherals.
*********************************************************************************/
void setup() {
Serial.begin(115200);
pinMode(START_BUTTON, INPUT_PULLUP);
pinMode(STOP_BUTTON, INPUT_PULLUP);
pinMode(RESET_BUTTON, INPUT_PULLUP);
attachInterrupt(START_BUTTON, Start_Button_ISR, RISING);
attachInterrupt(STOP_BUTTON, Stop_Button_ISR, RISING);
attachInterrupt(RESET_BUTTON, Reset_Button_ISR, RISING);
lcd.init();
lcd.backlight();
lcd.setCursor(3, 0);
lcd.print("STOPWATCH");
delay(2e3);
/* Timer configuration */
timer = timerBegin(0, 80, true);
timerAttachInterrupt(timer, &Timer_ISR, true);
timerAlarmWrite(timer, 1e3, true);
}
/*******************************************************************************
Main program loop
*******************************************************************************/
void loop() {
while (isRunning)
{
elapsedTime = millis() - startTime + pausedTime;
centiseconds = (elapsedTime / 10) % 100;
totalSeconds = elapsedTime / 1000;
milliseconds = elapsedTime % 1000;
seconds = totalSeconds % 60;
minutes = totalSeconds / 60;
lcd.setCursor(3, 1);
lcd.print(String(minutes) + ":" + String(seconds) + ":" + String(centiseconds)+ ":" + String(milliseconds));
}
isRunning = false;
}