フィルターのクリア

How to on/off a variable after 3 seconds interval

13 ビュー (過去 30 日間)
Ismat
Ismat 2023 年 3 月 3 日
コメント済み: Ismat 2023 年 3 月 13 日
I have a small project with Raspberry pi.
I would like to turn the on/off the LED after every 3 seconds. if the time is 90 seconds then the simulation would stop.
Note: I would like to program it in Matlab first, then I will convert it into C++ which is suitable for Raspberry pi.
I am not sure about this below written systax. I can not count the 90 seconds. should I use a timer? Should I use Stateflow or Matlab scripts?
led1=0; % initially the LED is off
aa=repmat(3,1,30); % I dont know how to initilize this 3 seconds
% aa=3:3:90
for i=aa
pause(i) % wait for 3 seconds
led1=1; % after 3 seconds LED is On
pause(i) % wait for 3 seconds
led1=0; % then after 3 seconds LED is Off
% continue this process upto 90 seconds.
end

採用された回答

DUY Nguyen
DUY Nguyen 2023 年 3 月 3 日
編集済み: DUY Nguyen 2023 年 3 月 3 日
Hi ismat,
You could use timer for more precise control of the LED.
However, for simple application, you can use pause() in Matlab (or Sleep() in C++,). There will be a small milli-seconds of the interval depends the code runtime. Matlab scripts works well for this.
You can store your led_value in this variable led_results. You can look at the example code below in Matlab:
led1 = 0; % initially the LED is off
t = 0; % initialize time to zero
dt = 1; % time step size in seconds
stop_time = 9; % stop time in seconds
led_time=3;
% preallocate array to store LED results
led_results = zeros(1, stop_time/led_time);
% loop until stop time is reached
while t < stop_time
% wait for 3 seconds
pause(led_time)
% turn LED on
led1 = 1;
led_results(floor(t/led_time)+1) = led1; % store LED result
% wait for 3 seconds
pause(led_time)
% turn LED off
led1 = 0;
led_results(floor(t/led_time)+2) = led1; % store LED result
% increment time
t = t + 2*led_time;
end
% display LED results
disp(led_results)
  3 件のコメント
Ismat
Ismat 2023 年 3 月 3 日
possibly i solved it. just need a break statement
led1 = 0; % initially the LED is off
t = 0; % initialize time to zero
dt = 1; % time step size in seconds
stop_time = 9; % stop time in seconds
led_time=3;
% preallocate array to store LED results
led_results = zeros(1, stop_time/led_time);
% loop until stop time is reached
while t < stop_time
% wait for 3 seconds
pause(led_time)
% turn LED on
led1 = 1;
led_results(floor(t/led_time)+1) = led1; % store LED result
if t==stop_time-3
break
end
% wait for 3 seconds
pause(led_time)
% turn LED off
led1 = 0;
led_results(floor(t/led_time)+2) = led1; % store LED result
% increment time
t = t + 2*led_time;
end
% display LED results
disp(led_results)
DUY Nguyen
DUY Nguyen 2023 年 3 月 3 日
yes, that works well!

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

その他の回答 (2 件)

VBBV
VBBV 2023 年 3 月 3 日
編集済み: VBBV 2023 年 3 月 3 日
led1=0; 
aa=3;
tic
T = toc
while T<=90
    pause(3);
    led1=1; 
    pause(3);
    led1=0;  toc;
T = toc;
end
%disp("The time lapsed is"+toc)
You can modify as above
  4 件のコメント
Ismat
Ismat 2023 年 3 月 3 日
I executed the code. It could not show the expected output.
when T=9, output would be 1 0 1
when T=12, output would be 1 0 1 0
VBBV
VBBV 2023 年 3 月 3 日
編集済み: VBBV 2023 年 3 月 3 日
Below is the modified code, which works according to the output you need
clear all % very important to run this line, otherwise displays incorrect results
led1 = 0;
aa=3;
tic
T = toc;
i = 1;
while T<90
pause(3)
led1 = 1;
T = floor(toc);
disp("The time lapsed is " + T + "seconds")
L(i) = led1;
disp("The timer ON/OFF sequence is ")
L
pause(3)
led1 = 0;
T = floor(toc);
disp("The time lapsed is " + T + "seconds")
L(i+1) = led1;
disp("The timer ON/OFF sequence is ")
L
i = i+2;
T = floor(toc);
end
>> The time lapsed is 3seconds
The timer ON/OFF sequence is
L =
1
The time lapsed is 6seconds
The timer ON/OFF sequence is
L =
1 0
The time lapsed is 9seconds
The timer ON/OFF sequence is
L =
1 0 1
The time lapsed is 12seconds
The timer ON/OFF sequence is
L =
1 0 1 0

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


Prasanth Sunkara
Prasanth Sunkara 2023 年 3 月 10 日
編集済み: Prasanth Sunkara 2023 年 3 月 10 日
Hi Ismat,
Simulink is an ideal platform for your project. Simulink offers time based simulations and you can generate C/C++ code easily from the model and deploy it to Raspberry Pi. Please feel free to check out the Simulink Support package for Raspberry Pi Hardware.
Also check out this basic LED example. You can chage the pulse duration and duty cycle to meet your use case. https://mathworks.com/help/supportpkg/raspberrypi/ref/getting-started-with-raspberry-pi-hardware.html
Thanks,
-Prasanth
  1 件のコメント
Ismat
Ismat 2023 年 3 月 13 日
Hello Prasanth
thank you very much for your comment. I am aware of the tool Simulink Support package for Raspberry Pi Hardware. But I guess it's not free.
Hence I am looking for an alternative way to write the code in Matlab/Simulink.

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

カテゴリ

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

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by