Hello everyone!
I encounter a problem when I use Arduino to measure impulsive voltage combine with Matlab.
The following is my code of hardware in Arduino, it can successfully run. Please see the attached file-"sketch_dec22b"
int analogPin = 5;
int val = 0;
void setup()
{
//Serial.begin(115200);
Serial.begin(9600);
}
void loop()
{
val = analogRead(analogPin);
Serial.println(val);//将传感器读取到温度值通过串口发送给LabVIEW
delay(20);
}
The following is my Matlab program (GUI, Please see the attached file-"New sample GUI") which is also can run, but the output figure can not correctly demonstrate the voltage variance. Please see the attached figure, named' GUIshow when the Matlab program is runing'.__
function varargout = light_degree_sensor3(varargin)
% LIGHT_DEGREE_SENSOR3 MATLAB code for light_degree_sensor3.fig
% LIGHT_DEGREE_SENSOR3, by itself, creates a new LIGHT_DEGREE_SENSOR3 or raises the existing
% singleton*.
%
% H = LIGHT_DEGREE_SENSOR3 returns the handle to a new LIGHT_DEGREE_SENSOR3 or the handle to
% the existing singleton*.
%
% LIGHT_DEGREE_SENSOR3('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in LIGHT_DEGREE_SENSOR3.M with the given input arguments.
%
% LIGHT_DEGREE_SENSOR3('Property','Value',...) creates a new LIGHT_DEGREE_SENSOR3 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before light_degree_sensor3_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to light_degree_sensor3_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help light_degree_sensor3
% Last Modified by GUIDE v2.5 22-Dec-2020 21:26:21
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @light_degree_sensor3_OpeningFcn, ...
'gui_OutputFcn', @light_degree_sensor3_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before light_degree_sensor3 is made visible.
function light_degree_sensor3_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 light_degree_sensor3 (see VARARGIN)
% Choose default command line output for light_degree_sensor3
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes light_degree_sensor3 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = light_degree_sensor3_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
global mycom;
myComPort = serial('COM3');
fopen(myComPort);
% now disconnect the device
delete(myComPort)
clear myComPort
% now connect the device again, the following will now be successful:
% myComPort = serial('COM3');
% fopen(myComPort);
% instrfind
s = serial('COM3'); %定义串口对象
set(s,'BaudRate',9600); %设置波特率s9600
% set(s,'BaudRate',115200);%设置波特率s115200是没有问题的
fopen(s); %打开串口对象s
mycom=s;
%% 把这些值传给下一个
handles.mycom=mycom;%传值出去
guidata(hObject,handles);%这一句必须加上
% fclose(mycom);%退出程序前,串口一定要关闭,否则端口占用,必须重新启动计算机
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
%%取出传值数据
mycom=handles.mycom;%%取出传值数据
fclose(mycom);%退出程序前,串口一定要关闭,否则端口占用,必须重新启动计算机
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
mycom=handles.mycom;%%取出传值数据
interval = 5000; %这里开始的代码很Part1里的代码类似
passo = 1;
t = 1;
x = 0;
while(t<interval)
b = str2num(fgetl(mycom)); %用函数fget(s)从缓冲区读取串口数据,当出现终止符(换行符)停止。
x = [x,b]; %所以在Arduino程序里要使用Serial.println()
plot(x);
grid
t = t+passo;
drawnow;
end
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
sketch_dec22b.ino (302 Bytes)
New_sample_GUI.zip (25.1 KB)






