Reading time between two INPUTS

Hello ArduinoLovers,

I'am facing a problem in reading time between two INPUTS. The project is as follows: A sensor gives a input to the Arduino. Then after sometime the same sensor gives another input to the Arduino.

I WANT TO MEASURE THE TIME BETWEEN THESE TWO INPUTS GIVEN BY THE SENSOR.

Which function should I use : pulseIn() millis()

or any another

I have read all the posts on the Forum but I couldn't get a suitable answer . PLEASE HELP !!!!!!!!!!!!!

I have read all the posts on the Forum

I seriously doubt that!

Which function should I use : pulseIn() millis()

or any another

Maybe any of these, maybe interrupts, maybe hardware timers. You failed to provide the necessary information. Either provide links to the manual/datasheet of that sensor or provide the technical data we would find there yourself (time range of that signal, accuracy needed, signal voltage, measuring error acceptable, etc.).

Adivijaya: Hello ArduinoLovers,

I'am facing a problem in reading time between two INPUTS. The project is as follows: A sensor gives a input to the Arduino. Then after sometime the same sensor gives another input to the Arduino.

I WANT TO MEASURE THE TIME BETWEEN THESE TWO INPUTS GIVEN BY THE SENSOR.

Which function should I use : pulseIn() millis()

or any another

I have read all the posts on the Forum but I couldn't get a suitable answer . PLEASE HELP !!!!!!!!!!!!!

"A sensor gives a input to the Arduino" What kind of sensor input? Analog? Digital? Serial? "Then after sometime" How long? microseconds, milliseconds, seconds, minutes?

ToddL1962: "A sensor gives a input to the Arduino" What kind of sensor input? Analog? Digital? Serial? "Then after sometime" How long? microseconds, milliseconds, seconds, minutes?

a digital sensor gives the input after some milliseconds

Code below has a button wired from pin14 to ground. Run with monitor open.

// https://forum.arduino.cc/index.php?topic=671988
// time between two digital inputs

/*
  BASED ON State change detection (edge detection) changed for INPUT PULLUP
               (button wired from pin to ground)
  https://www.arduino.cc/en/Tutorial/StateChangeDetection
*/

// this constant won't change:
const int  button = 14;

// Variables will change:
bool buttonState;         // current state of the button
bool lastButtonState;     // previous state of the button
unsigned long firstEventAt;
unsigned long secondEventAt;
unsigned long timeBetweenEvents;
bool weAreTiming;

void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println("time between two digital inputs");
  pinMode(button, INPUT_PULLUP);

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

  //turn bulitin led off
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  Serial.println(" ");
  Serial.println("setup() done... press the button");
  Serial.println(" ");
}

void loop()
{
  // read the button:
  buttonState = digitalRead(button);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) // != means not equal, so it changed one way or the other
  {
    if (buttonState == LOW) //... and if it's now low, that's a press
    {
      Serial.print("New event");
      if (!weAreTiming)
      {
        weAreTiming = true;
        Serial.print(" start ");
        firstEventAt = millis();
        Serial.println(firstEventAt);
      }
      else
      {
        weAreTiming = false;
        Serial.print(" end   ");
        secondEventAt = millis();
        Serial.println(secondEventAt);
        timeBetweenEvents = secondEventAt - firstEventAt;
        Serial.print("  Time between events ");
        Serial.println(timeBetweenEvents);        
      }
    }// change to low
    // Delay a little bit to avoid bouncing
    delay(50);
  }//change
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;

} //loop

I would think that an interupt With the simple effect of

Switch_time = millis()

Would get about as close as possible

Adivijaya:
a digital sensor gives the input after some milliseconds

If you are getting data from the sensor with digitalRead() then immediately afterwards you should save the value of millis() to a variable and do the same for the second reading. The difference between the two time values is the interval between readings. Use micros() for greater accuracy if the interval is short.

If that does not solve the problem then post your program.

…R

It is very inappropriate to waste time discussing how to deal with this when the nature of the "sensor" and the purpose of the measurement remain a secret. :roll_eyes:

Robin2: If you are getting data from the sensor with digitalRead() then immediately afterwards you should save the value of millis() to a variable and do the same for the second reading. The difference between the two time values is the interval between readings. Use micros() for greater accuracy if the interval is short.

If that does not solve the problem then post your program.

...R

I got your point, but I am not able to figure out how to save value of millis() after the second reading. Can you please give me a hint?

Adivijaya: Can you please give me a hint?

Read #4 before you read #6?

Adivijaya: I got your point, but I am not able to figure out how to save value of millis() after the second reading. Can you please give me a hint?

Not unless you post the program that represents your best attempt so we can see exactly what you are having difficulty with.

...R

I tried the code :

// https://forum.arduino.cc/index.php?topic=671988
// time between two digital inputs

/*
  BASED ON State change detection (edge detection) changed for INPUT PULLUP
               (button wired from pin to ground)
  https://www.arduino.cc/en/Tutorial/StateChangeDetection
*/

// this constant won't change:
const int  button = 14;

// Variables will change:
bool buttonState;         // current state of the button
bool lastButtonState;     // previous state of the button
unsigned long firstEventAt;
unsigned long secondEventAt;
unsigned long timeBetweenEvents;
bool weAreTiming;

void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println("time between two digital inputs");
  pinMode(button, INPUT_PULLUP);

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

  //turn bulitin led off
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  Serial.println(" ");
  Serial.println("setup() done... press the button");
  Serial.println(" ");
}

void loop()
{
  // read the button:
  buttonState = digitalRead(button);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) // != means not equal, so it changed one way or the other
  {
    if (buttonState == LOW) //... and if it's now low, that's a press
    {
      Serial.print("New event");
      if (!weAreTiming)
      {
        weAreTiming = true;
        Serial.print(" start ");
        firstEventAt = millis();
        Serial.println(firstEventAt);
      }
      else
      {
        weAreTiming = false;
        Serial.print(" end   ");
        secondEventAt = millis();
        Serial.println(secondEventAt);
        timeBetweenEvents = secondEventAt - firstEventAt;
        Serial.print("  Time between events ");
        Serial.println(timeBetweenEvents);        
      }
    }// change to low
    // Delay a little bit to avoid bouncing
    delay(50);
  }//change
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;

} //loop

But it didn't work on my Arduino.

And the one that I wrote:

unsigned long duration;
int sound_pin=3;
void setup()
{
  Serial.begin(9600);
  pinMode(sound_pin,INPUT);
}
void loop()
{
  duration=pulseIn(sound_pin,HIGH,2500000);
  Serial.println(duration);
}

Also didn't work on my Arduino UNO.

I think that there must be some or the other hardware problem with my Arduino UNO. I can say that because I have tried some example sketches in the IDE but they didn't work on the Arduino as well. Arduino is giving OUTPUTS correctly but it is not processing the INPUTS in a correct way. Also the Serial communication is not proper. What should I do???

Adivijaya: I tried the code.... But it didn't work on my Arduino.

How is the button wired, and is your monitor set at 9600 (or change the code to suit the monitor).

the button is wired with one wire to gnd and another to pin 7

Adivijaya: the button is wired with one wire to gnd and another to pin 7

Code below has a button wired from pin14 to ground

const int  button = 14;

Adivijaya: And the one that I wrote:

Also didn't work on my Arduino UNO.

Let's get back to basics .... the facts ....

What is causing these inputs that you want to time?

And roughly what is the interval between them (in millisecs or microsecs)?

How often does a pair of inputs occur?

...R

Yeah I actually change const int button=14 to const int button=7

I can’t say why it’s not working for you. I copied back my code that you posted in #11 just to make sure I had the exact code you did, and with the button pin 14 to ground, I got this output:

time between two digital inputs
 
setup() done... press the button
 
New event start 1667
New event end   5711
  Time between events 4044
New event start 11188
New event end   20764
  Time between events 9576
New event start 26746
New event end   39655
  Time between events 12909
New event start 40790
New event end   41119
  Time between events 329

Robin2: Let's get back to basics .... the facts ....

What is causing these inputs that you want to time?

And roughly what is the interval between them (in millisecs or microsecs)?

How often does a pair of inputs occur?

...R

A simple button [switch] is causing the INPUTS .

time interval: milliseconds

a few milliseconds.

I can't say why it's not working for you.

I guess that there Is a hardware problem in my Arduino.

You said you tried the code and posted code that shows button pin 14.

So either move your button to pin 14 or post the code that shows your changes.