matlab guide read from Arduino real Time

7 views (last 30 days)
Itziar Uzqueda
Itziar Uzqueda on 2 May 2017
Answered: Leon Bouman on 12 Apr 2019
Hi!
I'm trying to use Matlab Guide to collect data from an Arduino UNO and sent over Bluetooth. I want the sampling time to be a fixed value(for example 60ms). I can receive data but not at that rate. The first 100 samples more or less go so quickly and the others are not responding in real time because the Arduino has an ultrasonic sensor and when I move sth closer, it responds a few seconds later.
The code and the Guide interface are the following:
% --- Executes on button press in Medir.
function Medir_Callback(hObject, eventdata, handles)
% hObject handle to Medir (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.Abrir,'Enable','off');
set(handles.Medir,'Enable','off');
set(handles.guardar,'Enable','off');
set(handles.pushbutton5,'Enable','off');
set(handles.stop,'Enable','on');
global stop
stop=false;
global stop2;
stop2=false;
cla reset; %Limpiar la grafica cada vez que se hace click
handles.h=animatedline('Marker','none','LineWidth',1,'Color','b');
guidata(hObject,handles);
xlabel('Number of samples','FontSize',16,'FontWeight','bold','Color','w');
ylabel('Distance (cm)','FontSize',16,'FontWeight','bold','Color','w');
set(gca, 'XColor', [1 1 1]);
set(gca, 'YColor', [1 1 1]);
i=1;
while (stop==false) && (stop2==false)
handles.start=tic;
handles.distancia(i)=fscanf(handles.obj1,'%d');
addpoints(handles.h,i,handles.distancia(i));
drawnow;
handles.tiempo2(i)= toc(handles.start);
handles.retardo(i) = handles.interval-handles.tiempo2(i);
pause(handles.retardo(i));
i=i+1;
%handles.interval is 0.06sec in this case
end
if(stop==true)
stop=false;
set(handles.guardar,'Enable','on');
set(handles.Abrir,'Enable','on');
set(handles.Medir,'Enable','on');
guidata(hObject,handles);
cerrar_puerto(handles.obj1);
end
if(stop2==true)
stop2=false;
cerrar_puerto(handles.obj1);
guidata(hObject,handles);
delete(gcf);
end
  1 Comment
Kevin Gleason
Kevin Gleason on 5 May 2017
The issue might be that you are reading only 1 integer at a time in fscanf. There may be a backup of values before you are reading the "close to sensor" values. You can try to clear the buffer on each read, maybe take a mean of the values recorded over the last second:
Instead of "handles.distancia(i)=fscanf(handles.obj1,'%d');" Try:
vals=double(fscanf(handles.obj1,'%c'));
handles.distancia(i)=mean(vals);
"vals" will be an array of all the values waiting in the data buffer, which will clear it for the next reading, depending on how frequently the arduino is sending data.

Sign in to comment.

Answers (1)

Leon Bouman
Leon Bouman on 12 Apr 2019
Hello Itziar Uzqueda, i am looking trough your posts and i am seeing that you had a similar kind of project that i am doing now. I want to readout data from a Arduino with Matlab using a HC-05 Bluetooth module. Would you be so kind and send me your Matlab scripts and Simulink models of this project so that i can use them as an example for my project. If you want, you can send them to: "leon_bouman_9@live.nl".
Thank you!!
Léon

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!