function hse_usd;
%Haynes-Schokley Experiment 
%(Section 1.4.4 in Understanding Semiconductor Devices)

%   Sima Dimitrijev, September 27, 1999.
%   Copyright (c) 2000, Oxford University Press.

global hsecon1_usd6; global hsecon3_usd6; global hsecon4_usd6;

%Open parent figure
Hf_hse=figure('NumberTitle','off','Name','Section 1.4.4: Haynes-Shockley Experiment',...
   'Color',[0.8 0.8 0.8]);
set(gcf,'InvertHardCopy','off');

Ha_hse=axes('Xlim',[0 10],'Ylim',[0 10],'Visible','off',...
                 'Position',[.02 .15 .7 .7]);
              
%Setting the intro text
Hframe_hse=uicontrol(Hf_hse,'Units','normalized','Position',[0.8 0.14 0.18 0.76],'Style','frame');
introtext_hse={'UNDERSTANDING SEMICONDUCTOR DEVICES ';...
           'Section 1.4.4: Haynes-Shockley Experiment' ;...
      ' ';...
      'This animation illustrates a particle flow due to drift ';...
      'and diffusion, separately from each other, ';...
      'as well as in a combined action, like in the  '; ... 
      'Haynes-Shockley experiment.'};             
Ht_hse=text(0.1,0.6,char(introtext_hse),'Units','normalized');

%CONSTANTS
st_hse_dr=0.25; st_hse_diff=0.05; %Drift/diffusion step size
hse_cen_0=1.25; mu_hse_0=0.2;
number_part_hse=250;

%Setting the buttons
Hb1_hse=uicontrol(Hf_hse,'Units','normalized','Position',[0.81 0.77 0.16 0.1],...
   'Style','pushbutton','String','DRIFT','CallBack','hsecon1_usd6=1;');
Hb2_hse=uicontrol(Hf_hse,'Units','normalized','Position',[0.81 0.62 0.16 0.1],...
   'Style','pushbutton','String','DIFFUSION','CallBack','hsecon1_usd6=2;');
Hb3_hse=uicontrol(Hf_hse,'Units','normalized','Position',[0.81 0.47 0.16 0.1],...
   'Style','pushbutton','String','BOTH','CallBack','hsecon1_usd6=3;');
Hbs_hse=uicontrol(Hf_hse,'Units','normalized','Position',[0.81 0.32 0.16 0.1],...
   'Style','pushbutton','String','STOP','CallBack','hsecon3_usd6=1;','Enable','off');
Hb4_hse=uicontrol(Hf_hse,'Units','normalized','Position',[0.81 0.17 0.16 0.1],...
   'Style','pushbutton','String','CLOSE','CallBack','hsecon4_usd6=1;');


%Define the silicon region
hold on;
si_hse_x=[0,10,10,0]; si_hse_y=[5.5,5.5,9.75,9.75]; 
Hph_hse1=patch(si_hse_x,si_hse_y,'w','EraseMode','normal','Visible','off'); %Defines Si region
m_hse_xl=[0,1,1,0]; m_hse_yl=[9.75,9.75,10,10]; 
Hph_hse2=patch(m_hse_xl,m_hse_yl,'k','EraseMode','normal','Visible','off'); %Defines masking oxide
m_hse_xr=[1.5,10,10,1.5]; m_hse_yr=[9.75,9.75,10,10]; 
Hph_hse3=patch(m_hse_xr,m_hse_yr,'k','EraseMode','normal','Visible','off'); 

%Generate initial set of particles, and set the particle plot ("Hp_particles")
hse_cen=hse_cen_0; mu_hse=mu_hse_0; %the center of the el. distribution
x_p_hse=randn(1,number_part_hse)*mu_hse+hse_cen;
y_p_hse=rand(1,number_part_hse)*4.25+5.5;
Hp_part_hse=plot(x_p_hse,y_p_hse,'k.','Markersize',6,'EraseMode','xor','Visible','off');
% Generate the initial distribution-plot
x_hse=[0:0.05:10]; y_hse=(0.83/mu_hse)*exp(-0.5*((x_hse-hse_cen)/mu_hse).^2)+5.5;
Hp_fun_hse=plot(x_hse,y_hse,'w-','LineWidth',2,'EraseMode','xor','Visible','off');
%Generate the i-t plot for the "BOTH" option
%%The axes
Hp_axes=plot([2 10 10],[0.2 0.2 4],'k-','Visible','off','EraseMode','normal','Visible','off');
%%The text
Htext_i=text(10.3,1.9,'CURRENT','Rotation',90,'Visible','off',...
   'Color','k','HorizontalAlignment','center','EraseMode','normal');
Htext_t=text(6,-0.1,'TIME','Visible','off',...
   'Color','k','HorizontalAlignment','center','EraseMode','normal');
i_data=[1:1]*0+0.25; t_data=[1:1]*10;
Hp_i_t=plot(t_data,i_data,'r-','LineWidth',2,'Visible','off','EraseMode','xor');
Hp_i_cont=plot([10 10],[5.5 9.8],'r-','LineWidth',5,'Visible','off','EraseMode','xor');

hold off

hsecon1_usd6=0; hsecon3_usd6=0; hsecon4_usd6=0;
while hsecon4_usd6==0 % Main loop
   
   while (hsecon1_usd6+hsecon4_usd6)<1; %Reading the intro text
      if ishandle(Hf_hse)==0;
         return; 
      end;
   pause(0.2);
   end
   
if hsecon1_usd6>0;   
      if ishandle(Hf_hse)==0;
         return; 
      end;
set(Ht_hse,'Visible','off');
set([Hph_hse1 Hph_hse2 Hph_hse3],'Visible','on');
%Generate initial set of particles, and set the particle plot ("Hp_particles")
hse_cen=hse_cen_0; mu_hse=mu_hse_0; %the center of the el. distribution
x_p_hse=randn(1,number_part_hse)*mu_hse+hse_cen;
y_p_hse=rand(1,number_part_hse)*4.25+5.5;
set(Hp_part_hse,'Xdata',x_p_hse,'Ydata',y_p_hse,'Visible','on');
% Generate the initial distribution-plot
x_hse=[0:0.05:10]; y_hse=(0.83/mu_hse)*exp(-0.5*((x_hse-hse_cen)/mu_hse).^2)+5.5;
set(Hp_fun_hse,'Xdata',x_hse,'Ydata',y_hse,'Visible','on');

if ishandle(Hf_hse)==0;
   return; 
end;
pause(1);
if ishandle(Hf_hse)==0;
   return; 
end;

switch hsecon1_usd6;
   
case 1; % Drift
   %Organize the buttons
   if ishandle(Hf_hse)==0;
      return; 
   end;
   set([Hb1_hse Hb2_hse Hb3_hse Hb4_hse],'Enable','off');
   set(Hbs_hse,'Enable','on');
   
   while hsecon3_usd6<1; %The animation loop 
   hse_cen=hse_cen+st_hse_dr;
   x_p_hse=randn(1,number_part_hse)*mu_hse+hse_cen;
   if ishandle(Hf_hse)==0;
      return; 
   end;
   set(Hp_part_hse,'XData',x_p_hse,'YData',y_p_hse);
   y_hse=(0.83/mu_hse)*exp(-0.5*((x_hse-hse_cen)/mu_hse).^2)+5.5;
   set(Hp_fun_hse,'YData',y_hse);
   
   tic;
   while toc<0.3
     pause(0.1)   
   end   
   
   if (hse_cen-3*mu_hse)>10;
      hsecon3_usd6=1; end
end
      if ishandle(Hf_hse)==0;
         return; 
      end;
set([Hb1_hse Hb2_hse Hb3_hse Hb4_hse],'Enable','on');
set(Hbs_hse,'Enable','off');

case 2; % Diffusion
%Organize the buttons
      if ishandle(Hf_hse)==0;
         return; 
      end;
set([Hb1_hse Hb2_hse Hb3_hse Hb4_hse],'Enable','off');
set(Hbs_hse,'Enable','on');

while hsecon3_usd6<1 %The animation loop
   mu_hse=mu_hse+st_hse_diff;
   x_p_hse=x_p_hse+randn(1,number_part_hse)*mu_hse;
   if ishandle(Hf_hse)==0;
      return; 
   end;
   set(Hp_part_hse,'XData',x_p_hse,'YData',y_p_hse)
   y_hse=(0.83/mu_hse)*exp(-0.5*((x_hse-hse_cen)/mu_hse).^2)+5.5;
   set(Hp_fun_hse,'YData',y_hse);
   
   tic;
   while toc<0.3
     pause(0.1)   
   end   
   if mu_hse>1.6 hsecon3_usd6=1; end
   end
      if ishandle(Hf_hse)==0;
         return; 
      end;
set([Hb1_hse Hb2_hse Hb3_hse Hb4_hse],'Enable','on');
set(Hbs_hse,'Enable','off');
  
case 3; %BOTH 
   if ishandle(Hf_hse)==0;
      return; 
   end;
set([Hb1_hse Hb2_hse Hb3_hse Hb4_hse],'Enable','off');
set(Hbs_hse,'Enable','on');

set([Hp_axes Htext_i Htext_t Hp_i_t Hp_i_cont],'Visible','on');

   N_st=15/st_hse_dr; st=1;
   while hsecon3_usd6<1;
      st=st+1;
      hse_cen=hse_cen+st_hse_dr; mu_hse=mu_hse+st_hse_diff;
      x_p_hse=randn(1,number_part_hse)*mu_hse+hse_cen;
      if ishandle(Hf_hse)==0;
         return; 
      end;
      set(Hp_part_hse,'XData',x_p_hse,'YData',y_p_hse)
      y_hse=(0.83/mu_hse)*exp(-0.5*((x_hse-hse_cen)/mu_hse).^2)+5.5;
      set(Hp_fun_hse,'YData',y_hse);
      
      t_data(st)=10-(hse_cen-hse_cen_0)*8/(15+st_hse_dr);
      i_data(st)=(3/mu_hse)*exp(-0.5*((10-hse_cen)/mu_hse).^2)+0.25;
      set(Hp_i_t,'Xdata',(10+t_data(st)-t_data),'Ydata',i_data);
      
      tic;
      while toc<0.3
         pause(0.1)   
      end   
   
      if hse_cen>16.25;
      while hsecon3_usd6<1;
         pause(1);
         if ishandle(Hf_hse)==0;
            return; 
         end;
      end;   
      end
    end
      if ishandle(Hf_hse)==0;
         return; 
      end;
set([Hb1_hse Hb2_hse Hb3_hse Hb4_hse],'Enable','on');
set(Hbs_hse,'Enable','off');

end % Switch
      if ishandle(Hf_hse)==0;
         return; 
      end;
set([Hph_hse1 Hph_hse2 Hph_hse3 Hp_part_hse Hp_fun_hse Hp_i_t Hp_i_cont],'Visible','off');
set([Hp_axes Htext_i Htext_t],'Visible','off');
set(Ht_hse,'Visible','on');
pause(0.1);


end %If hsecon1_usd6>0
hsecon1_usd6=0; hsecon3_usd6=0;
end % End main loop
close
