Matlab Arduino I have to turn five lights on with a 0.5 second delay between each one, then keep them all on for 2 seconds, then turn them all off and repeat this 10 times. How do I repeat this?

16 次查看(过去 30 天)
Cycle 1: Turn lights on with 0.5 second delay between each one. Then keep them on for 2 seconds,
Then turn them all off. Repeat this 10 times.
Cycle 2: Implement another lighting pattern with no delay and they all turn on at the same time. Hold them
on for one second. Then turn them all off. Repeat 10 times
I know how to turn the lights on/off and delay them, but I need help on how to repeat them and then clear cycle 1 to go to cycle 2.
This is my starting point
clear;clc
a= arduino;
for idx = 0:10
writeDigitalPin(a,'D8',1);
pause(0.5);
writeDigitalPin(a,'D8',1);
pause(0.5);
end

回答(1 个)

Prabhan Purwar
Prabhan Purwar 2021-3-19
Hi,
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.
Following is an example illustration
timelimit1 = (4*0.5+2)*10;
timelimit2 = 1*10;
start = tic;
while toc(start) < timelimit1
% Case 1
% Turn on one-by-one with 0.5 delay
writeDigitalPin(a,'D8',1);
pause(0.5);
writeDigitalPin(a,'D9',1);
pause(0.5);........
% Adopt for all 5 LED pins
pause(0.5);
% Turn all 5 lights on
writeDigitalPin(a,'D8',1);
writeDigitalPin(a,'D9',1);........
% Adopt for all 5 LED pins
pause(2);
%Put every pin to off state
end
start = tic;
while toc(start) < timelimit2
% Case 2
% Turn all 5 lights on
writeDigitalPin(a,'D8',1);
writeDigitalPin(a,'D9',1);........
% Adopt for all 5 LED pins
pause(1);
%Put every pin to off state
end
Thanks

类别

Help CenterFile Exchange 中查找有关 Arduino Hardware 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by