# Recording time difference between input0 and input1

**URL:** <https://forum.arduino.cc/t/recording-time-difference-between-input0-and-input1/1367126>\
**Category:** Programming\
**Created:** [March 25, 2025, 9:58am UTC](https://forum.arduino.cc/t/recording-time-difference-between-input0-and-input1/1367126 "2025-03-25T09:58:26Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [March 25, 2025, 12:49pm UTC](https://forum.arduino.cc/t/recording-time-difference-between-input0-and-input1/1367126/3 "2025-03-25T12:49:40Z")

</div>

I edited your post as `<CODE>` is not the right code tag for proper formatting. (Thanks for trying at least).

Why do you calculate the voltage ? Everything seems linear so you would save time by working from the raw values.

So you want to measure if a value is above a certain threshold for a certain time.

The algorithm is pretty simple

Set a flag to false initially (in setup)

In the loop  
Calculate the value

If the value goes over the threshold and the flag was not set then set up the flag to true and mark the time (record millis value in a startTime variable)

If the value is below the threshold set the flag to false

If the flag is true check the difference between the current time and the recorded startTime and if it’s above your target duration take action

You’ll have to decide what to do after taking action - do you keep repeating or you await for the value to go back under the threshold before starting a new cycle

This is a simple state machine. Here is a small introduction to the topic: [Yet another Finite State Machine introduction](https://forum.arduino.cc/t/yet-another-finite-state-machine-introduction/1198997)

---

_[View the full topic](https://forum.arduino.cc/t/recording-time-difference-between-input0-and-input1/1367126)._
