MATLAB BLINK 2 LEDS AT THE SAMETIme

2 views (last 30 days)
I been trying to blink 2 leds at the same time with different frecuencies, but the pause() function will block matlab and won't let any other code work during that stop time, here is my code
function blinkled4_Callback(hObject, eventdata, handles) % The blind on this led its acitivate by a button nadem blinkled4
% hObject handle to blinkled4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.a.configurePin('D4','DigitalOutput');
status1 =get(handles.blinkled4,'string');
if strcmp(status1,'Turn ON')==1
set(handles.blinkled4,'string','Turn OFF');
set(handles.blinkled4,'BackgroundColor','green');
for i = 1:10
handles.a.writeDigitalPin('D4', 1);
pause(0.5);
handles.a.writeDigitalPin('D4', 0);
pause(0.5);
end
else
handles.a.writeDigitalPin('D4',0)
set(handles.blinkled4,'string','Turn ON');
set(handles.blinkled4,'BackgroundColor','red');
end
and here is the other code for my other led
function blinkled5_Callback(hObject, eventdata, handles)
% hObject handle to blinkled5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.a.configurePin('D5','DigitalOutput');
status =get(handles.blinkled5,'string');
if strcmp(status,'Turn ON')==1
set(handles.blinkled4,'string','Turn OFF');
set(handles.blinkled4,'BackgroundColor','green');
for i = 1:10
handles.a.writeDigitalPin('D5', 1);
pause(0.5);
handles.a.writeDigitalPin('D5', 0);
pause(0.5);
end
else
handles.a.writeDigitalPin('D5',0)
set(handles.blinkled4,'string','Turn ON');
set(handles.blinkled4,'BackgroundColor','red');
Any ideas? please help

Accepted Answer

Mark Sherstan
Mark Sherstan on 12 Dec 2018
Edited: Mark Sherstan on 12 Dec 2018
I havent used the MATLAB/Arduino toolbox to much but I modified one of my old Arduino sketches that should help. The key is to use a timer instead of the pause function and check how much time has elapsed and base your on/off from that. Look at my example below and let me know if you have any questions.
% Initialize timer and LED state
previousTime = 0;
ledState = false;
% Start timer
tic
% Loop for 20 seconds
while toc < 20
currentTime = toc;
if (currentTime - previousTime >= 2)
previousTime = currentTime;
if (ledState == false)
ledState = true;
else
ledState = false;
end
end
% WRITE TO ARDUINO - I think this would work for you assuming it is initialized...
handles.a.writeDigitalPin('D5', ledState);
end
This isent a direct solution to the functions you posted but you could implment this in your main script and call your sub functions with only a slight modification.
  2 Comments
Vincent Valenzuela
Vincent Valenzuela on 12 Dec 2018
i have tried implementing your code, but it won't let me blink de 2 leds a the same time, first will blink 1 led, and when its done blinking, then will blink the second led.
Mark Sherstan
Mark Sherstan on 12 Dec 2018
Here is a more developed script for you, the LED's can be thought of as the output from fprintf being either 1 or 0 / on or off / true or false.
% Initialize timer and LED state
previousTime1 = 0;
previousTime2 = 0;
ledState1 = true;
ledState2 = true;
% Start timer
tic
% Loop for 10 seconds
while toc < 10
currentTime = toc;
if (currentTime - previousTime1 >= 1)
previousTime1 = currentTime;
if (ledState1 == false)
ledState1 = true;
else
ledState1 = false;
end
end
if (currentTime - previousTime2 >= 2)
previousTime2 = currentTime;
if (ledState2 == false)
ledState2 = true;
else
ledState2 = false;
end
end
fprintf('%0.0f ',ledState1)
fprintf('%0.0f\n',ledState2)
end

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!