Making Stopwatch with Pushbutton & millis()

Hello. I'm making a stopwatch with pushbutton & millis() function.
However, there is a problem.

-Rule I want
If I press a button, print the first time once.
If I release the button, print the calculated time(last time - first time) once.

But if I run following code, it always prints the values.
Could you help me with this please?

/* Blinking LED without using delay


  • turns on and off a light emitting diode(LED) connected to a digital
  • pin, without using the delay() function. this means that other code
  • can run at the same time without being interrupted by the LED code.
  • Created 14 February 2006
  • David A. Mellis
  • http://arduino.berlios.de
    */

int ledPin = 13; // LED connected to digital pin 13
int value = LOW; // previous value of the LED
long previousMillis = 0; // will store last time LED was updated
long interval = 1000; // interval at which to blink (milliseconds)

int Btn1Pin = 7;
int Temp = 0; //Like switch??
long StartTime = 0;
long EndTime = 0;
int Btn1Val = LOW;
int Test = 0;

void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
pinMode(Btn1Pin, INPUT);
Serial.begin(115200);
}

void loop()
{
// here is where you'd put code that needs to be running all the time.

// check to see if it's time to blink the LED; that is, is the difference
// between the current time and last time we blinked the LED bigger than
// the interval at which we want to blink the LED.
//if (millis() - previousMillis > interval) {
// previousMillis = millis(); // remember the last time we blinked the LED

// if the LED is off turn it on and vice-versa.
// if (value == LOW)
// value = HIGH;
// else
// value = LOW;

// digitalWrite(ledPin, value);

Btn1Val = digitalRead(Btn1Pin);

if (Btn1Val == LOW) {//IF BUTTON = PUSH
if (Temp == 0) {
Temp = 1;
StartTime = int(millis());
Serial.println(StartTime);
//digitalWrite(ledPin, HIGH);
goto NextTime;

}
}

if (Btn1Val == HIGH) {//IF BUTTON=RELEASED
if (Temp == 1) {
Temp = 0;
EndTime = int(millis());
Serial.println(EndTime - StartTime);
//digitalWrite(ledPin, LOW);
goto NextTime;
}
}

NextTime:
Test = 0;

}

Hmmm... I'm staring at this code, and I don't see why it would print continuously.
I do see some things you might want to change. The lines

StartTime = int(millis());
EndTime = int(millis());

should be:

StartTime = millis();
EndTime = millis();

because otherwise you are chopping off the high 2 bytes of the return value of millis().
To be completely correct, you should change the declarations of these variables to the following, because millis() returns unsigned long:

unsigned long StartTime = 0;
unsigned long EndTime = 0;

But none of this explains why you would be seeing continuous output, even when you are not pressing the button. Maybe you should try adding more debug prints to see where exactly the problem is. There are some simplifications to the logic you could make also. Here would be my idea of how to change the loop() function to see what is going on. Note that I'm using an if/else instead of two ifs with gotos. But more importantly, I have added extra prints of strings "PUSH:" and "RELEASE:" so you can tell which part of the code is running when you don't expect it to.

void loop()
{
Btn1Val = digitalRead(Btn1Pin);

if (Btn1Val == LOW) {//IF BUTTON = PUSH
if (Temp == 0) {
Temp = 1;
StartTime = millis();
Serial.print("PUSH: ");
Serial.println(StartTime);
//digitalWrite(ledPin, HIGH);

}
} else {
//IF BUTTON=RELEASED
if (Temp == 1) {
Temp = 0;
EndTime = millis();
Serial.print("RELEASE: ");
Serial.println(EndTime - StartTime);
//digitalWrite(ledPin, LOW);
}
}
}

Hope this helps!

  • Don

Wow thanks alot kitty, your code makes so much more sense to me