# 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?

1 view (last 30 days)
Zachary Schmitt on 12 Mar 2021
Answered: Prabhan Purwar on 19 Mar 2021
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

Prabhan Purwar on 19 Mar 2021
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