Clear Filters
Clear Filters

Timing button presses code

3 views (last 30 days)
Madison Gladstone
Madison Gladstone on 8 Apr 2020
When running my code I would like it so that when a button is pushed on, a timer will start and when the button is pushed again the timer will stop and display the time it took from when the button was pushed on to when it was pushed off. The following is my current code but the tic toc does not work.
a = arduino; %register Arduino Uno
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
tic
writeDigitalPin(a, 'D10', 1); %turn on RGB LED to green
else %when button 1 is pushed again
writeDigitalPin(a, 'D10', 0); %RGB LED turns off
toc
end
if pin2==1 %if button 2 is pushed
tic
writeDigitalPin(a, 'D13', 1); %turn on yellow LED
else %when button 2 is pushed again
writeDigitalPin(a, 'D13', 0); %LED turns off
toc
end
if pin3==1 %if button 3 is pushed
tic
writeDigitalPin(a, 'D12', 1); %turn on red LED
else %when button 3 is pushed again
writeDigitalPin(a, 'D12', 0); %LED turns off
toc
end
if pin4==1 %if button 4 is pushed
tic
writeDigitalPin(a, 'D11', 1); %turn on RGB LED to blue
else %when button 4 is pushed again
writeDigitalPin(a, 'D11', 0); %RGB LED turns off
toc
end
end

Answers (1)

Rik
Rik on 8 Apr 2020
You need to review your logic. What conditions are true when a button is pressed for the first time? And how do those conditions differ from what happens when the button is pressed a second time? You probably need to keep track of whether a timer is running. If you use code like I wrote below it is easier to change behavior and add or remove pins as needed.
Note that ==1 is only needed if pin_value can also have other non-zero values.
pins=struct;
pins(1).read='D2';
pins(1).write='D10';
pins(1).RunningTimer=false;
pins(1).tic_handle=[];
pins(2).read='D4';
pins(2).write='D13';
pins(2).RunningTimer=false;
pins(3).read='D6';
pins(3).write='D12';
pins(3).RunningTimer=false;
pins(4).read='D8';
pins(4).write='D11';
pins(4).RunningTimer=false;
a = arduino; %register Arduino Uno
while true %make code continuous
for n=1:numel(pins)
pin=pins(n);%store for shorter syntax
pin_value=readDigitalPin(a,pin.read);
if pin_value==1
%button pushed
if ~pin.RunningTimer % first time
%start timer
pin.tic_handle=tic;
pin.RunningTimer=true;
writeDigitalPin(a,pin.write,1);
else
%stop timer
pin.RunningTimer=false;
toc(pin.tic_handle)
writeDigitalPin(a,pin.write,0);
end
else
%button not pushed
end
pins(n)=pin;
end
end
  3 Comments
Rik
Rik on 8 Apr 2020
You can easily modify this code to store the time:
pin.TimeElapsed=toc(pin.tic_handle);
The benefit of that line is that it will automatically overwrite any previous measurement.
I don't have an arduino myself, so I can't test any of this code. My assumption was that you had an already working interface with the hardware, leaving only the Matlab part as an issue. If I had to guess I would say the code runs too fast, so it will read the pin multiple times for each press.
I can't test the behavior of readDigitalPin so you will have to do that on your own. Use the debugging tools. Put a breakpoint in this code and see what happens line by line.
Madison Gladstone
Madison Gladstone on 8 Apr 2020
Thanks I will give it a try

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!