How can I read values from an ultrasonic sensor connected to an Arduino in Matlab app designer?
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;
endThe 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 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 matlab app designer, ultrasonic sensor, arduino MATLAB Answers — New Questions