Turning on and off leds with Arduino with parallel loops
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Alfredo Arévalo
el 24 de Sept. de 2024
Respondida: MathWorks MATLAB Hardware Team
el 27 de Sept. de 2024
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 comentarios
Respuestas (2)
Taylor
el 25 de Sept. de 2024
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 comentarios
MathWorks MATLAB Hardware Team
el 27 de Sept. de 2024
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 comentarios
Ver también
Categorías
Más información sobre Modeling en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!