how to measure time duration of any input be high

Hello!
i am very new to arduino programing. i want to measure time that any input be high. it should keep measuring time even after input be high from low.

any one can help me out.

Thanks,

Save the value of millis() when the input becomes HIGH then save the value again when the input becomes LOW. Subtract one from the other to get the duration

If you want to know at any time how long the input has been HIGH then subtract the value of millis() at the start from the value of millis() now if the input is currently HIGH

NOTE : the above advice requires that the loop() function repeats freely so no delay()s or while loops should be used in the loop() function or functions called from it.

int input=D0;
int timer=0;
int inputstate;

void setup(){
pinMode(input,INPUT);
Serial.begin(9600);
}
void loop() {
inputstate=digitalRead(input);
if(inputstate==HIGH){
timer=millis();
Serial.print(timer);
}

see this code, it prints millis time value irrespective of input high or low.
what is wrong in this code.
please helpl

How is the input wired ?
Any pulldown resistor in place or is it floating at an unknown, possibly HIGH, voltage ?

Try printing inputstate. Is it ever LOW ?

Which Areduino board are you using ? On many, digital pin 0 is used by the Serial interface so should not be used for digital input unless you know what you are doing.

Better to use INPUT_PULLUP in setMode() pinMode() on another pin to hold the input HIGH and rearrange the circuit to take the input LOW when the button is pressed and change the program logic to detect LOW indicating that the button is pressed.

EDIT : corrected name of function

UKHeliBob:
setMode()

That's new ;).... pinMode()

meltDown:
That's new ;).... pinMode()

Whoops on my part.

It should, of course, be

pinMode(pinNumber, INPUT_PULLUP);

no success,
i want like that, if input pin be high i want to store duration how long it was high and when next time if it be again high again this time duration should be added in previous time.
simple time calculation when ever input be high and display on serial monitor.

any help please

Maybe pulseIn would be to your liking.

Asif138:
i want like that, if input pin be high i want to store duration how long it was high and when next time if it be again high again this time duration should be added in previous time.

So that's what this meant?.....

Asif138:
i want to measure time that any input be high. it should keep measuring time even after input be high from low.

I didn't understand what it meant to measure high but still keep measuring when it's gone low again :wink:

means i want to calculate how long input was high.

Asif138:
means i want to calculate how long input was high.

Well we got that part, but now it seems you want a running total?

yes

Asif138:
no success,
i want like that, if input pin be high i want to store duration how long it was high and when next time if it be again high again this time duration should be added in previous time.
simple time calculation when ever input be high and display on serial monitor.

any help please

You have been instructed on how to determine how long a pin was held high. You also in this post described what you want done. Is it that hard to take that little leap and realize that you need to variable at the top of your program to store the running total. Is it that hard to take the leap of logic to add the duration of the pin high to this variable.

Start small

Write a sketch that saves the start and end time that an input is HIGH as I suggested in post #1. When the input becomes LOW print the time period that it was HIGH

Once that works you can build on it

Or, if you want it I will post a solution that I just wrote that gives this output:

.... how long is a a button pressed ....
Created: 14:58:47, Aug  1 2019
Setup done
 
Newly pressed at 4638 ms, newly released at 15294 ms
   This press: 10656 ms, Total: 10656 ms
Newly pressed at 17170 ms, newly released at 17476 ms
   This press: 306 ms, Total: 10962 ms
Newly pressed at 19112 ms, newly released at 39395 ms
   This press: 20284 ms, Total: 31246 ms

yes code please

Asif138:
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.

//  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