const int analogInPin_RST = A0; // MERAH - RESET
const int analogInPin_STRT = A1; // HIJAU - START
const int analogInPin_STOP = A2; // HIJAU - STOP
const int analogOutPin = 9; // MERAH - RESET
int ResetbuttonState = LOW; // variable for reading the pushbutton status
int Counter = 0;
unsigned long startTime = 0; // Variable to store the start time
unsigned long currentTime = 0; // Variable to store the current time
bool timerRunning = false; // Flag to indicate if the timer is running
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// pinMode(analogInPin_STRT, INPUT_PULLUP);
// pinMode(analogInPin_STOP, INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
// ResetbuttonState = digitalRead(analogInPin_STOP);
Counter = Counter + 1;
Serial.print(Counter);
// Serial.print(" - timerunning: ");
// Serial.print(timerRunning);
// Serial.print(" - analogInPinSTRT: ");
// Serial.print(digitalRead(analogInPin_STRT));
// Serial.print(" - analogInPinSTOP: ");
// Serial.println(digitalRead(analogInPin_STOP));
// if (ResetbuttonState == HIGH) {
// turn LED on:
// digitalWrite(analogOutPin, HIGH);
// } else {
// // turn LED off:
// digitalWrite(analogOutPin, LOW);
// }
delay(100);
if (digitalRead(analogInPin_STRT) == HIGH && !timerRunning) {
startTime = millis(); // Record the start time
timerRunning = true; // Set the timer running flag
digitalWrite(analogInPin_STOP, LOW);
digitalWrite(analogOutPin, HIGH); // Turn on the LED to indicate timer is running
}
if (timerRunning) {
currentTime = millis(); // Get the current time
unsigned long elapsedTime = currentTime - startTime; // Calculate the elapsed time
// Print the elapsed time in seconds to the serial monitor
Serial.print("Elapsed Time: ");
Serial.print(elapsedTime / 1000);
Serial.println(" seconds");
if (digitalRead(analogInPin_STOP) == HIGH && timerRunning) {
timerRunning = false; // Set the timer running flag
digitalWrite(analogInPin_STRT, LOW);
digitalWrite(analogOutPin, LOW); // Turn on the LED to indicate timer is running
}
}
}