How can I read values from an ultrasonic sensor connected to an Arduino in Matlab app designer?
22 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Mmakgabo Masipa
el 28 de Sept. de 2022
Respondida: MathWorks MATLAB Hardware Team
el 30 de Mayo de 2024
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)
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
0 comentarios
Respuestas (3)
Walter Roberson
el 28 de Sept. de 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
xochitl berenice
el 29 de Mayo de 2024
Hello, I have the same problem right now. Could you solve it?
0 comentarios
MathWorks MATLAB Hardware Team
el 30 de Mayo de 2024
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
0 comentarios
Ver también
Categorías
Más información sobre MATLAB Support Package for Arduino Hardware en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!