Blink LED at specify time with Matlab - Arduino

90 ビュー (過去 30 日間)
melisa samad
melisa samad 2018 年 2 月 1 日
コメント済み: ishani uthpala 2021 年 3 月 10 日
Hi, I want to turn on LED for a given time such as 2 minutes and then it will turn off by using Matlab with Arduino hardware. I already search how to code it but I have no idea how to do it. The code below is my trial.
time = str2num(get(handles.edit3,'String'));
S = seconds(time);
if S > 0
writeDigitalPin(a, 'D13',1);
else
writeDigitalPin(a, 'D13', 0);
end
  2 件のコメント
Jan
Jan 2018 年 2 月 1 日
@melisa: Please mention, how this question is related to Matlab. How is the LED connected to Matlab? Without knowing this detail, an answer would required to guess.
melisa samad
melisa samad 2018 年 2 月 1 日
hi, thank you for replying and sorry for my mistake. I already edit my question.

サインインしてコメントする。

採用された回答

Jan
Jan 2018 年 2 月 4 日
編集済み: Jan 2018 年 2 月 4 日
time = str2num(get(handles.edit3,'String'));
% S = seconds(time); % [EDITED] Not needed
writeDigitalPin(a, 'D13', 1);
pause(time); % [EDITED] assumed, that edit3 contains the seconds
writeDigitalPin(a, 'D13', 0);
Or with a timer, which does not block Matlab:
TimerH = timer('TimerFcn', {TimerCB, 'off', a}, ...
'StartDelay', time, ... % [EDITED] S -> time
'ExecutionMode', 'SingleShot', ...
'StartFcn', {TimerCB, 'on', a});
start(TimerH);
which calls the Timer callback function at starting and stopping:
function TimerCB(TimerH, EventData, Cmd, a)
switch Cmd
case 'on'
writeDigitalPin(a, 'D13', 1);
case 'off'
writeDigitalPin(a, 'D13', 0);
end
end
  7 件のコメント
melisa samad
melisa samad 2018 年 2 月 5 日
okay, I will take note. Thank you
ishani uthpala
ishani uthpala 2021 年 3 月 10 日
thank you very much it's work

サインインしてコメントする。

その他の回答 (1 件)

Madhu Govindarajan
Madhu Govindarajan 2018 年 2 月 2 日
You can use tic and toc for this. When you run tic it starts a stopwatch style timer, you can then use toc to know the elapsed time and use a while loop to check if it is still less than your desired elapsed time.
  2 件のコメント
melisa samad
melisa samad 2018 年 2 月 4 日
how to done it?
Walter Roberson
Walter Roberson 2018 年 2 月 4 日
timelimit = 2*60;
writeDigitalPin(a, 'D13',1);
start = tic;
while toc(start) < timelimit
pause(0.01); %do _something_, even if just a useless computation
end
writeDigitalPin(a, 'D13', 0);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMATLAB Support Package for Arduino Hardware についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by