MATLAB-Arduino Serial Communication Available Feedback

조회 수: 2 (최근 30일)
Eren GÜVEN
Eren GÜVEN 2021년 8월 8일
편집: Eren GÜVEN 2021년 8월 14일
Hello, I am connecting to Arduino on MATLAB App Designer. ( port=arduino('COM7','Uno','Libraries','Servo'); ) I want to get feedback when it is connected to Arduino so when the connection is complete. For example, when the connection is complete, the Lamp should turn from red to green. How can I do it ?
Thanks...

채택된 답변

Shivam Ahuja
Shivam Ahuja 2021년 8월 12일
It is my understanding that you want to establish a connection and you want to get the feedback whether the connection is successful or not. The following solution will help you to achieve this:
1. Create a private property or member variable for App class to denote a connection flag and set it once Arduino object creation is complete.
properties (Access = private)
IsConnected = false % Initially set to false to denote no connection
end
2. Create the Arduino object in try/catch block and perform digital operation in try/catch itself. If something is wrong with the connection, the Arduino object creation itself will error out and will go to the catch statement and the digital operation will double check the Arduino object connection.
function ConnectButtonPushed(app, event)
try
a = arduino;
app.IsConnected = true;
%digital operation
catch
app.IsConnected = false;
%digital operation
end
end
Refer to the MATLAB App Designer documentation for more information on how to use properties in app class.

추가 답변 (1개)

Eren GÜVEN
Eren GÜVEN 2021년 8월 14일
편집: Eren GÜVEN 2021년 8월 14일
Thank you so much my friend. The project is running. Here is the project :
function startupFcn(app)
clear all;
delete(instrfindall);
end
function ConnectButtonButtonPushed(app, event)
global port;
global s;
port=arduino('COM7','Uno','Libraries','Servo');
s=servo(port,'D3');
try
app.IsConnected = true;
app.ActivityLamp.Color='green';
%digital operation
catch
app.IsConnected = false;
app.ActivityLamp.Color='red';
%digital operation
end
end
function DegreeKnobValueChanging(app, event)
global port;
global s;
changingValue = event.Value;
degree=changingValue/180;
writePosition(s,degree);
end
properties (Access = private)
IsConnected = false
end
function CloseButtonPushed(app, event)
global port;
clear port;
app.delete;
clear all;
delete(instrfindall);
end

카테고리

Help CenterFile 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!

Translated by