Turning on and off leds with Arduino with parallel loops
8 次查看(过去 30 天)
显示 更早的评论
Hi everyone,
I am trying to turn on and off leds randomly but independently. I think it might be doable with parallel loops, but I get an error saying "Arduino objects cannot be saved to disk, and will be skipped." Is it possible to do what I am trying?
a = arduino();
t1 = parfevalOnAll(backgroundPool, @f1, 1, a, 'D7')
t2 = parfevalOnAll(backgroundPool, @f1, 1, a, 'D4')
v1 = fetchOutputs(t1);
v2 = fetchOutputs(t2);
clear
function [tx, ty] = f1(a, pin)
tx = zeros(10, 1);
ty = zeros(10, 1);
for i = 1:10
writeDigitalPin(a, pin, 1);
x = rand(1);
tx(i) = x;
pause(x)
writeDigitalPin(a, pin, 0);
y = 2 * rand(1);
ty(i) = y;
pause(y)
end
end
0 个评论
回答(2 个)
Taylor
2024-9-25
The issue you're encountering is related to the fact that arduino objects are not serializable and cannot be passed directly to parallel workers because they cannot be saved to disk. When using parallel processing in MATLAB, each worker operates in its own separate process, and objects like arduino cannot be directly shared across these processes.
If your goal is to toggle each LED randomly and independently you could try just creating the random order of pins and delay times before entering the loop, and then just loop through them. In the code below, you would just replace the display commands with your function to toggle the specified pin on/off.
pins = "D" + (1:10)
randIdx = randperm(length(pins));
pauseTimes = rand([1 10]);
for ii = randIdx
disp(pins(ii))
disp(pauseTimes(ii))
end
0 个评论
MathWorks MATLAB Hardware Team
2024-9-27
Hi,
MATLAB Support Package for Arduino Hardware doesn't support parallel executions. Please feel free to refer following link for more info,
https://in.mathworks.com/help/matlab/supportpkg/getting-started-with-matlab-support-package-for-arduino-hardware.html
Thanks
MATLAB Hardware Team
MathWorks
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Modeling 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!