필터 지우기
필터 지우기

Timing a train on a loop

조회 수: 2 (최근 30일)
Dillon
Dillon 2012년 11월 2일
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.

답변 (1개)

John Petersen
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
  댓글 수: 1
Dillon
Dillon 2012년 11월 2일
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!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by