Help with timing a pushbutton being pushed twice
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
So i'm trying to find the time interval between a single push button when pushed twice.. here is my code so far. This is for a GUI btw
% Choose default command line output for BW2
handles.output = hObject;
set(handles.pushbutton1, 'UserData', 0);
setappdata(handles.text11, 'text11', 0);
setappdata(handles.text12, 'STARTING', 0);
setappdata(handles.text13, 'FINISH', 0);
% Update handles structure
guidata(hObject, handles);
handles.gx= 0;
handles.gy= 0;
% UIWAIT makes BW2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = BW2_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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
counter = get(hObject, 'UserData') + 1;
set(hObject, 'UserData', counter);
set(handles.text1, 'String', sprintf('%d', counter));
set(handles.text8,'String',datestr(now));
if mod(counter,2)
c = clock;
seconds = c(6);
setappdata(handles.text12, 'STARTING', seconds);
guidata(hObject, handles);
else
c = clock;
seconds = c(6);
setappdata(handles.text13, 'FINISH', seconds);
guidata(hObject, handles);
end
before = getappdata(handles.text12, 'STARTING');
after = getappdata(handles.text13, 'FINISH');
interval = (after-before)
if (interval < 5)
bpm = (2/interval)*(60/1);
set(handles.text11, 'string', bpm);
end
the problem starts under the pushbutton callback. i'm basically reading in values every time they're even or odd and measuring time between each click. My first value is always something huge like 50+ seconds, and every other value is negative.
Respuestas (2)
Kye Taylor
el 12 de Feb. de 2013
Editada: Kye Taylor
el 12 de Feb. de 2013
Computing the time elapsed between two date vectors is nontrivial, I bet you're getting some weird behavior here. Try using the etime function
t = clock
elapsedTime = etime(clock,t);
If you include the line
handles.startTime = [];
in the *_OpeningFcn callback, then the following is an example of a callback that will spit out the elapsed time between pushing the pushbutton:
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if isempty(handles.startTime)
handles.startTime = clock;
else
% get elapsed time
elapsedTime = etime(clock, handles.startTime);
% reset
handles.startTime = [];
% display elapsed time
disp(elapsedTime)
end
guidata(hObject, handles);
4 comentarios
Sean de Wolski
el 12 de Feb. de 2013
Yes. I would do all of this in an object but that's a different story.
Anyway, it doesn't matter if tic is started elsewhere. You can have the handle to a specific tic. Consider this:
t = tic;
pause(4);
tt=tic;
toc(t);
toc(tt);
Kye Taylor
el 12 de Feb. de 2013
Editada: Kye Taylor
el 12 de Feb. de 2013
Well.. once he figures out how to keep track of the state, and either pass around the outputs from tic or make them persistent, the tic/toc commands you mention are viable. I didn't realize toc could take inputs. sweet.
Ver también
Categorías
Más información sobre Startup and Shutdown en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!