Measure on time

Funny :rofl:

Show us how you think it would be done then we can give comments.

How accurate does this need to be ?

so joke aside, you go in a loop

highState = 0
lowState = 0

note start time t0
read start pin state

loop until pin changes state
note end time t1
add (t1 - t0) to a variable based on state 
copy t1 into t0
invert start pin state
rinse and repeat
//total runtime     both key pressed+key not pressed

if(millis() >= (Runtime) )
{
Runtime = Runtime + 1000;
second = second +1;
if (second == 60)
{
second = 0;
minute = minute +1;
}

  }           

Time shows on lcd display well ,as second and minute
Its work well for total runtime
But i want ro measure total key pressed time


//
key= digitalRead(13);

if (key==1). //key pressed
{
if(millis()>=runtime1)
{
runtime1=Runtime- runtime2+1000;
sec=sec+1;
if (sec == 60)
{
sec = 0;
min = min +1;
}
}
}
if (key==0)//key not pressed
{
runtime2=millis();
}

This lower part of code not work

When i show both time on lcd i.e.total Runtime and key pressed time ,key pressed time goes faster than total Runtime

Take a look at the state change example in the IDE.
When the pin goes from LOW to HIGH, take the value of millis (or micros).
When the pin goes from HIGH to LOW, take the time again and calculate the HIGH time.
Sum this.

Hi @vimal1982 , do you also have the ID which is @lingesh97 ?

@lingesh97 shared a piece of code almost identical to your code above, so I was curious if it is the same person or you are associated in some way.

No i just entered this forum

Ok.

If you look at this topic, I'm sure you will see a few similarities with your code and your question.

Hello, you might have received an error message from the forum when you posted code without the code tags. Did you ? if so, why did you ignore it?

Do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (add code tags).

type or paste code here

No cigar :slight_smile:

please edit your post, select the code part and press the </> icon in the tool bar to mark it as code.

> Blockquote

type or paste code

One press only ?

Or

If you press the switch 12 times then you want to add the times of the 12 press durations and display that total ?

could you please delete your two ugly posts and edit post #7 (click the little pencil) and add the code tags there. Indenting the code would get you extra credits :slight_smile:

//*********************************************************************************
// Version   YY/MM/DD   Description
// =======   ========   ===========
// 1.00      21 11 28   Working sketch
//
//

//Prints the switch closed times to the serial monitor.

//*********************************************************************************
#define OPEN                          HIGH
#define CLOSED                        LOW

//*********************************************************************************
const byte heartbeatLED             = 13;

const byte resetSwitch              = 3;
const byte mySwitch                 = 2;

byte currentState;
byte resetLastState                 = OPEN;
byte mySwitchLastState              = OPEN;


//timing stuff
unsigned long heartbeatMillis;
unsigned long switchMillis;
unsigned long totalTime;
unsigned long timeClosed;

//*********************************************************************************
void setup()
{
  Serial.begin(9600);

  pinMode(heartbeatLED, OUTPUT);

  pinMode(resetSwitch, INPUT_PULLUP);
  pinMode(mySwitch, INPUT_PULLUP);

} //END of setup()


//*********************************************************************************
void loop()
{
  //***************************************
  //time to toggle the heartbeat LED ?
  if (millis() - heartbeatMillis >= 500)
  {
    //restart the TIMER
    heartbeatMillis = millis();

    //toggle LED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));

  }

  //******************************************
  //is it time to check the switches ?
  if (millis() - switchMillis >= 20)
  {
    //restart the TIMER
    switchMillis = millis();

    checkSwitches();

  }


  //******************************************
  //other none blocking code goes here
  //******************************************

} //END of loop()


//*********************************************************************************
void checkSwitches()
{
  //***********************************************
  //                                                 m y S w i t c h
  //check the switch for a change in state
  currentState = digitalRead(mySwitch);

  //******************************
  //was there a change ?
  if (mySwitchLastState != currentState)
  {
    //update to the new state
    mySwitchLastState = currentState;

    //******************
    //was the switch closed ?
    if (currentState == CLOSED)
    {
      timeClosed = millis();
    }

    //******************
    //the switch was opened
    else
    {
      totalTime = totalTime + millis() - timeClosed;
      
      //print switch closed times
      Serial.print("The switch was closed for ");
      Serial.print(millis() - timeClosed);
      Serial.println(" milliseconds.");  
     
      Serial.print("Accumulated switch closed time in ms = \t");
      Serial.println(totalTime);
      Serial.println();
    }

  } //END of mySwitch code

  //***********************************************
  //                                                 r e s e t S w i t c h
  //check the switch for a change in state
  currentState = digitalRead(resetSwitch);

  //******************************
  //was there a change ?
  if (resetLastState != currentState)
  {
    //update to the new state
    resetLastState = currentState;

    //******************
    //was the switch closed ?
    if (currentState == CLOSED)
    {
      totalTime = 0;
      
      //print the total time the switch has been closed
      Serial.print("Accumulated switch closed time in ms = \t");
      Serial.println(totalTime);
      Serial.println();
    }

  } //END of resetSwitch code

  //***********************************************
  //other switch code goes here   

} //END of checkSwitches()

//*********************************************************************************

@LarryD What is the purpose of a "heartbeat LED"?

The same as any heartbeat - to show that there is still activity

I notice that, in your code, totalTime does not advance while the switch is closed. It only advances once the switch becomes open.

To have the totalTime variable advance in real time, you could have the heartbeat code do the timing. Something like:

  if (millis() - heartbeatMillis >= 20)
  {
    //restart the TIMER
    heartbeatMillis += 20;

    //toggle LED
    digitalWrite(heartbeatLED, ((heartbeatMillis / 500) & 1));

    if (digitalRead(mySwitch)==CLOSED) {
      totalTime += 20;
      if ((totalTime % 100) < 20)
      {
        Serial.print((totalTime / 1000.0), 1);
        Serial.println(" s");
      }
    }
  }

Need to get some input from the OP on what their requirement is.