FSM Lecture 28- Exercise-003 Dispatching time tick event

  • Post author:
  • Post category:Blog

 

Exercise-003 Dispatching time tick event

 

 

In the previous article, we implemented the code to dispatch the user event.

Let’s write a small code to dispatch the time tick event. For that, we will use the millis() function provided by the Arduino framework.

 

millis()

It returns the number of milliseconds passed since the Arduino board began running the current program. This number will overflow(go back to 0), after approximately 50 days. Because its datatype is unsigned long, the width is 4 bytes. 

Syntax: time=millis()

Figure 1. Example code
Figure 1. Example code

 

Here, they have used millis(). And in this code statement: myTime = millis(), they are storing the current time into the myTime variable. The current time is not the clock time; it is the number of milliseconds passed since the Arduino board began running the program. That means a number of milliseconds passed before executing this statement. So, that value is stored in myTime, and we will use millis() function. 

 

In our project, the fourth task is, ‘Dispatch the time tick event for every 100 milliseconds’. Because that’s what we decided to do, our state machine receives the time tick event for every 100 milliseconds.

 

Figure 2. Loop function
Figure 2. Loop function

 

I will create one local static variable in the loop function. current_time – this is to store the current time; This returns the saved current time. Its data type is uint32_t, and initially, I will store the current time using the millis() functions.  Since it is a static variable, the first time when the control reaches this loop, only one time this variable will be initialized to the current_time. That is, in terms of the number of milliseconds. 

//4. dispatch the time tick event for every 100ms
if(millis() - current_time >= 100){
  //100ms has passed
  current_time = millis();
  te.super.sig = TIME_TICK;
  if(++te.ss > 10) te.ss = 1;
  protimer_event_dispatcher(&protimer,&te.super);
}

Dispatch the Time tick event function code

After that, I will write one small logic. if(millis() – current_time >= 100). This millis() function is the actual running current_time, minus the saved current_time. If this difference is equal to or grows bigger than 100 milliseconds, that means 100 milliseconds has passed.

In that case, what should we do? We should first reset this current_time because, for the next event generation, we have to reset this with millis(). I’m just resetting this variable with the current time. Because, current_time = millis() code in loop function will not execute for every loop, so because it’s a static variable. That’s why I am again initializing it. 

Exercise-003 Dispatching time tick event
Figure 2. Loop function

 

Let’s create one variable. Protimer_tick_event t te = {.ss = 0}; The ‘ss’ field, that is = 0. Because we have to use the sub-second field. 

Then, we have to make an event te.super.sig = TIME_TICK; ++te.ss; Whenever 100 milliseconds have passed, we have to increment this. Its value varies from 1 to 10. That’s why, if te.ss grows bigger than 10, then we have to reset te.ss to 1. Or you can do if(++te.ss > 10) te.ss = 1). That means ‘te’ has to be the static variable because it can’t be a local variable. Otherwise, for every loop, this will be initialized to 0. When it becomes 11, it resets to 1.  After that, we have to call the event_dipatcher te.super. That’s about dispatching the time tick event to the state machine.

And you can also do this using a timer interrupt for every 100 milliseconds, you can generate a timer interrupt, and you can also do that.

In the upcoming article, let’s understand the button bouncing, and we will try to implement this. 

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1 

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.