%ESTE PROGRAMA POSEE UNA INTERFAZ GRAFICA REALIZADA EN MATLAB, QUE PERMITE EL CONTROL DE
%ENCENDIDO Y APAGADO DE 4 LEDSFIG QUE ESTAN CONECTADOS A PINES DIGITALES DE LA
%TARJETA DE DESARROLLO ARDUINO UNO R3.


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

% Last Modified by GUIDE v2.5 21-Jun-2019 15:58:24

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ledsfig_OpeningFcn, ...
                   'gui_OutputFcn',  @ledsfig_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 ledsfig is made visible.
function ledsfig_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 ledsfig (see VARARGIN)

%ESTAS LINEAS DE CODIGO SON LAS QUE PERMITEN INTRODUCIR UNA IMAGEN 
%DENTRO DE UNA INTERFAZ GRAFICA
axes(handles.axes5);
background = imread('ab.png');
axis off;
imshow(background);

%CREACION DEL OBJETO ARDUINO PARA OBTENER LA CONECCION ENTRE ARDUINO Y
%MATLAB
a = arduino('COM30');
a.pinMode(5, 'output');
a.pinMode(6, 'output');
a.pinMode(7, 'output');
a.pinMode(13, 'output');
handles.a = a;

% Choose default command line output for ledsfig
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes ledsfig wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = ledsfig_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 btn_led_1.
function btn_led_1_Callback(hObject, eventdata, handles)
% hObject    handle to btn_led_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of btn_led_1
button_state = get(hObject, 'value');

if button_state == get(hObject, 'Max');
    set(handles.txt_led1, 'BackgroundColor', 'g');
    set(handles.txt_led1,'String', 'ON' );    
    handles.a.digitalWrite(5,1);
   
    
else button_state == get(hObject,'Min');
    set(handles.txt_led1, 'BackgroundColor', [1 0 0]);
    set(handles.txt_led1,'String', 'OFF' );
     handles.a.digitalWrite(5,0);
end





% --- Executes on button press in btn_led_2.
function btn_led_2_Callback(hObject, eventdata, handles)
% hObject    handle to btn_led_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of btn_led_2
button_state = get(hObject, 'value');

if button_state == get(hObject, 'Max');
    set(handles.txt_led2, 'BackgroundColor', 'g');
    set(handles.txt_led2,'String', 'ON' );    
    handles.a.digitalWrite(6,1);
   
    
else button_state == get(hObject,'Min');
    set(handles.txt_led2, 'BackgroundColor', [1 0 0]);
    set(handles.txt_led2,'String', 'OFF' );
     handles.a.digitalWrite(6,0);
end


% --- Executes on button press in btn_led_3.
function btn_led_3_Callback(hObject, eventdata, handles)
% hObject    handle to btn_led_3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of btn_led_3
button_state = get(hObject, 'value');

if button_state == get(hObject, 'Max');
    set(handles.txt_led3, 'BackgroundColor', 'g');
    set(handles.txt_led3,'String', 'ON' );    
   handles.a.digitalWrite(7,1);
   
    
else button_state == get(hObject,'Min');
    set(handles.txt_led3, 'BackgroundColor', [1 0 0]);
    set(handles.txt_led3,'String', 'OFF' );
    handles.a.digitalWrite(7,0);
end


% --- Executes on button press in btn_led_4.
function btn_led_4_Callback(hObject, eventdata, handles)
% hObject    handle to btn_led_4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of btn_led_4
button_state = get(hObject, 'value');

if button_state == get(hObject, 'Max');
  set(handles.txt_led4, 'BackgroundColor', 'g');
    set(handles.txt_led4,'String', 'ON');    
    handles.a.digitalWrite(13,1);
   
      
else button_state == get(hObject,'Min');
    set(handles.txt_led4, 'BackgroundColor', [1 0 0]);
    set(handles.txt_led4,'String', 'OFF' );
     handles.a.digitalWrite(13,0);
end



% --- Executes on key press with focus on btn_led_1 and none of its controls.
function btn_led_1_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to btn_led_1 (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)



% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton1



function txt_led1_Callback(hObject, eventdata, handles)
% hObject    handle to txt_led1 (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 txt_led1 as text
%        str2double(get(hObject,'String')) returns contents of txt_led1 as a double




% --- Executes during object creation, after setting all properties.
function txt_led1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txt_led1 (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 txt_led2_Callback(hObject, eventdata, handles)
% hObject    handle to txt_led2 (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 txt_led2 as text
%        str2double(get(hObject,'String')) returns contents of txt_led2 as a double


% --- Executes during object creation, after setting all properties.
function txt_led2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txt_led2 (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 txt_led3_Callback(hObject, eventdata, handles)
% hObject    handle to txt_led3 (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 txt_led3 as text
%        str2double(get(hObject,'String')) returns contents of txt_led3 as a double


% --- Executes during object creation, after setting all properties.
function txt_led3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txt_led3 (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 txt_led4_Callback(hObject, eventdata, handles)
% hObject    handle to txt_led4 (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 txt_led4 as text
%        str2double(get(hObject,'String')) returns contents of txt_led4 as a double


% --- Executes during object creation, after setting all properties.
function txt_led4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txt_led4 (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 mouse press over axes background.
function axes3_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to axes3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on mouse press over axes background.
function axes5_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to axes5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
