Go Down

Topic: how to measure time duration of any input be high (Read 816 times) previous topic - next topic


meltDown

yes code please

Lest the "but you mustn't spoon feed the guy / gal"-police cr@p on me, please note I'm only posting this specifically in response to the OP's request.

OP, note that my button here is wired to ground and pinMode switches on the internal pullup.

The code is really just the state change detect example adjusted for a switch to ground, and with the time captured.

Code: [Select]

//  https://forum.arduino.cc/index.php?topic=629321
// running total of how long a button is pressed
/*
  State change detection (edge detection) changed for INPUT PULLUP
*/

// this constant won't change:
const int  buttonPin = 3;    // the pin that the pushbutton is attached to
//   the button must be wired from pin to ground, it's pinmode is input_pullup

// Variables will change:
bool buttonState;         // current state of the button
bool lastButtonState;     // previous state of the button

unsigned long buttonBecamePressedAt;
unsigned long buttonHasBeenPressedForTotal = 0;
unsigned long buttonHasBeenPressedForThisTime = 0;

void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println(".... how long is a button pressed ....");
  Serial.print("Created: ");
  Serial.print(__TIME__);
  Serial.print(", ");
  Serial.println(__DATE__);
  Serial.println(__FILE__);

  // initialize the button pin as a input with pullup so active low
  //    make sure the button is from pin to ground
  pinMode(buttonPin, INPUT_PULLUP);

  //initialize button states
  buttonState = digitalRead(buttonPin);
  lastButtonState = buttonState;

  Serial.println("Setup done");
  Serial.println(" ");
}

void loop()
{
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) // means it changed... but which way?
  {
    if (buttonState == LOW)  // changed to pressed
    {
      // if the current state is LOW then the button was pressed
      Serial.print("Newly pressed at ");
      Serial.print(millis());
      Serial.print(" ms");
      buttonBecamePressedAt = millis();

    }
    else  // changed to released
    {
      // if the current state is HIGH then the button was released
      Serial.print(", newly released at ");
      Serial.print(millis());
      Serial.println(" ms");

      buttonHasBeenPressedForThisTime = millis() - buttonBecamePressedAt;
      buttonHasBeenPressedForTotal = buttonHasBeenPressedForTotal + buttonHasBeenPressedForThisTime;
      Serial.print("   This press: ");
      Serial.print(buttonHasBeenPressedForThisTime);
      Serial.print(" ms");

      Serial.print(", Total: ");
      Serial.print(buttonHasBeenPressedForTotal);
      Serial.println(" ms");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;

} //loop



Go Up