0% found this document useful (0 votes)
51 views9 pages

Function: 'Gui - Name' 'Gui - Singleton' 'Gui - Openingfcn' 'Gui - Outputfcn' 'Gui - Layoutfcn' 'Gui - Callback'

The document describes code for an Arduino GUI created in MATLAB. It contains functions for initializing the GUI, opening and closing the GUI window, and callback functions for buttons and edit boxes that get or set string values. The code sets up the serial connection to the Arduino board and loads an ultrasonic sensor library when the "Setup" button is pressed. It also contains the main loop function for a traffic light simulation that sequences the light colors over time based on a distance reading from the sensor.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views9 pages

Function: 'Gui - Name' 'Gui - Singleton' 'Gui - Openingfcn' 'Gui - Outputfcn' 'Gui - Layoutfcn' 'Gui - Callback'

The document describes code for an Arduino GUI created in MATLAB. It contains functions for initializing the GUI, opening and closing the GUI window, and callback functions for buttons and edit boxes that get or set string values. The code sets up the serial connection to the Arduino board and loads an ultrasonic sensor library when the "Setup" button is pressed. It also contains the main loop function for a traffic light simulation that sequences the light colors over time based on a distance reading from the sensor.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

function varargout = arduino_1(varargin)

% ARDUINO_1 MATLAB code for arduino_1.fig


% ARDUINO_1, by itself, creates a new ARDUINO_1 or raises the
existing
% singleton*.
%
% H = ARDUINO_1 returns the handle to a new ARDUINO_1 or the handle
to
% the existing singleton*.
%
% ARDUINO_1('CALLBACK',hObject,eventData,handles,...) calls the
local
% function named CALLBACK in ARDUINO_1.M with the given input
arguments.
%
% ARDUINO_1('Property','Value',...) creates a new ARDUINO_1 or
raises the
% existing singleton*. Starting from the left, property value pairs
are
% applied to the GUI before arduino_1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property
application
% stop. All inputs are passed to arduino_1_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 arduino_1

% Last Modified by GUIDE v2.5 13-Dec-2017 18:17:46

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @arduino_1_OpeningFcn, ...
'gui_OutputFcn', @arduino_1_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 arduino_1 is made visible.


function arduino_1_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 arduino_1 (see VARARGIN)

% Choose default command line output for arduino_1


handles.output = hObject;
handles.s=1;
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes arduino_1 wait for user response (see UIRESUME)


% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = arduino_1_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)
% 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)

function edit1_Callback(hObject, eventdata, handles)


% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text


% str2double(get(hObject,'String')) returns contents of edit1 as a
double

% --- Executes during object creation, after setting all properties.


function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns
called

% Hint: edit controls usually have a white background on Windows.


% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in Setup.


function Setup_Callback(hObject, eventdata, handles)
global a
set(handles.text4,'String','Espere...')
pause(0.0001)
if isempty (a)
try
puerto=get(handles.edit1,'String');
placa=get(handles.edit2,'String');
a=arduino(puerto,placa,'Libraries','JRodrigoTech/HCSR04');
set(handles.text4,'String','Listo')
catch
set(handles.text4,'String','error')
end
else
set(handles.text4,'String','Listo')
end

guidata(hObject, handles);

% hObject handle to Setup (see GCBO)


% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

function edit2_Callback(hObject, eventdata, handles)


% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text


% str2double(get(hObject,'String')) returns contents of edit2 as a
double

% --- Executes during object creation, after setting all properties.


function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns
called

% Hint: edit controls usually have a white background on Windows.


% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function edit3_Callback(hObject, eventdata, handles)


% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit3 as text


% str2double(get(hObject,'String')) returns contents of edit3 as a
double

% --- Executes during object creation, after setting all properties.


function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns
called

% Hint: edit controls usually have a white background on Windows.


% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function edit4_Callback(hObject, eventdata, handles)


% hObject handle to edit4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit4 as text


% str2double(get(hObject,'String')) returns contents of edit4 as a
double

% --- Executes during object creation, after setting all properties.


function edit4_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns
called

% Hint: edit controls usually have a white background on Windows.


% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% 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 BotonInicio.


function BotonInicio_Callback(hObject, eventdata, handles)
global a
sensor = addon(a, 'JRodrigoTech/HCSR04', 'D12', 'D13');
dmax=str2double(get(handles.edit12,'String'));
secuencia=0;
rojo=0;
s=1;

axes(handles.axes1);
vid=videoinput('winvideo',2,'MJPG_800x600');
himage=image(zeros(800,600,3),'parent', handles.axes1);
preview(vid,himage);

%Semaforo 1
writeDigitalPin(a,'D2',0) %rojo
set(handles.rs1,'BackgroundColor','black')
writeDigitalPin(a,'D3',0) %amarillo
set(handles.as1,'BackgroundColor','black')
writeDigitalPin(a,'D4',0) %verde
set(handles.vs1,'BackgroundColor','black')
%Semaforo 2
writeDigitalPin(a,'D5',0) %rojo
set(handles.rs2,'BackgroundColor','black')
writeDigitalPin(a,'D6',0) %amarillo
set(handles.as2,'BackgroundColor','black')
writeDigitalPin(a,'D7',0) %verde
set(handles.vs2,'BackgroundColor','black')

set(handles.BotonParar,'UserData',0)
tic
while(1)
if get(handles.BotonParar,'UserData')==1
break
end

if secuencia==8
if s==1
s=0;
else
s=1;
end
secuencia=0;
tic
end

if rojo==1
set(handles.distancia,'String',readDistance(sensor))
if readDistance(sensor)<dmax
set(handles.multa,'BackgroundColor','red')
foto=getsnapshot(vid);
imwrite(foto,'multa.png')
else
set(handles.multa,'BackgroundColor','green')
end
end

if toc<10 && secuencia==0


if s==1
writeDigitalPin(a,'D2',1) %rojo S1
set(handles.rs1,'BackgroundColor','red')
writeDigitalPin(a,'D7',1) %verde S2
set(handles.vs2,'BackgroundColor','green')
rojo=1;
else
writeDigitalPin(a,'D5',1) %rojo S2
set(handles.rs2,'BackgroundColor','red')
writeDigitalPin(a,'D4',1) %verde S1
set(handles.vs1,'BackgroundColor','green')
end
secuencia=1;
elseif toc>=10 && toc<10.5 && secuencia==1
if s==1
writeDigitalPin(a,'D7',0) %verde S2
set(handles.vs2,'BackgroundColor','black')
else
writeDigitalPin(a,'D4',0) %verde S1
set(handles.vs1,'BackgroundColor','black')
end
secuencia=2;
elseif toc>=10.5 && toc<11 && secuencia==2
if s==1
writeDigitalPin(a,'D7',1) %verde S2
set(handles.vs2,'BackgroundColor','green')
else
writeDigitalPin(a,'D4',1) %verde S1
set(handles.vs1,'BackgroundColor','green')
end
secuencia=3;
elseif toc>=11 && toc<11.5 && secuencia==3
if s==1
writeDigitalPin(a,'D7',0) %verde S2
set(handles.vs2,'BackgroundColor','black')
else
writeDigitalPin(a,'D4',0) %verde S1
set(handles.vs1,'BackgroundColor','black')
end
secuencia=4;
elseif toc>=11.5 && toc<12 && secuencia==4
if s==1
writeDigitalPin(a,'D7',1) %verde S2
set(handles.vs2,'BackgroundColor','green')
else
writeDigitalPin(a,'D4',1) %verde S1
set(handles.vs1,'BackgroundColor','green')
end
secuencia=5;
elseif toc>=12 && toc<12.5 && secuencia==5
if s==1
writeDigitalPin(a,'D7',0) %verde S2
set(handles.vs2,'BackgroundColor','black')
else
writeDigitalPin(a,'D4',0) %verde S1
set(handles.vs1,'BackgroundColor','black')
end
secuencia=6;
elseif toc>=12.5 && toc<15.5 && secuencia==6
if s==1
writeDigitalPin(a,'D6',1) %amarillo S2
set(handles.as2,'BackgroundColor','yellow')
writeDigitalPin(a,'D7',0) %verde S2
set(handles.vs2,'BackgroundColor','black')
else
writeDigitalPin(a,'D3',1) %amarillo S1
set(handles.as1,'BackgroundColor','yellow')
writeDigitalPin(a,'D4',0) %verde S1
set(handles.vs1,'BackgroundColor','black')
end
secuencia=7;
elseif toc>=15.5 && secuencia==7
writeDigitalPin(a,'D6',0) %amarillo S2
set(handles.as2,'BackgroundColor','black')
writeDigitalPin(a,'D3',0) %amarillo S1
set(handles.as1,'BackgroundColor','black')
writeDigitalPin(a,'D2',0) %rojo S1
set(handles.rs1,'BackgroundColor','black')
writeDigitalPin(a,'D5',0) %rojo S2
set(handles.rs2,'BackgroundColor','black')
secuencia=8;
rojo=0;
end

pause(0.001)
end

% hObject handle to BotonInicio (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 BotonParar.


function BotonParar_Callback(hObject, eventdata, handles)
global a
set(handles.BotonParar,'UserData',1)
%Semaforo 1
writeDigitalPin(a,'D2',0) %rojo
set(handles.rs1,'BackgroundColor','black')
writeDigitalPin(a,'D3',0) %amarillo
set(handles.as1,'BackgroundColor','black')
writeDigitalPin(a,'D4',0) %verde
set(handles.vs1,'BackgroundColor','black')
%Semaforo 2
writeDigitalPin(a,'D5',0) %rojo
set(handles.rs2,'BackgroundColor','black')
writeDigitalPin(a,'D6',0) %amarillo
set(handles.as2,'BackgroundColor','black')
writeDigitalPin(a,'D7',0) %verde
set(handles.vs2,'BackgroundColor','black')

% hObject handle to BotonParar (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 rs1.


function rs1_Callback(hObject, eventdata, handles)
% hObject handle to rs1 (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 as1.


function as1_Callback(hObject, eventdata, handles)
% hObject handle to as1 (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 vs1.


function vs1_Callback(hObject, eventdata, handles)
% hObject handle to vs1 (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 rs2.


function rs2_Callback(hObject, eventdata, handles)
% hObject handle to rs2 (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 as2.


function as2_Callback(hObject, eventdata, handles)
% hObject handle to as2 (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 vs2.
function vs2_Callback(hObject, eventdata, handles)
% hObject handle to vs2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

function edit12_Callback(hObject, eventdata, handles)


% hObject handle to edit12 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit12 as text


% str2double(get(hObject,'String')) returns contents of edit12 as
a double

% --- Executes during object creation, after setting all properties.


function edit12_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit12 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns
called

% Hint: edit controls usually have a white background on Windows.


% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in pushbutton15.


function pushbutton15_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton15 (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 multa.


function multa_Callback(hObject, eventdata, handles)
% hObject handle to multa (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

You might also like