How can I read values from an ultrasonic sensor connected to an Arduino in Matlab app designer?

25 次查看(过去 30 天)
The problem is mainly getting the measured distance and display as I want. I do not get any errors when ruuning the app but no distance is shown on the editable field. Below is some of the code I have used for the button as well as the editable numeric field. I believe there is something I am missing and I cannot figure it out, please let me know of any suggestion I can try out to get this right.
properties (Access = private)
Unrecognized function or variable 'private'.
a
u
dist
end
I have declared the above properties which I will be using in the code to further try and attain the measured distance and display it in an editable numeric field
function startupFcn(app)
app.a = arduino;
app.u = ultrasonic(app.a,'d50','d51');
end
Above is the start up function whereby I have assignedd values to the variables I declared earlier on. Where app.a= arduino gets all the required properties and makes the neccessary connections and settings to allow me to communicate with the arduino board. app.u = .... lets me define the pins on the arduino board which will be used as triger and echo pins.
function ButtonPushed(app, event)
% app.u = ultrasonic(app.a,'D50','D51');
app.dist = readDistance(app.u); % determing the initial distance
while (app.dist > 0.050) % this loop ensures that the sensor continues to
% "measure" distance for as long as the object is further than 5cm
app.dist = readDistance(app.u);
end
end
Below is the last piece of code which I am using to try and display the measured distance
function EditFieldValueChanged(app, event)
app.EditField.Value = app.dist;
end

回答(3 个)

Walter Roberson
Walter Roberson 2022-9-28
EditFieldValueChanged is only invoked when the user edits the field. You need to update the field as you store new app.distance values. You might need a
drawnow limitrate
to allow the display to update

xochitl berenice
xochitl berenice 2024-5-29
Hello, I have the same problem right now. Could you solve it?

MathWorks MATLAB Hardware Team
Hi
Thanks for reaching out to us.
Based on your description, it seems like you want to display the distance measurement in an edit field upon clicking a push button. If that's the case, I suggest trying out the following code snippet within your ButtonPushed callback function:
function ButtonPushed(app, event)
% app.u = ultrasonic(app.a,'D50','D51');
app.dist = readDistance(app.u); % Determine the initial distance
app.EditField.Value = app.dist; % Update the Edit field
while (app.dist > 0.050) % Continue measuring distance as long as the object is more than 5cm away
app.dist = readDistance(app.u);
end
end
Please feel free to contact us if you need any further assistance,
https://in.mathworks.com/support/contact_us.html
Best Regards,
MATLAB Hardware Team
MathWorks

类别

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