unsigned long zeroTime, splitTime, newTime, oldTime = 0, fivek = 4999; // needed 1ms for Serial printing
bool startFlag = 0;
int buttonPin = 2;
int motorPin = 3;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(motorPin, OUTPUT);
}
void loop() {
if (!digitalRead(buttonPin)) { // the button is press (LOW)
zeroTime = millis(); // set zeroTime to the time of button press
delay(150);
oldTime = zeroTime; // use oldTime as a variable
startFlag = 1; // this indicates the button was pressed
Serial.print("Button pressed. \nSplit time of 0 seconds at ");
Serial.println(zeroTime);
}
if (startFlag) { // if the button was pressed
newTime = millis(); // start a new 5 seconds count up
if (newTime - oldTime > fivek) { // if the 5 seconds interval has passed
splitTime = (newTime - zeroTime)/1000; // time to do things
Serial.print("Split time of ");
Serial.print(splitTime); // show the split time, or turn on a motor
Serial.print(" seconds at ");
Serial.println(newTime);
oldTime = newTime; // start a new split time
digitalWrite(motorPin, !digitalRead(motorPin)); // change the motor state
}
}
}PRESS
MOTOR