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

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

% Update handles structure
guidata(hObject, handles);
%initialize_gui(hObject, handles, false);
% UIWAIT makes serial_reader wait for user response (see UIRESUME)
% uiwait(handles.figure1);


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

if get(handles.pushbutton_run,'UserData')==1
    return;
end
set(handles.pushbutton_run,'UserData',1);


disp('RUN')

waittime=0.001; % Zeit zum Scannen der Antwort der Channels  DRINGEND NÖTIG!!!


s1 = serial('COM3');    % define serial port COM3 am PC
s1.BaudRate=9600;               % define baud rate
set(s1, 'terminator', 'LF');    % define the terminator for println CR(10) or LF(13)

fopen(s1);   % Öffnet die Verbindung zum Arduino

try                             % use try catch to ensure fclose
                                % signal the arduino to start collection
w=fscanf(s1,'%s');
                      % must define the input % d or %s, etc.
if (w=='X')
    fprintf(s1,'%s\n','X');     % establishContact just wants something to buffer
    pause(0.3);
    display(['Collecting data...']);
    
end

x = get(handles.pushbutton_run,'UserData');


while x==1;
    
    
    
    fprintf(s1, '%s','A')
    pause(waittime);
    a0=fscanf(s1,'%d');   
    a0=(5*a0/1023);
    a0=num2str(a0);
    set(handles.ausgabe0,'String',a0);
    
    
    
    fprintf(s1, '%s','B')
    pause(waittime);
    a1=fscanf(s1,'%d');
    a1=(5/1023)*a1;
    a1=num2str(a1);
    set(handles.ausgabe1,'String',a1);
    
    %Temperatur
    fprintf(s1, '%s','C')
    pause(waittime);
    a2=fscanf(s1,'%d');
    a2=(5/1023)*a2;
    Tfahr= ((1/298)+(1/3988)*log(a2/(5-a2))) ^(-1)*1.8-460;
    Tcelsius = (Tfahr-32)*5/9;
    Tcelsius = round(Tcelsius);
    a2=num2str(Tcelsius);
    set(handles.ausgabe2,'String',a2);
    
    
    fprintf(s1, '%s','D')
    pause(waittime);
    a3=fscanf(s1,'%d');
    a3=(5/1023)*a3;
    a3=num2str(a3);
    set(handles.ausgabe3,'String',a3);
    
    
    fprintf(s1, '%s','E')
    pause(waittime);
    a4=fscanf(s1,'%d');
    a4=(5/1023)*a4;
    a4=num2str(a4);
    set(handles.ausgabe4,'String',a4);
    
    
    fprintf(s1, '%s','F')
    pause(waittime);
    a5=fscanf(s1,'%d');
    a5=(5/1023)*a5;
    a5=num2str(a5);
    set(handles.ausgabe5,'String',a5);
    
    x = get(handles.pushbutton_run,'UserData');
        
end

    set(handles.ausgabe0,'String','0');
    set(handles.ausgabe1,'String','0');
    set(handles.ausgabe2,'String','0');
    set(handles.ausgabe3,'String','0');
    set(handles.ausgabe4,'String','0');
    set(handles.ausgabe5,'String','0');
 

fclose(s1); % Beenden der Arduino-Verbindung
             
catch exception % sollte keine Verbindung zustande kommen.....
    fclose(s1);
    delete(s1);
    clear s1;
    %freeserial;
end


% --- Executes on button press in pushbutton_stop.
function pushbutton_stop_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_stop (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.pushbutton_run,'UserData',0);
disp('STOP')



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


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


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


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


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


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


% --- Executes during object creation, after setting all properties.
function ausgabe5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ausgabe5 (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 during object creation, after setting all properties.
function axes6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
axes(hObject);
imshow('logo_sunrace.jpg');  % WARUM WIRD DAS BILD NICHT MEHR ANGEZEIGT?
guidata(hObject,handles)
% Hint: place code in OpeningFcn to populate axes6


% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject    handle to slider2 (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
x = get(hObject,'Value');

%x = get(handles.slider2,'Value')

handles.wert.slider2 = x;

guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end



% --- Executes during object creation, after setting all properties.
function axes7_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
axes('units',get(gca,'units'),'position',get(gca,'position'),'color','none','visible','off')
axis([-1 1 0 1])
axis equal
h = line(NaN,NaN,'linewidth',3,'Color',[1 0 0]);



for i=110 % HIER SOLLTE DER SLIDER WERT HIN !!!!!!!!!!!!!!!
   set(h,'xdata',[0 cos(i*pi/180)],'ydata',[0 sin(i*pi/180)])
   drawnow
   pause(0.01)
end

guidata(hObject,handles)
% Hint: place code in OpeningFcn to populate axes7


