[SOLVED] Elapsed Time

Thread Starter

MTech1

Joined Feb 15, 2023
181
I am trying to understand the concepts of 'Elapsed Time,' 'Current Time,' and 'Previous Time' within the context of a 1ms timer used to generate alerts at specific time intervals, such as 15ms and 22ms.

Let's assume we have three variables - 'current_time,' 'previous_time,' and 'elapsed_time' - that will help me to know these concepts in this timing scenario. The 'current_time' variable increments each time a timer interrupt occurs. Specifically, when 'current_time' reaches a value of 15, alerts should be generated to indicate that 15ms have passed.

When 'current_time' reaches 22, alerts should be generated to indicate that 22ms have passed.

When 'current_time' is 15ms and 'previous_time' is 0ms, the correct calculation for 'elapsed_time' should be:

elapsed_time = current_time - previous_time = 15ms - 0ms = 15ms
 

Ya’akov

Joined Jan 27, 2019
10,235
What is the previous_time variable doing? When will it ever be non-zero?

What is it you are trying to understand? If you increment a counter each millisecond then that counter reflects the time since you began counting, which is identical to the elapsed time so far as I can tell by your secnario.

You are making something dead simple into a confusion of unnecessay parts. What are you not saying that would change this?
 

Thread Starter

MTech1

Joined Feb 15, 2023
181
What is the previous_time variable doing? When will it ever be non-zero?

What is it you are trying to understand?
I am trying to understand how a system can notify events at specific intervals, such as every 15 ms and 22 ms, when using a 1 ms timer interrupt.

I see to develop the logic for this scenario, it's essential to consider three variables: 'current_time,' 'previous_time,' and 'elapsed_time.' These variables help in tracking and determining when the specified time intervals have passed.

I want to know how the system notify when it has these three time values
 

Ya’akov

Joined Jan 27, 2019
10,235
I am trying to understand how a system can notify events at specific intervals, such as every 15 ms and 22 ms, when using a 1 ms timer interrupt.

I see to develop the logic for this scenario, it's essential to consider three variables: 'current_time,' 'previous_time,' and 'elapsed_time.' These variables help in tracking and determining when the specified time intervals have passed.

I want to know how the system notify when it has these three time values
There are numerous tutorials online concerning state machines and interrupts. I would recommend reading/watching some.

Your approach to ”learning” is… novel… and highly restrictive. You don’t start by driving a stake in the ground made of parts you don’t understand.
 

boostbuck

Joined Oct 5, 2017
1,043
'Current-time' and 'previous-time' are variables that are used to derive 'elapsed-time' from a continuously running clock.

However, if you have a 1mS event available then elapsed-time is given to you, and current-time and previous-time are irrelevant, and elapsed-time is just a counter of event occurrences.
 

Thread Starter

MTech1

Joined Feb 15, 2023
181
'Current-time' and 'previous-time' are variables that are used to derive 'elapsed-time' from a continuously running clock.

However, if you have a 1mS event available then elapsed-time is given to you, and current-time and previous-time are irrelevant, and elapsed-time is just a counter of event occurrences.
We start by setting previous_time to 0ms.
When current_time reaches 15ms, record the elapsed time for the first interval: elapsed_time_1 = current_time - previous_time. This will give the time elapsed since the start, which should be 15ms.
Now, we set previous_time to the current current_time, which is 15ms.
When current_time reaches 22ms, record the elapsed time for the second interval: elapsed_time_2 = current_time - previous_time. This will give the time elapsed since the last measurement, which should be 7ms. But we need 22 ms ?
 

tumbleweed

Joined Jun 27, 2023
19
Code:
program start:
current_time = 0
elapsed_time = 0
previous_time = 0


every 1ms (interrupt):
current_time = current_time + 1
elapsed_time = current_time - previous_time
if (elapsed_time >= 15) then        // generate an alarm every 15ms
    ALARM
    previous_time = current_time        // record new previous_time
endif
 

boostbuck

Joined Oct 5, 2017
1,043
current_time and previous_time are irrelevant if you have a 1 mS interrupt, as I already said.
All you require is elapsed_time to count interrupts.

In your example in your first post, you could have two elapsed-time counters. Each is incremented on interrupt. When one reaches 15mS it activates '15mS-alert' and is reset to zero. The other does the same at 22mS. This allows two asynchronously independent timing events which seems to be what you want to achieve.
 

Thread Starter

MTech1

Joined Feb 15, 2023
181
current_time and previous_time are irrelevant if you have a 1 mS interrupt, as I already said.
All you require is elapsed_time to count interrupts.
Thank you, I now understand the concept you were explaining.

C:
// Constants for time intervals (in milliseconds)
#define INTERVAL_1 15
#define INTERVAL_2 22

// Global elapsed-time counters
volatile unsigned int elapsed_time_1 = 0;
volatile unsigned int elapsed_time_2 = 0;

// Flags to indicate when to trigger events
volatile bool trigger_event_1 = false;
volatile bool trigger_event_2 = false;

// Interrupt handler for a 1ms timer
void timer_interrupt_handler() {
    // Increment both elapsed-time counters
    elapsed_time_1++;
    elapsed_time_2++;

    // Check if the first interval has elapsed
    if (elapsed_time_1 >= INTERVAL_1) {
        elapsed_time_1 = 0; // Reset the counter
        trigger_event_1 = true; // Set the flag to trigger the event
    }

    // Check if the second interval has elapsed
    if (elapsed_time_2 >= INTERVAL_2) {
        elapsed_time_2 = 0; // Reset the counter
        trigger_event_2 = true; // Set the flag to trigger the event
    }
}

// Main loop
int main() {
  
    while (1) {
        // Check if it's time to trigger event 1
        if (trigger_event_1) {
            printf("Event 1 triggered!\n");
            trigger_event_1 = false; // Reset the flag
        }

        // Check if it's time to trigger event 2
        if (trigger_event_2) {
            printf("Event 2 triggered!\n");
            trigger_event_2 = false; // Reset the flag
        }
    }

    return 0;
}

I was confused by reading code in the Arduino link below.
https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay
 
Last edited:

boostbuck

Joined Oct 5, 2017
1,043
The example uses millis() which is effectively a running clock, so measuring elapsed time requires removing previous_time from current_time. That's not a very efficient or elegant way to control tightly timed events.
 

Thread Starter

MTech1

Joined Feb 15, 2023
181
Hi MTech,

Why do you consider this action is required?
Now, we set previous_time to the current current_time, which is 15ms.

E
In the example I gave earlier, previous_time should not to used to measure the total time since the program started. Instead, it' should used to measure the time that has passed since the last action or event took place.
 
Top