Hello,
I'm fairly new to Arduino and I need to write a program for a class that creates a stopwatch using three push buttons. The first pushbutton starts the timer, the second pushbutton pauses the timer, and the third pushbutton resets the timer to zero. If the timer is started and paused and then started again, the program needs to be able to resume the timer from where it was paused. The output is to the serial monitor and must be in the format of HH:MM:SS:MS (all the way to 1/100th of a second). I don't need to actually run this program on an Arduino board, I just need to write the program. I began working on the portion for the timer pausing, but I get lost at that point. I don't know how to have the program hold a value as the loop continues to run (when it's paused) and have time the timer resume from the paused value, since from what I understand millis() will continue to run. What I have so far is below:
const int buttonPin1 = 2; // define variables
const int buttonPin2 = 3;
const int buttonPin3 = 4;
int buttonState1 = LOW;
int previousButtonState1 = LOW;
int buttonState2 = LOW;
int previousButtonState2 = LOW;
int buttonState3 = LOW;
int previousButtonState3 = LOW;
int starttime = 0;
int currenttime = 0;
int pausedtime = 0;
int H = 0;
int Hr = 0;
int M = 0;
int Mr = 0;
int S = 0;
int Sr = 0;
int MS = 0;
void setup() //void setup() runs once to initialize variables
{
Serial.begin(9600);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
digitalWrite(buttonPin1, LOW);
digitalWrite(buttonPin2, LOW);
digitalWrite(buttonPin3, LOW);
}
void loop() //void loop() runs continuously
{
starttime = millis(); // start timer
buttonState1 = digitalRead(buttonPin1); // read button states
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
if (buttonState1 == LOW && previousButtonState1 == LOW || buttonState1 == HIGH && previousButtonState1 == LOW && buttonState3 == HIGH && previousButtonState3 == LOW)
// start button not pressed
{
H = 0;
M = 0;
S = 0;
MS = 0;
H = sprintf("%02d",H);
M = sprintf("%02d",M);
S = sprintf("%02d",S);
MS = sprintf("%02d",MS);
Serial.print(H);
Serial.print(":");
Serial.print(M);
Serial.print(":");
Serial.print(S);
Serial.print(".");
Serial.println(MS);
delay(1000);
}
else if(buttonState1 == HIGH && previousButtonState1 == LOW)
// timer started
{
currenttime = millis()-starttime;
H = round((currenttime / 3600000)-0.5);
Hr = currenttime % 3600000;
M = round((Hr / 60000)-0.5);
Mr = Hr % 60000;
S = round((Mr / 1000)-0.5);
Sr = Mr % 3600000;
MS = round((Sr / 10)-0.5);
H = sprintf("%02d",H);
M = sprintf("%02d",M);
S = sprintf("%02d",S);
MS = sprintf("%02d",MS);
Serial.print(H);
Serial.print(":");
Serial.print(M);
Serial.print(":");
Serial.print(S);
Serial.print(".");
Serial.println(MS);
delay(1000);
}
else if(buttonState1 == HIGH && previousButtonState1 == LOW && buttonState2 == HIGH && previousButtonState2 == LOW)
// timer paused
{
pausedtime = millis()-starttime;
}