Timing a train on a loop
조회 수: 1 (최근 30일)
이전 댓글 표시
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
답변 (1개)
John Petersen
2012년 11월 2일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Downloads에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!