Timing a train on a loop
1 view (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
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
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!