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?
    조회 수: 10 (최근 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
댓글 수: 0
답변 (1개)
  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
댓글 수: 0
참고 항목
카테고리
				Help Center 및 File Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!