MATLAB Answers

1

Hi, I want 2 timer objects to execute sequentially, one after the other. Please help

Asked by Mitra Bapat on 2 Mar 2015
Latest activity Commented on by Mitra Bapat on 6 Mar 2015
Suppose my program is as follows:
t = timer;
t.StartDelay = 3;
t.TimerFcn = @(myTimerObj, thisEvent)disp('hello');
start(t);
t1 = timer;
t1.StartDelay = 5;
t1.TimerFcn = @(myTimerObj, thisEvent)disp('world');
start(t1);
I want my program to display 'hello' after 3 seconds, which it does. But it displays 'world' 2 seconds after 'hello' is displayed. i.e. simultaneous execution takes place.
I want 'world' to be displayed 5 seconds after 'hello' is displayed. And making the delay 8 is not the answer I'm looking for! I need sequential execution because I want to put it in an infinite loop.
It should display 'hello' after 3 seconds, and 5 seconds later 'world'. Then 3 more seconds later 'hello' should be displayed, and 5 seconds after that, 'world'. This is needed in an infinite loop.
Please help
Thank you!

  0 Comments

Sign in to comment.

1 Answer

Answer by Oleg Komarov on 2 Mar 2015
Edited by Oleg Komarov on 2 Mar 2015
 Accepted Answer

You can use one timer and a function that alternate the state(s):
tic
t = timer;
t.ExecutionMode = 'fixedDelay';
t.StartDelay = 3;
t.TimerFcn = @(obj,evt) alternateState(obj);
start(t);
% After you are done
stop(t)
clear persistent
And the state alternation is done by:
function alternateState(t)
persistent on
if isempty(on)
on = true;
end
toc
stop(t)
if on
disp('hello')
t.StartDelay = 5;
on = false;
else
disp('world')
t.StartDelay = 3;
on = true;
end
start(t)
end
The state persists within the scope of the function.
Basically, you start the timer on repeated execution with fixed delay, and initial delay of 3. When the first time the function fires, it initializes the state to 'on', prints 'hello', sets the next delay to 5. On the next call, it reverts the delay back and prints 'world'.
Note, I am not sure this is clean, and would appreciate feedback, but it has the advantage to support several states. You can also clean away the tic/toc which is there for debugging purposes.

  3 Comments

Thanks a lot! It works properly, just like I wanted.
Now in my project, I have 2 webcams. I want to capture images with them alternately and automatically, and then do further processing. The time elapsed between the capturing of any 2 images(1 from camera_1 and the other from camera_2) is variable (it depends on the result of processing on previous image)... So I just passed those variables as arguments in the alternateState function.
So, state alternation should take care of alternating between the 2 cameras, and 'persistent' will help in automation. Am I right so far?
But now I'm not sure how to move forward.
If I use the snapshot function, what will the syntax be? Also, snapshot function requires MATLAB 2014a or 2014b. I'm currently using 2009 version. So is there any other method to do all this, if snapshot won't work on the 2009 version?
Please help, Thank you...
Actually, why do you think you need a timer? For the snapshot question, please ask it separately, since I have not used the function before and others might help.

Sign in to comment.