Use of timer: what's the code to indicate that its period of time is expired?

2 views (last 30 days)
I created a timer with a period of 30 seconds: the program has to execute some tasks if this period is passed. How do i write the code for expressing the fact that the period is passed?

Answers (2)

Doug Hull
Doug Hull on 3 Apr 2013
Edited: Doug Hull on 3 Apr 2013
It is not clear what you are asing, but this is a primer on timers.
  1 Comment
Umberto
Umberto on 4 Apr 2013
I didnt't find the answer. Here is my code
V = xlsread('data.xls', 1, 'C2:C1002'); %import data from Excel
t = timer('Period', 30, 'TimerFcn', @(~,~)calculate(V, g, n, Vmin, Vmax));
function calculate(V, g, n, Vmin, Vmax)
for i=1:n
if V(i) < Vmin
start(t);
if %30 seconds have passed
V(i) = V(i)+ g;
end
elseif V(i) > Vmax
start(t);
if %30 seconds have passed
V(i) = V(i)- g;
end
elseif (V(i) >= Vmin && V(i) <= Vmax)
stop(t);
end
end
how do I express in code the sentence "30 seconds have passed"?

Sign in to comment.


Sean de Wolski
Sean de Wolski on 4 Apr 2013
One thing you could be do would be to toc a tic that was started earlier. However, the best approach would be to get the InstantPeriod from the timer object
function timerNSeconds
T = timer('Period',10,... %period
'ExecutionMode','fixedRate',... %{singleShot,fixedRate,fixedSpacing,fixedDelay}
'BusyMode','drop',... %{drop, error, queue}
'TasksToExecute',2,...
'StartDelay',0,...
'TimerFcn',@(src,evt)tfcn(src,evt,pi),...
'StartFcn',[],...
'StopFcn',[],...
'ErrorFcn',[]);
start(T);
end
function tfcn(src,evt,piapprox)
pause(10) %comment this line to have it not exceed
pi
if get(src,'InstantPeriod') > 10
disp('Period exceeded 10');
end
end
Uncomment the pause to see it working with not exceeding the 10s.
  20 Comments
Umberto
Umberto on 12 Apr 2013
But if I pass src to calculate() then why I get the "Undefined function or variable 't'" message?
Sean de Wolski
Sean de Wolski on 12 Apr 2013
Because you don't pass t!!!! You've renamed it to src even though it is the same thing. You call it t instead if you wish; it's just a good programming practice to call the handle of the source object of a callback src or similar.

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!