Setup a continuous arduino timer?
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello! I'm essentially building a bike speedometer and i want to initialize a timer in matlab that begins when the arduino turns on. Preferably, I would have a variable t which updates every tenth of a milisecond. My idea was to take the value of this variable every time i had an external interupt (this interupt is produced by a magnetic field sensor which actuates every time a wheel makes a revolution) and take the time between interrupts in order to get the frequency of rotation of a wheel. My question ishow do I setup some sort of counter that records the time (or rather change in time since any arbitrary start) down to miliseconds or tenths of miliseconds and be able to take this value as it continuously updates for the rest of my code. any advice is appreciated, I'm having some trouble looking through online resources.
댓글 수: 1
Geoff Hayes
2020년 3월 15일
Declan - do you really mean you want to update the variable every tenth of a millisecond? Because that implies that the period of the timer would need to be 0.0001 which seems to be unsupported (see period description) as the minimum allowed value is one millisecond (0.001). If one millisecond is sufficient, then you could nest your timer callback within your main function so that the timer callback has access to the variable t. Perhaps something like
function myTimerTest
t = 0;
cumulativeTimerCallbackStart = 0;
periodSec = 0.001;
numTasksToExecute = 100;
myTimer = timer('Name','MyTimer', ...
'StartDelay', periodSec, ...
'ExecutionMode','fixedRate', ...
'TimerFcn',{@timerCallback}, ...
'TasksToExecute', numTasksToExecute, ...
'StartFcn', @startCallback, ...
'StopFcn', @stopCallback, ...
'Period', periodSec);
start(myTimer);
function timerCallback(hObject, eventdata)
t = toc(cumulativeTimerCallbackStart);
end
function startCallback(hObject, eventdata)
cumulativeTimerCallbackStart = tic;
end
function stopCallback(hObject, eventdata)
fprintf('Elapsed time after %d iterations: %.4f\n', numTasksToExecute, t);
end
where tic and toc are used to get the elapsed time between each timer callback and so "should" be a decent t. Note that the smaller the period, the greater the delay introduced between each callback firing (so you won't necessarily see the callback fire every 1 ms if that is what you have set the period to be).
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Clocks and Timers에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!