Running a timer inside another timer
2 views (last 30 days)
Show older comments
Hello, so I need to run a timer inside another timer, and I need to keep the properties of each timer specific to THAT timer. I was able to get it to run the inside timer once, but it never repeated once it went to the outside timer. Seems like there is an issue with the outside timer. May I please have some assistance with this by any ideas on how to go about it? Thank you.
6 Comments
Adam Danz
on 12 Jan 2023
Edited: Adam Danz
on 12 Jan 2023
This isn't a minimal working example. If you could provide a watered down version with the bare minimum components that we could copy-paste-run, that would expedite the troubleshooting process.
Where are you starting these timers? I see thier start functions but I don't see thier start commands.
Answers (1)
Adam Danz
on 14 Jan 2023
The problem seems to be that the line that starts deprivTimer uses "app.deprivTimer" but "app" is not one of the input arguments in that function.
function CHATstep (cTimer, event) %<---------- "app" is not an input
event_type = event.Type;
switch event_type
case 'StartFcn'
disp([event_type ' dayTimer started ',datestr(event.Data.time,'dd-mmm-yyyy HH:MM:SS.FFF')]);
case 'TimerFcn'
disp([event.Type ' dayTimer executed ',datestr(event.Data.time,'dd-mmm-yyyy HH:MM:SS.FFF')]);
start(app.deprivTimer); % <--------------- HERE
case 'StopFcn'
disp([event.Type ' dayTimer Fcn complete ',datestr(event.Data.time,'dd-mmm-yyyy HH:MM:SS.FFF')]);
stop(cTimer);
end
end
To fix that, you need to include the app object in the function definitions. Here are examples you can apply to your file.
app.dayTimer.StartFcn = @(timer,event)CHATstep(app,timer,event);
app.dayTimer.TimerFcn = @(timer,event)CHATstep(app,timer,event);
app.dayTimer.StopFcn = @(timer,event)CHATstep(app,timer,event);
function CHATstep (app,cTimer, event) % <-- add app input
...
start(app.deprivTimer); % <--- now this will work
...
end
However there are some other puzzles here. First it appears that there is much time between events. This line below suggests several minutes. The other section below shows that you're pausing for potentially long periods. Make sure you reduces these to short periods of time when testing and developing.
app.dayTimer.Period = app.waitTime * 3600
case 'TimerFcn'
pause(app.deprivTimer.UserData(2)) % pauses for the specified random interval until the deprivation movement should start
pause(mTimer.UserData(1)) % pauses the function until the deprivator is finished cycling
Lastly, as mentioned in the documentation for timer, timers like these should not be used for precise control of temporal events. So if your program requires precise timing control (sub-second), be aware that of these limitations.
0 Comments
See Also
Categories
Find more on Develop Apps Using App Designer 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!