How do i make MATLAB remember moments in the code?

2 次查看(过去 30 天)
Hi!
This is my GUI code:
The problem i got is when im press pushbutton1_Callback the function is running this:
a=arduino('COM4');
a.pinMode(9, 'OUTPUT');
a.digitalWrite(9, 1);
msgbox('ON');
a=arduini('COM4') means that the program connecting to the Arduino UNO card. a.pinMode(9, 'OUTPUT') means that the program declare that pin 9 is an output for current. a.digitalWrite(9, 1) means that the LED lamp is ON. msgbox('ON') means that after the program has turn on the LED lamp, a message bos will show up.
My problem is when the message box shows up, the LED lamp turn OFF. My program cannot remember the moments it has read.
If i place this code
a=arduino('COM4');
a.pinMode(9, 'OUTPUT');
in the first function above of the code below and remove the same code in the push button functions
function pushbutton2_Callback(hObject, eventdata, handles)
a.digitalWrite(9, 0);
msgbox('OFF');
i got error if a press any function push button. The error is "I don't know what a or a.digitalWrite are??". Just because the program don't remember that it has run this code before
a=arduino('COM4');
a.pinMode(9, 'OUTPUT');
This is my whole code:
function varargout = First(varargin)
% FIRST MATLAB code for First.fig
% FIRST, by itself, creates a new FIRST or raises the existing
% singleton*.
%
% H = FIRST returns the handle to a new FIRST or the handle to
% the existing singleton*.
%
% FIRST('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FIRST.M with the given input arguments.
%
% FIRST('Property','Value',...) creates a new FIRST or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before First_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to First_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 First
% Last Modified by GUIDE v2.5 19-Jul-2014 22:43:46
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @First_OpeningFcn, ...
'gui_OutputFcn', @First_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 First is made visible.
function First_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 First (see VARARGIN)
% Choose default command line output for First
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes First wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = First_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)
a=arduino('COM4');
a.pinMode(9, 'OUTPUT');
a.digitalWrite(9, 1);
msgbox('ON');
% 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)
a=arduino('COM4');
a.pinMode(9, 'OUTPUT');
a.digitalWrite(9, 0);
msgbox('OFF');
% 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)

回答(1 个)

Image Analyst
Image Analyst 2014-7-20
You probably have the LED reversed. Try turning it around. Have the + side go into pin 9, the - side go into the breadboard and a resistor going from the breadboard to ground.

类别

Help CenterFile Exchange 中查找有关 Arduino Hardware 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by