How can I create a specific stopwatch

12 views (last 30 days)
Ahsen Abid
Ahsen Abid on 27 Aug 2018
Commented: Adam Danz on 27 Aug 2018
I've just started using MATLAB and was wondering if I could make a stopwatch timer which displays tasks after a specific time. Multiple tasks ie.
At 40 seconds - Display " Task_1" text
At 60 seconds - Display " Task_2" text
At 95 seconds - Display " Task_3" text
And a way to reset the timer, including all tasks. The tasks should be noticeable when displayed as well.
Layout of the timer should be in hh:mm:ss
  4 Comments
Ahsen Abid
Ahsen Abid on 27 Aug 2018
Edited: Ahsen Abid on 27 Aug 2018
Well it would be convenient to have implement the code into GUI, but at the moment, I managed to find a code somewhere on the site which executes a command after a specific time. So the current code I'm using is
t = timer('TimerFcn', 'stat=false; disp(''Task_1'')',...
'StartDelay',4);
start(t)
stat=true;
dispTime = 1;
while(stat==true)
disp(dispTime)
pause(1)
dispTime = dispTime + 1;
end
t = timer('TimerFcn', 'stat=false; disp(''Task_2'')',...
'StartDelay',15);
start(t)
stat=true;
while(stat==true)
disp(dispTime)
pause(1)
dispTime = dispTime + 1;
end
t = timer('TimerFcn', 'stat=false; disp(''Task_3'')',...
'StartDelay',5);
start(t)
stat=true;
while(stat==true)
disp(dispTime)
pause(1)
dispTime = dispTime + 1;
end
This is pretty much what I was looking for but further question:
- Can I implement this code into a GUI? ie. having this on a digital display clock and on the side displaying "Task_n" when it's time?
Sorry if my questions seem bad
And to answer Adam's query, I'm currently running scripts
Adam Danz
Adam Danz on 27 Aug 2018
You could add a count-down clock to the GUI I shared below.

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 27 Aug 2018
I coded up a demo for you. The m file is attached but I've also included the code below so I can walk you through it. I suggest you download the m file so you can run it and play around with it.
This section sets up a quick and dirty GUI that allows the user to
  1. Select a task type (each task time will have its own message)
  2. Enter the delay period (second)
  3. Control the timer (start, stop, reset).
% Set up GUI
h.fig = figure('name', mfilename);
options = {'task 1', 'task 2', 'task 3'};
h.list1 = uicontrol('style', 'listbox', 'units', 'normalize', 'position', [.25 .30 .45 .51], 'string', options);
h.text1 = uicontrol('style', 'text', 'units', 'normalize', 'position', [.25 .83 .45 .06], 'string', 'Select task message', 'fontsize', 12);
h.edit1 = uicontrol('style', 'edit', 'units', 'normalized', 'position', [.80 .72 .12 .06], 'string', 40);
h.text1 = uicontrol('style', 'text', 'units', 'normalize', 'position', [.70 .81 .3 .05], 'string', 'Delay (seconds)', 'fontsize', 12);
h.button1 = uicontrol('style', 'pushbutton', 'units', 'normalize', 'position', [.20 .03 .10 .10], 'string', 'Start');
h.button2 = uicontrol('style', 'pushbutton', 'units', 'normalize', 'position', [.45 .03 .10 .10], 'string', 'Stop');
h.button3 = uicontrol('style', 'pushbutton', 'units', 'normalize', 'position', [.70 .03 .10 .10], 'string', 'Reset');
set(h.button1, 'callback', {@controlTimerCallback, 'start', h})
set(h.button2, 'callback', {@controlTimerCallback, 'stop', h})
set(h.button3, 'callback', {@controlTimerCallback, 'reset', h})
This callback function is executed by all of the GUI button and it either creates a new timer, stops an existing timer, or resets an existing timer.
function controlTimerCallback(hObj, ~, command, handles)
% Receives command from GUI to control timer(s)
% get time delay (seconds)
delayTime = str2double(handles.edit1.String);
% get the message type
msgType = handles.list1.String{handles.list1.Value};
% Name of timer for this task
timerName = sprintf('announcementTimer %s', msgType);
% Perform command
switch command
case 'start'
% Create new timer. Ideally, you'd want to check if one exists under this task name alread.
% Create timer object; name it after the task type.
t = timer('name', timerName);
t.ExecutionMode = 'singleShot';
t.StartDelay = delayTime;
t.TimerFcn = {@dispTimerMsg msgType};
% Start timer; it will stop after 1 cycle.
start(t)
case 'stop'
% Search for a timer with task name and stop it.
% The search query needs to match the naming convensions in the 'start' case.
t = timerfind('name', timerName);
stop(t)
case 'reset'
% To reset, we're just going to close and restart the timer.
controlTimerCallback(hObj, [], 'stop', handles);
controlTimerCallback(hObj, [], 'start', handles);
otherwise
error('Unknown command.')
end
Finally, this callback function is executed by the timer and displays a message box after the timer has reached its delay period (if it hasn't been stopped).
function dispTimerMsg(~, ~, msgType)
% This function is called by the timer object 'tObj' and displays a message
% chosed by 'msgType'.
switch msgType
case 'task 1'
msg = 'Task 1 complete';
icon = 'none';
case 'task 2'
msg = 'Task 2 complete';
icon = 'none';
case 'task 3'
msg = 'Task 3 complete';
icon = 'none';
otherwise
msg = 'Error: Message type unknown';
icon = 'error';
end
% here the mode is set to 'replace' such that future msgboxs will replace ones already in the screen.
msgbox(msg, 'Timer', icon, 'replace')
The message boxes will overwrite each other if >1 appears but that can be changed by removing 'replace' in the msgbox() call.
  1 Comment
Adam Danz
Adam Danz on 27 Aug 2018
Note that matlab warns again using their timer class for real-time applications that require greater precision.

Sign in to comment.

Categories

Find more on Environment and Settings 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!