Clear Filters
Clear Filters

How to write TimerFcn code with logic

6 views (last 30 days)
Sheikh Khaleduzzaman
Sheikh Khaleduzzaman on 20 Dec 2023
Answered: Binaya on 11 Jan 2024
Hi All,
I have the following command line to switch ON/OFF a system.
body = struct('switch','on');
response = webwrite(url,body, opts); % to control ON/OFF
Also I have following code to get sensor data from system via database
rawdata = get_data(start date,end date,sensorlist,api,[],[],[],[]); % access system temperature and ambient temperature.
Now I am trying to write a TimerFcn code where "response" code will be execute based on system temperature and ambient temperature data.
Example:
if system temperature => ambient temperature then send "response" command to OFF the system
if system temperature < ambient temperautre then send "response" command to ON the system
I am here upto, but dont know how to put "response" and "rawdata" lines together with logic mentioned above for @TimerFcn.....also in here i am geeting data based on start and end date, not sure how I can read data over time from database to check the logic.
t = timer(...
'ExecutionMode', 'fixedRate', ... % Run timer repeatedly
'Period', 1, ... % Period in seconds
'BusyMode', 'drop',... % Drop timer updates if previous is still processing
'TimerFcn', {@TimerFcn},... % Specify callback function
);
disp('Starting timer...');
start(t);
delete(t)
Any help will be highly appreciated. Thanks

Answers (1)

Binaya
Binaya on 11 Jan 2024
Hi Sheikh
Based on my understanding, you would like to call the "TimerFcn" callback function periodically. Inside this callback function you would like to get data from database and send a "response" to the system based on the provided conditions.
For achieving the above requirement, please follow the below steps:
  1. Create a function, say, "TimerFun".
  2. Inside this function, write the code for reading the data from database and the code for sending the "response" of "ON" or "OFF" to the system.
  3. Inside the "timer" object, create a function handle for the function "TimerFun" and pass it as argument to 'TimerFcn' option.
Let us assume the "TimerFun" callback function is defined as below,
function TimerFun(url, opts, api, sensorlist)
...
end
The function handle for this function will look something like this:
{@TimerFun, url, opts, api, sensorlist}
Please refer to the following documentation for more information on:
  1. Function handle: https://www.mathworks.com/help/matlab/matlab_prog/creating-a-function-handle.html
  2. Timer Callback Functions: https://www.mathworks.com/help/matlab/matlab_prog/timer-callback-functions.html
  3. timer: https://www.mathworks.com/help/matlab/ref/timer.html
I hope this helps.
Regards
Binaya

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!