Clear Filters
Clear Filters

Undefined function error for readDistance function within GUI editor

2 views (last 30 days)
I am using the function readDistance() with an Arduino and HC - SR04 ultrasonic sensor. I have tested it in the command line to ensure my equipment is working and successfully received the distance value. However, for my assignment, I need to create a GUI and get the data there. When I try to use it in the GUI editor, I get the following error: 'Undefined function 'readDistance' for input arguments of type 'double'. '
I am using it in this context:
function CollectDataButtonPushed(app, event)
data = zeros(1e4,1);
t = zeros(1e4,1);
i = 1;
tic
while toc < 10
value = readDistance(app.us);
data(i) = value;
data(i) = data(i) * 100; %convert from m to cm
t(i) = toc;
i = i + 1;
end
plot(app.UIAxes, data);
%writetable(T,filename);
end
I have declared and set up my Arduino and sensor like this:
properties (Access = private)
a % arduino
us % ultrasound sensor
end
app.a = arduino('COM4', 'Uno', "Libraries", "Ultrasonic");
app.us = app.a.ultrasonic('D7','D6');
I have also tried to use the function like the following, but I get an error that says 'Dot indexing is not supported for variables of this type.'
value = app.us.readDistance();
I have tried searching for this error, but all I can find are the pages on how to use the readDistance() function which don't offer specific help.

Accepted Answer

Marisa Assink
Marisa Assink on 21 Oct 2023
I was able to figure it out, it was a problem with my ISP (Xfinity). Works fine with no edits on different networks, or at home with the wifi off on my laptop.

More Answers (1)

Yash Sharma
Yash Sharma on 18 Oct 2023
Hi Marisa,
I understand that you are unable to use the function “readDistance()” with an Arduino and HC - SR04 ultrasonic sensor, I believe that you have defined the ultrasonic object in an incorrect way, to define it correctly you should use the following syntax:
app.us = ultrasonic(app.a, D7, D6).
In this code snippet, app.arepresents the arduino object, 'D7' is the trigger pin, and 'D6' is the echo pin.
Please find the documentation of the “ultrasonic” function below:
Hope this helps!

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!