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

44 views (last 30 days)
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

Answers (1)

Walter Roberson
Walter Roberson on 28 Sep 2022
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

Community Treasure Hunt

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

Start Hunting!