Clear Filters
Clear Filters

Timing a train on a loop

2 views (last 30 days)
Dillon
Dillon on 2 Nov 2012
I'm trying to find the time it takes a little motorized train to go from one gate(that has a break beam sensor) to another on a small track using a loop. It is connected to an arduino and everything for that is already set up, but i cant seem to get the timer to work. the train just keeps going around the track and it doesnt time anything. I've been tinkering with the code, and so far this is what I've got (<300 is un-obstructed, >300 is obstructed):
% code
clc
clear
%calc speed in motor
a=arduino('COM6');
approach=2;
departure=3;
startLoop=tic;
endLoop=toc(startLoop);
a.motorRun(1,'forward');
while a.analogRead(approach) < 300 && a.analogRead(departure) < 300;
a.motorSpeed(1,255);
if a.analogRead(approach) > 300 && a.analogRead(departure) < 300;
startLoop;
elseif a.analogRead(approach) < 300 && a.analogRead(departure) > 300;
endLoop;
end
end
a.motorSpeed(1,0);
end
I get no error codes, it just wont time.

Answers (1)

John Petersen
John Petersen on 2 Nov 2012
toc measures the elapsed time since tic was executed. You probably want something like
if a.analogRead(approach) > 300 && a.analogRead(departure) < 300;
startLoop = tic;
elseif a.analogRead(approach) < 300 && a.analogRead(departure) > 300;
endLoop = toc(startLoop);
end
  1 Comment
Dillon
Dillon on 2 Nov 2012
the set up is in the lab, so of course I have to wait until Monday to try it out, but thanks for the quick answer!

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!