How to make a clock program

조회 수: 44(최근 30일)
Braeden McKeown
Braeden McKeown 2021년 3월 15일
댓글: Image Analyst 2021년 9월 13일
How would I start and or make a code for a clock function that can count up,down,or pause depending on user input?
Any help would be appreciated!!!

답변(2개)

Isabelle Foster
Isabelle Foster 2021년 3월 15일
이 답변에 1명의 참여자가 플래그를 지정함
⚡Open Notepad
⚡To start off, type
@echo off
this code is basically saying that you want your program to display something.
⚡The next code on a new line is
:start
The " : " means that you have a specific point you want to return to. I then typed "start" to specify a name for this point. You can use whatever you want, i only used "start" for the sake of simplicity.
⚡ More code
cls
This code is just a shortened version of the words "Clear Screen". And that is what it does. Later on we will find that clearing the screen makes the program run smoother and visually attractive.
⚡ Codes, codes, codes
echo %time%
The code "echo" tells the program to prompt whatever follows on that line.
The %time% just means to display the time that the computer has.
⚡ [Insert funny one-liner here]
goto start
Whoever made this programming language honestly just put the words "go" and "to" together and made it go to a specified point. In this case, I made it go back to "start". Don't Pass Go. Don't Collect 200.
⚡ Saving a program from Notepad is different from saving a text document. There are a few step you must take for it to work correctly.
1. Where it says "Save as type", choose the option "All Files".
2. Save your program as Clock.bat . The ".bat" tells the computer what programming language you used
Choose a safe place to save this and hit save.
You have now officially written a program. Now if you open it you may notice that it looks a little sketchy, but no worries. These next few steps will show you how to amp up your program.
⚡Lets add some color to our project. Add the command
color 0a
to a new line under "@echo off". "0a" Is just a color code for light green text on a black background. You can use any color combination you want, but 0a is the most stereotypical color scheme for a hacker.
⚡If you opened the program then you would have noticed that it flashed each time at you. I found this to be distracting and ugly, so I added the command:
timeout 0 /nobreak>null
This is a more advanced command, so I won't explain it all to you. It basically tell the program to wait for "0" seconds and then continue without letting the user know it paused. If you look up the "timeout command", you can learn more.
⚡Almost done. Add the command:
title Clock
on the first line, on its own line.(Reference the picture above). This just make the name of window that will open "Clock".
And now you're Done.
When you open it now it should look a lot nicer and more appealing. I simply shrunk mine down to a reasonable size.
This was the first program I ever wrote when I was first starting out (Not as advanced obviously) and still enjoy the look of it. I hope to have inspired all of you to carry on with writing programs, but if not, at least you have a new clock.
Have a good day while never having to worry about what time it is ever again.
  댓글 수: 2
Image Analyst
Image Analyst 2021년 9월 13일
@Oliveira Pereira, did you see my Answer below?

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2021년 9월 10일
Attached is one way.
% Reference web site:
%http://blogs.mathworks.com/videos/2010/12/03/how-to-loop-until-a-button-is-pushed-in-matlab/?dir=autoplay
function varargout = clock_app(varargin)
% CLOCK_APP MATLAB code for clock_app.fig
% CLOCK_APP, by itself, creates a new CLOCK_APP or raises the existing
% singleton*.
%
% H = CLOCK_APP returns the handle to a new CLOCK_APP or the handle to
% the existing singleton*.
%
% CLOCK_APP('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in CLOCK_APP.M with the given input arguments.
%
% CLOCK_APP('Property','Value',...) creates a new CLOCK_APP or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before clock_app_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to clock_app_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help clock_app
% Last Modified by GUIDE v2.5 10-Sep-2021 16:39:55
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @clock_app_OpeningFcn, ...
'gui_OutputFcn', @clock_app_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before clock_app is made visible.
function clock_app_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to clock_app (see VARARGIN)
% Choose default command line output for clock_app
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes clock_app wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = clock_app_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in btnCountUp.
function btnCountUp_Callback(hObject, eventdata, handles)
% hObject handle to btnCountUp (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.btnStop.UserData = 0;
i = 1;
while i < 10000
% your iterative computation here
i = i + 1;
message = datestr(now);
handles.text1.String = message;
drawnow
pause(0.1);
if get(handles.btnStop, 'userdata') % stop condition
break;
end
end
% Reset the value
handles.btnStop.UserData = 0;
% --- Executes on button press in btnStop.
function btnStop_Callback(hObject, eventdata, handles)
% hObject handle to btnStop (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.btnStop.UserData = 1
% --- Executes on button press in btnCountDown.
function btnCountDown_Callback(hObject, eventdata, handles)
% hObject handle to btnCountDown (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.btnStop.UserData = 0;
currentTime = now;
startTime = tic;
i = 1;
while i < 10000
% your iterative computation here
i = i + 1;
newTime = currentTime - toc(startTime) / 86400;
message = datestr(newTime);
handles.text1.String = message;
drawnow
pause(0.1);
if get(handles.btnStop, 'userdata') % stop condition
break;
end
end
% Reset the value
handles.btnStop.UserData = 0;

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by