MATLAB Creating a Countdown Timer in MATLAB

  • Thread starter Thread starter amazingzuc
  • Start date Start date
  • Tags Tags
    Matlab Timer
AI Thread Summary
The discussion focuses on creating a countdown timer in MATLAB using the tic and toc functions. The initial code provided includes a basic structure for the timer, prompting the user for input and displaying elapsed time. However, the user seeks assistance with the loop functionality to effectively count down from the specified time. A suggested solution introduces a new function that checks if one second has elapsed and updates the countdown accordingly, displaying the remaining time until it reaches zero. The conversation emphasizes the importance of accurately managing the timer's state and ensuring the countdown operates smoothly.
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
Views
10K
Replies
2
Views
3K
Replies
5
Views
1K
Replies
10
Views
3K
Replies
2
Views
44K
Back
Top