As distinct from say digitalWrite(5) which does have a parameter, namely the pin number
Not the best example because digitalWrite takes two parameters but that's the idea ![]()
Note I made a correction in the code above. (baud needs to be 115200 and forgot to check if button was not pressed when initializing the startTime)
if you want to test if the button is pressed for too long you can do that as well, here I check if you keep it pressed for more than 5 secs and if so the code will bark at you
(you'll get to see all the bouncing possibly of the button)
const byte buttonPin = 2;
unsigned long startTime, stopTime, totalTime; // are initialized to 0 by compiler
boolean buttonPressed = false;
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
if ((buttonPressed == false) && (digitalRead(buttonPin) == LOW)) {
// button is pressed
buttonPressed = true;
startTime = millis();
Serial.print("start recording at ");
Serial.println(startTime);
} else { // button is not pressed, checking if this is a transition
if (buttonPressed) { // button was pressed
// button is released
buttonPressed = false;
stopTime = millis();
totalTime += stopTime - startTime;
Serial.print("stoped recording at ");
Serial.print(stopTime);
Serial.print(", duration was ");
Serial.println(stopTime - startTime);
Serial.print("Total cumulated pressed time is ");
Serial.print(totalTime);
}
}
if (buttonPressed) {
if (millis() - startTime >= 5000ul) { // print this repetitively if more than 5 sec
Serial.print ("Button is being pressed for ");
Serial.print (millis() - startTime);
Serial.println("ms, that's a long time...");
}
}
}