Display continuous alert in GUI from serial data

조회 수: 5 (최근 30일)
Iemad Sofi
Iemad Sofi 2019년 11월 13일
댓글: Iemad Sofi 2019년 11월 14일
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
Iemad Sofi
Iemad Sofi 2019년 11월 13일
i tried using for and it worked. Just had to put long duration (3600 or more for longer duration)
global a k;
for k=1:1:3600
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
pause(1); %or as appropriate
end
Walter Roberson
Walter Roberson 2019년 11월 13일
Don't put the loop in the open fcn callback.

댓글을 달려면 로그인하십시오.

채택된 답변

Guillermo Rubio Gomez
Guillermo Rubio Gomez 2019년 11월 13일
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.
  댓글 수: 1
Iemad Sofi
Iemad Sofi 2019년 11월 14일
thankyou so much. i appreciate your help. ill try it

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 I2C Devices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by