I keep getting this error...

I am new to Matlab, and I am trying to create a blinking LED program using an arduino. After I have finished setting up my arduino and my program. I kept encountering the same error and I don't know how to fix it. The error saids, "MATLAB connection to Uno at COM3 exists in your workspace. To create a new connection, clear the existing object".
The following is my program:
a=arduino;
for i=1:10
writeDigitalPin(a,'D13',1);
pause(0.5);
writeDigitalPin(a,'D13',0);
pause(0.5);
end
disp('Done');

 Respuesta aceptada

Chris
Chris el 17 de Sept. de 2022
Editada: Chris el 17 de Sept. de 2022

2 votos

In your workspace, does a already exist, or some other arduino object?
if so, type clear a (or the other relevant object) in the command window. You could put that at the end of your program to try and avoid these errors in the future.
Or, as many do, simply put clear; close all at the beginning of your programs to clean the slate at the start.

2 comentarios

Abrahim
Abrahim el 17 de Sept. de 2022
Thanks a lot, the clear; workout. Apparently i did have a in my workspace.
Chris
Chris el 17 de Sept. de 2022
You're welcome.

Iniciar sesión para comentar.

Más respuestas (1)

哲史 伊藤
哲史 伊藤 el 1 de Mayo de 2023

0 votos

I also have the same problem in R2021b and in my case the program still reporting the error even I delete using
clear
delete(instrfindall)
fclose( serial(a.Port) );
delete( serial(a.Port) );
clear a
clearvars -global DIN a
my code is following. It works fine for the first time but secondly the error occurs.
error: arduino_cycle_DIN (line 9)
MATLAB connection to Uno at COM3 exists in your workspace. To create a new connection, clear the existing object.
function arduino_cycle_DIN
delete(instrfindall)
clear
global DIN
DIN=nan(100,1);
a=arduino("COM3","Uno");
t=timer;
t.Period=0.01;
t.TimerFcn={@readDIN,a};
t.TasksToExecute=100;
t.ExecutionMode="fixedRate";
start(t)
while t.TasksExecuted<100
DIN(100)
end
delete(t)
fclose( serial(a.Port) );
delete( serial(a.Port) );
clear a
clearvars -global DIN a
function readDIN(obj,event,a)
global DIN
dat=readDigitalPin(a,"D2");
% 1
DIN(2:100)=DIN(1:99);
DIN(100)=dat;

3 comentarios

Chris
Chris el 1 de Mayo de 2023
@哲史 伊藤 This works fine as a standalone function for me. However, if I create a connection with the arduino in the base workspace, and then call the function, I get the error.
Normally, a variable created inside a particular workspace or function is scoped to that workspace--not accessible from another function unless passed into the function as an argument. However, since only one connection to the arduino is allowed, it would appear these connections--when made from the base workspace--are sort of global in scope.
If you type clear in the command window before calling the function, does that fix your problem?
哲史 伊藤
哲史 伊藤 el 1 de Mayo de 2023
Thanks Chris,
I always type
clear
before calling the function, but still get the error.
Tetsufumi
Chris
Chris el 2 de Mayo de 2023
Hi Tetsufumi,
I am stumped--but I don't use the arduino very much.
You may want to create a new question so more people will see it. If that doesn't work, you can submit a support request with Mathworks.
Best of luck.
Chris

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB Support Package for Arduino Hardware en Centro de ayuda y File Exchange.

Productos

Versión

R2022b

Etiquetas

Preguntada:

el 17 de Sept. de 2022

Comentada:

el 2 de Mayo de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by