setting a timer Matlab

Trying to start a timer when a button is pushed and stop the timer when the button is pushed again and store the times everytime any of the four buttons are pushed. I tried reading on the Matlab help page on how to make a timer but got lost. This is the code I have right now but the times in the command window are all over the place and are not stored when the button is turned off. Any help would be appreciated! Thanks
a = arduino; %register Arduino Uno
t = timer('StartFcn',@(~,~)disp('timer started.'),'TimerFcn',@(~,~)disp(rand(1)));
while 1==1 %make code continuous
pin1 = readDigitalPin (a, 'D2'); %continuously read button 1
pin2 = readDigitalPin (a, 'D4'); %continuously read button 2
pin3 = readDigitalPin (a, 'D6'); %continuously read button 3
pin4 = readDigitalPin (a, 'D8'); %continuously read button 4
if pin1==1 %if button 1 is pushed
start(t);
writeDigitalPin(a, 'D10', 1); %turn on RGB LED to green
else %when button 1 is pushed again
stop(t);
writeDigitalPin(a, 'D10', 0); %RGB LED turns off
end
if pin2==1 %if button 2 is pushed
start(t);
writeDigitalPin(a, 'D13', 1); %turn on yellow LED
else %when button 2 is pushed again
stop(t);
writeDigitalPin(a, 'D13', 0); %LED turns off
end
if pin3==1 %if button 3 is pushed
start(t);
writeDigitalPin(a, 'D12', 1); %turn on red LED
else %when button 3 is pushed again
stop(t);
writeDigitalPin(a, 'D12', 0); %LED turns off
end
if pin4==1 %if button 4 is pushed
start(t);
writeDigitalPin(a, 'D11', 1); %turn on RGB LED to blue
else %when button 4 is pushed again
stop(t);
writeDigitalPin(a, 'D11', 0); %RGB LED turns off
end
end

8 comentarios

Tommy
Tommy el 9 de Abr. de 2020
if pin1==1 %if button 1 is pushed
start(t);
writeDigitalPin(a, 'D10', 1); %turn on RGB LED to green
else %when button 1 is pushed again
stop(t);
writeDigitalPin(a, 'D10', 0); %RGB LED turns off
end
Based on this, if you hold down button 1, start(t) will be called over and over again. Whenever button 1 isn't being held, stop(t) will be called over and over again. If you want to treat the button like a toggle button, I think something like this would work:
button1Down = false;
button1FirstClickNext = true;
% ^ put before while loop
...
% within while loop:
if pin1 && ~button1Down %if button 1 is _newly_ pushed
button1Down = true;
if button1FirstClickNext
start(t);
writeDigitalPin(a, 'D10', 1); %turn on RGB LED to green
else
stop(t);
writeDigitalPin(a, 'D10', 0); %RGB LED turns off
end
button1FirstClickNext = ~button1FirstClickNext;
elseif ~pin1 && button1Down %when button 1 is _newly_ released
button1Down = false;
end
with similar code for each button. I don't think that will totally solve your problem, but let me know if the command window output at least corresponds to button pushes.
Madison Gladstone
Madison Gladstone el 9 de Abr. de 2020
I am actually using latching push buttons and the random numbers start generating when the button is pushed and stop when the button is pressed again. I just think the code is displaying random numbers instead of time elapsed.
Tommy
Tommy el 9 de Abr. de 2020
Oops my bad!
They are displaying random numbers, because of this line:
t = timer('StartFcn',@(~,~)disp('timer started.'),'TimerFcn',@(~,~)disp(rand(1)));
% ^ random number
If you are just trying to get the time between button pushes, I would recommend using tic and toc instead of a timer, such as:
t1Start = tic;
...
if pin1==1 %if button 1 is pushed
t1Start = tic;
writeDigitalPin(a, 'D10', 1); %turn on RGB LED to green
disp('button 1 first push')
else %when button 1 is pushed again
t1 = toc(t1Start)
disp([num2str(t1) ' seconds passed between button 1 presses'])
writeDigitalPin(a, 'D10', 0); %RGB LED turns off
disp('button 1 second push')
end
And if you want to store all of the times for button 1, try this:
t1Start = tic;
t1 = [];
...
if pin1==1 %if button 1 is pushed
t1Start = tic;
writeDigitalPin(a, 'D10', 1); %turn on RGB LED to green
disp('button 1 first push')
else %when button 1 is pushed again
t1 = [t1 toc(t1Start)];
disp([num2str(t1(end)) ' seconds passed between button 1 presses'])
writeDigitalPin(a, 'D10', 0); %RGB LED turns off
disp('button 1 second push')
end
Hopefully this helps. I put those extra disp statements in there because I am wondering if you do still need an extra variable to keep track of the button's current state, like button1Down did above. I could be wrong again.
Geoff Hayes
Geoff Hayes el 9 de Abr. de 2020
Madison - where is there code to display the time elapsed? You could use tic and toc to get that elapsed time. In this example, a simple GUI is created with two buttons - one that starts the timer and one that stops it. When the timer is started, the start timer callback fires and we call tic. When the timer is stopped, the stop timer callback fires and we call toc to get the elapsed time since the timer was started.
function elapsedTimerTest
t = timer('StartFcn', @startCallback, 'StopFcn', @stopCallback, 'TimerFcn',@timerCallback, 'ExecutionMode', 'fixedRate');
figure('Visible','on','Position',[500,500,120,100], 'MenuBar', 'none');
uicontrol('Style','pushbutton','String','Start Timer',...
'Position',[25,60,70,25], 'Callback', @(~,~)start(t));
uicontrol('Style','pushbutton','String','Stop Timer',...
'Position',[25,25,70,25], 'Callback', @(~,~)stop(t));
elapsedTimes = [];
function startCallback(~,~)
tic;
end
function stopCallback(~,~)
elapsedTimes = [elapsedTimes toc];
end
function timerCallback(~,~)
% do nothing
end
end
Note how the elapsedTimes array is updated every time the timer stops. The above might be helpful.
From your code, I don't really understand how the buttons work - is it possible to have 2 or more of the four buttons on at the same time? Or if one is off, does that mean another is on? Suppose button or pin3 is on then we turn it of by turning on pin1 (is this possible?). Your code would then start the timer for pin3, start the timer for pin1, and then stop the timer for pin3...which would mean the timer is stopped (since you have one timer that is shared for all pins/buttons).
Geoff Hayes
Geoff Hayes el 9 de Abr. de 2020
Madison Gladstone
Madison Gladstone el 9 de Abr. de 2020
Hi Tommy,
Thanks for your response! I tried your code and it is what I am looking for but oppposite. Currently it is taking the time between the second button push and the first button push instead of the time between the first button push and the second button push. I've tried messing with your code to see if i could reverse it but to no avail... maybe you know how?
Madison Gladstone
Madison Gladstone el 9 de Abr. de 2020
Nevermind Tommy I got it all figured out!!! Thank you so much for the help!
Steven Lord
Steven Lord el 9 de Abr. de 2020
I'm guessing that your actual application for which you want to use this technique isn't to display random numbers when a button is depressed. Can you say a little more about the actual application? It's possible that the functionality to achieve your main goal already exists and doesn't require a timer.
For example, if you wanted to record for how long the button remained depressed, instead of using a timer I would store the current time (returned by datetime('now') as a datetime array) when the button is pressed and subtract the stored time from the current time when the button is released.
rightNow = datetime('now');
pause % wait for the user to end the pause
howlong = datetime('now')-rightNow;
disp("MATLAB was paused for " + seconds(howlong) + " seconds." + newline)

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Preguntada:

el 8 de Abr. de 2020

Comentada:

el 9 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by