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;
}