Display continuous alert in GUI from serial data
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Iemad Sofi
le 13 Nov 2019
Commenté : Iemad Sofi
le 14 Nov 2019
Hello guys. I need help to continuously display alert in edit box based on digital serial data recieved, 0 or 1. For now, i only get to read from serial port once so the alerts wont change based on the serial data.
Can anyone show me the coding to add below to continuously display alert based on serial data i recieve from arduino? How should i use loop in this?
Thankyou in advance :)
% --- Executes just before level_simulation is made visible.
function level_simulation_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to level_simulation (see VARARGIN)
% Choose default command line output for level_simulation
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
delete(instrfind({'PORT'},{'COM3'}));
clear a;
clc;
global a;
a = arduino('com3','uno');
x = 0;
global a;
reading = readDigitalPin(a,'D3');
set(handles.edit1,'String',num2str(reading)); % edit1 is the tag of edit box used to display the temperature
if reading == 1
set(handles.edit2,'String','FIRST ALERT'); % edit2 is the tag of edit box used to display the alert
set(handles.edit3,'String','');
elseif reading == 0
set(handles.edit3,'String','SECOND ALERT');
set(handles.edit2,'String','');
else
set(handles.edit2,'String','');
set(handles.edit3,'String','');
end
4 commentaires
Réponse acceptée
Guillermo Rubio Gomez
le 13 Nov 2019
The problem is that you are doing what you need just once, when the GUI starts, since your code is placed only in the OpeningFcn
You can not use a
while true
end
loop to place your code in since it will keep that code executing perpetually, preventing GUIDE to refresh.
I think that you need to declare a timer object in your OpeningFcn, with a period of 1s for example, or a rate that allows you to continuosly read the data while allowing GUI to refresh. Then you have to link a function with the timer, the function is responsible for receive data from the serial port and display the alert in GUI. The code could be like this:
% --- Executes just before level_simulation is made visible.
function level_simulation_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to level_simulation (see VARARGIN)
% Choose default command line output for level_simulation
handles.output = hObject;
delete(instrfind({'PORT'},{'COM3'}));
clear a;
clc;
global a;
a = arduino('com3','uno');
x = 0;
global a;
%% here you create your timer object, a period of 1s could work for what you need
handles.timer1 = timer(...
'ExecutionMode', 'fixedRate', ... % Continuous execution
'Period', 1, ... % 1s Period
'TimerFcn', {@update_display,hObject}); % Function executed in each timer execution
%% Lets start the timer, so it will call the timerFcn every period time
start(handles.timer1)
% Now you have to update the handles structure after defining the timer
guidata(hObject, handles);
reading = readDigitalPin(a,'D3');
set(handles.edit1,'String',num2str(reading)); % edit1 is the tag of edit box used to display the temperature
if reading == 1
set(handles.edit2,'String','FIRST ALERT'); % edit2 is the tag of edit box used to display the alert
set(handles.edit3,'String','');
elseif reading == 0
set(handles.edit3,'String','SECOND ALERT');
set(handles.edit2,'String','');
else
set(handles.edit2,'String','');
set(handles.edit3,'String','');
end
% Then, in the callback section, you add the function linked to the timer object:
function update_display(hObject,eventdata,data)
% Function that executes on each timer period
% Obtain handles data strcuture
handles = guidata(data);
% Now you add the code you need to execute perdiodically
reading = readDigitalPin(a,'D3');
set(handles.edit1,'String',num2str(reading)); % edit1 is the tag of edit box used to display the temperature
if reading == 1
set(handles.edit2,'String','FIRST ALERT'); % edit2 is the tag of edit box used to display the alert
set(handles.edit3,'String','');
elseif reading == 0
set(handles.edit3,'String','SECOND ALERT');
set(handles.edit2,'String','');
else
set(handles.edit2,'String','');
set(handles.edit3,'String','');
end
% finally update the handles structure
guidata(data,handles);
I think that should work.
Additionally, I would add the "a" variable to the handles structure, instead of declaring it as a global variable. Also, you can use the stop a start functions of the timer for example to use a button to start and stop the data refreshing.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur MATLAB Support Package for Arduino Hardware dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!