フィルターのクリア

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 Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by