I am running this code
// general constants
#define startButton 8
#define stopButton 9
#define DEBUG true
// general global variables
unsigned long start = 0.0;
unsigned long finished = 0.0;
unsigned long elapsed = 0.0;
// forward declarations
// output calculation results
void displayResult();
void setup()
{
// start Serial
Serial.begin(9600);
// tell 'em who we are, man
Serial.println("Stopwatch.ino");
Serial.println();
// initialize buttons
pinMode(startButton, INPUT);
pinMode(stopButton, INPUT);
digitalWrite(startButton, LOW);
digitalWrite(stopButton, LOW);
} // end setup()
void loop()
{
// give instructions
Serial.println();
Serial.println("Press left button for Start/Reset");
Serial.println("Press right button for elapsed time");
Serial.println();
if (digitalRead(startButton) == HIGH)
{
if (DEBUG) Serial.println("startButton was HIGH");
start = millis();
finished = 0.0;
delay(200); // debounce
Serial.println("Started ...");
if (DEBUG) digitalWrite(startButton, LOW);
}
if (digitalRead(stopButton) == HIGH);
{
if (DEBUG)Serial.println("stopButton was HIGH");
finished = millis();
delay(200); // debounce
displayResult();
if (DEBUG) digitalWrite(stopButton, LOW);
}
if (DEBUG) delay(2000);
} // end loop()
/*==========================================*/
// output calculation results
void displayResult()
{
// local variables
float h, m, s, ms;
unsigned long over;
// get elapsed
finished = millis();
elapsed = finished - start;
// calculate time elements
h = int(elapsed / 3600000);
over = elapsed % 3600000;
m = int(over / 60000);
over = over % 60000;
s = int(over / 1000);
ms = over % 1000;
// display time elements
Serial.print("Raw elapsed time: ");
Serial.print(elapsed);
Serial.println(": ms");
Serial.print("Elapsed time: ");
Serial.print(h, 0);
Serial.print("h ");
Serial.print(m, 0);
Serial.print("m ");
Serial.print(s, 0);
Serial.print(" s ");
Serial.print(ms, 0);
Serial.println("ms");
Serial.println();
Serial.println();
} // end displayResult()
on this layout
My problem is that the stopButton is permanently high (even when the power lead is disconnected from the stopButton)
Press left button for Start/Reset
Press right button for elapsed time
startButton was HIGH
Started ...
stopButton was HIGH
Raw elapsed time: 401: ms
Elapsed time: 0h 0m 0 s 401ms
Press left button for Start/Reset
Press right button for elapsed time
stopButton was HIGH
Raw elapsed time: 2635: ms
Elapsed time: 0h 0m 2 s 635ms
Press left button for Start/Reset
Press right button for elapsed time
stopButton was HIGH
Raw elapsed time: 4870: ms
Elapsed time: 0h 0m 4 s 870ms
Press left button for Start/Reset
Press right button for elapsed time
stopButton was HIGH
Raw elapsed time: 7104: ms
Elapsed time: 0h 0m 7 s 104ms
When I put a DMM across it the stopSwitch is HOT when button pressed and COLD when not. Video confirmation at Youtube.
The start button behaves itself.
How do I get the stopButton to go LOW when the button is not pressed?
Thanks