UPDATE DATA every 5 sec

2 views (last 30 days)
Eeman Fatima
Eeman Fatima on 6 Sep 2018
Answered: mbvoyager on 6 Sep 2018
I have a ekg sensor attached which reads the heart rate and i have written the code to display Beats per minute based on it. How can add code so that it reads data every 5 seconds and save BPM.
  2 Comments
mbvoyager
mbvoyager on 6 Sep 2018
Edited: mbvoyager on 6 Sep 2018
I do not really understand your issue.
But a very quick and very very dirty solution could be to use a for or while loop and input the function pause or wait.
As seen in: function wait
This is far from exact in timing but it can limit the readings of your data to a certain coarse interval.
In general it might be a good idea if you look into the usage of timers. Can be found in: timer class.
Eeman Fatima
Eeman Fatima on 6 Sep 2018
I have this code which reads the data from the sensor and then calculates beat per minuts is there anyway i can make so that it automatically updates data every5 seconds and save the bpm for each time.

Sign in to comment.

Answers (1)

mbvoyager
mbvoyager on 6 Sep 2018
Look into the examples of
With the timer class it is possible to call a function in certain time intervals.
Here is a minimal working example exactly from the help page of the timer class:
t = timer;
t.StartFcn = @(~,thisEvent)disp([thisEvent.Type ' executed '...
datestr(thisEvent.Data.time,'dd-mmm-yyyy HH:MM:SS.FFF')]);
t.TimerFcn = @(~,thisEvent)disp([thisEvent.Type ' executed '...
datestr(thisEvent.Data.time,'dd-mmm-yyyy HH:MM:SS.FFF')]);
t.StopFcn = @(~,thisEvent)disp([thisEvent.Type ' executed '...
datestr(thisEvent.Data.time,'dd-mmm-yyyy HH:MM:SS.FFF')]);
t.Period = 5; % 5 seconds interval executing TimerFcn
t.TasksToExecute = 3; % 3 total calls excecuting TimerFcn
t.ExecutionMode = 'fixedRate';
start(t)
The output will be:
StartFcn executed 06-Sep-2018 14:15:03.161
TimerFcn executed 06-Sep-2018 14:15:03.161
TimerFcn executed 06-Sep-2018 14:15:05.162
TimerFcn executed 06-Sep-2018 14:15:07.161
StopFcn executed 06-Sep-2018 14:15:07.168
After that to delete the timer object
delete(t)
I hope this can help you.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!