Creating a Countdown Timer in MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter amazingzuc
  • Start date Start date
  • Tags Tags
    Matlab Timer
Click For Summary
SUMMARY

The discussion focuses on creating a countdown timer in MATLAB using the tic and toc functions. Two code snippets are provided, with the second one offering a more refined approach to decrementing the timer. The key improvement involves resetting the timer every second and displaying the remaining time until it reaches zero. The final output announces "BLAST OFF!" when the countdown completes.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of the tic and toc functions in MATLAB
  • Basic knowledge of control flow structures (loops and conditionals)
  • Experience with user input handling in MATLAB
NEXT STEPS
  • Explore MATLAB's pause function for more control over timing
  • Learn about MATLAB's graphical user interface (GUI) capabilities for creating interactive timers
  • Investigate MATLAB's timer objects for advanced timing functionalities
  • Study error handling in MATLAB to improve user input validation
USEFUL FOR

This discussion is beneficial for MATLAB students, educators, and developers looking to implement countdown timers or enhance their understanding of timing functions in MATLAB.

amazingzuc
Messages
1
Reaction score
0
Im trying to make a countdown timer for my MATLAB class using the tic and toc functions.
so far this is what I have:

function lab4program1
disp('Welcome to the countdown timer program');
done = false;
tic();
while ~done
time = input('Enter a time (seconds) or negative number to quit:');

****************************

disp(toc);


last_time = toc();
while (true)
elapsed_time = toc()-last_time
last_time = last_time+elapsed_time;
end

if done
disp('BLAST OFF!');
end

end


Can anyone help me with the loop that goes where the asterisks are?
 
Physics news on Phys.org
Hi what about this

function labtimer
disp('Welcome to the countdown timer program');time = input('Enter a time (seconds) ');
tstart=tic; % store starting time
while time >1 % if counter is greater than 1

if (abs(toc(tstart)-1))<0.01 % if one second has elapsed

tstart=tic; % start timer again for next second
time=time-1; % reduce the time by one as one second has elapsed
disp(time) % display the counter
end

end
end
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 7 ·
Replies
7
Views
11K
  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K