Help with timing a pushbutton being pushed twice

6 views (last 30 days)
Amed
Amed on 12 Feb 2013
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.

Answers (2)

Kye Taylor
Kye Taylor on 12 Feb 2013
Edited: Kye Taylor on 12 Feb 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 Comments
Kye Taylor
Kye Taylor on 12 Feb 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.

Sign in to comment.


Amed
Amed on 12 Feb 2013
Edited: Amed on 12 Feb 2013
Thanks guys I will check the answers when i'm back from class.
In terms of getting strange answers, i'm getting normal absolute values (except for the first value, seems to be random or some sorts?), but afterwards the intervals ARE correct. If I can just somehow void the first value and then take the absolute values before converting the time (s) into (beats/minute)
As for tic/toc functions, I couldn't get them to measure two separate push's on the same exact button. They read out twin numbers each time.

Products

Community Treasure Hunt

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

Start Hunting!