function diff_usd;
%The concept of diffusion 
%(Section 1.3.1 in "Understanding Semiconductor Devices")

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

global whcon1_usd1; global whcon2_usd1; global whcon3_usd1; global whcon4_usd1;

%Open parent figure
Hf_diffanim=figure('NumberTitle','off','Name','Section 1.3.1: The concept of diffusion',...
   'Color',[0.8 0.8 0.8]);
set(gcf,'InvertHardCopy','off');

Ha_diffanim=axes('Xlim',[0 10],'Ylim',[-5 5],'Visible','off',...
               'Position',[.02 .25 .7 .7]);
              
Hf_clue=figure('NumberTitle','off','Name','Here it is!',...
   'Units','normalized','Position',[0.3 0.35 0.4 0.3],'Color','w',...
   'MenuBar','none');
Ha_clue=axes('Xlim',[0 10],'Ylim',[0 10],'Visible','off',...
                 'Position',[0 0 1 1]);
close(Hf_clue);              
              
%Setting the intro text
Hframe_diaffanim=uicontrol(Hf_diffanim,'Units','normalized','Position',[0.8 0.25 0.18 0.70],'Style','frame');
introtext={'UNDERSTANDING SEMICONDUCTOR DEVICES ';...
           'Section 1.3.1: The Concept of Diffusion' ;...
      ' ';...
      'The animation in this example is created by generating ';...
      'a random set of particles above semiconductor substrate, ';...
      'and allowing every particle to move in a '; ... 
      'PURELY RANDOM DIRECTION at every animation step.';...
      ' ';...
      'Although individual particles move with equal chances ';...
      'in any direction, overall, the particles move into ';...
      'the semiconductor.';...
      ' ';...
      'WHY?'};             
Ht_diffanim=text(0.1,0.5,char(introtext),'Units','normalized');

%Setting the buttons
Hb1_diffanim=uicontrol(Hf_diffanim,'Units','normalized','Position',[0.81 0.83 0.16 0.1],...
   'Style','pushbutton','String','START','CallBack','whcon1_usd1=1;');
Hb2_diffanim=uicontrol(Hf_diffanim,'Units','normalized','Position',[0.81 0.68 0.16 0.1],...
   'Style','pushbutton','Enable','off','String','STOP','CallBack','whcon2_usd1=1;');
Hb3_diffanim=uicontrol(Hf_diffanim,'Units','normalized','Position',[0.81 0.53 0.16 0.1],...
   'Style','pushbutton','String','EXIT','CallBack','whcon3_usd1=1;');
Hb4_diffanim=uicontrol(Hf_diffanim,'Units','normalized','Position',[0.81 0.28 0.16 0.1],...
   'Style','pushbutton','Enable','off','String','CLUE?','CallBack','whcon4_usd1=1;');

whcon1_usd1=0; whcon2_usd1=0; whcon3_usd1=0; whcon4_usd1=0;
while whcon3_usd1<1 % Main loop 
   
    while (whcon1_usd1+whcon3_usd1)<1;   
       if ishandle(Hf_diffanim)==0;
          if ishandle(Hf_clue)==1; close(Hf_clue); end;
       return; end
    pause(0.2);
    if ishandle(Hf_diffanim)==0;
                 if ishandle(Hf_clue)==1; close(Hf_clue); end;
       return; end
    end

if whcon1_usd1>0; 
whcon1_usd1=0;
      if ishandle(Hf_diffanim)==0;
             if ishandle(Hf_clue)==1; close(Hf_clue); end;
          return; 
       end
 set(Ht_diffanim,'Visible','off')

%Define the material regions
hold on
si_x=[0,10,10,0]; si_y=[-5,-5,0,0]; Hph_diff1=patch(si_x,si_y,'y'); %Defines Si region
oxide_xl=[0,3,3,0]; oxide_yl=[0,0,2.5,2.5]; Hph_diff2=patch(oxide_xl,oxide_yl,'b'); %Defines masking oxide
oxide_xr=[7,10,10,7]; oxide_yr=[0,0,2.5,2.5]; Hph_diff3=patch(oxide_xr,oxide_yr,'b'); 

%Accompanying text
acctext_diffan={'Individual particles move with equal chances in any direction. ';...
                'Overall, the particles flow into the semiconductor. ';...
                'WHY? '};             
Hacctext_diffan=uicontrol(Hf_diffanim,'Units','normalized','Position',[0.00 0.0 1 0.2],...
   'Style','text','String',acctext_diffan,'FontSize',14,'BackgroundColor',[0.76 0.76 0.76]);

%Generate initial set of particles, and set the particle plot ("Hp_particles")
number_particles=1000;
part_x=1:number_particles;
part_y=1:number_particles;
for parind=1:number_particles
   ch1w=0; 
   while ch1w<1
      xtr=rand(1,1)*10; ytr=rand(1,1)*10-5;
   if ytr<0; ch=0;
   elseif ytr>2.5; ch1w=1;
   elseif xtr<3; ch1w=0;
   elseif xtr>7; ch1w=0;
   else ch1w=1;
   end
   end
   part_x(parind)=xtr;
   part_y(parind)=ytr;
end
Hp_particles=plot(part_x,part_y,'k.','Markersize',20,'EraseMode','normal');
%particles=plot(part_x,part_y,'k.','Markersize',20,'EraseMode','xor');
hold off
     if ishandle(Hf_diffanim)==0;
             if ishandle(Hf_clue)==1; close(Hf_clue); end;
          return; 
       end
pause(1);

%Change the particle position
st1=0.5; %Step size
       if ishandle(Hf_diffanim)==0;
             if ishandle(Hf_clue)==1; close(Hf_clue); end;
          return; 
       end
set([Hb1_diffanim Hb3_diffanim],'Enable','off');
set([Hb2_diffanim Hb4_diffanim],'Enable','on');
while whcon2_usd1<1
   if ishandle(Hf_diffanim)==0;
                if ishandle(Hf_clue)==1; close(Hf_clue); end;
   return;
   else   
   part_x=part_x+(rand(1,number_particles)-0.5)*st1;
   part_y=part_y+(rand(1,number_particles)-0.5)*st1;
          if ishandle(Hf_diffanim)==0;
                if ishandle(Hf_clue)==1; close(Hf_clue); end;
          return; 
          end
   set(Hp_particles,'XData',part_x,'YData',part_y)
      if whcon4_usd1>0; 
         clue; 
         whcon4_usd1=0; 
      end 
      if ishandle(Hf_diffanim)==0;
                   if ishandle(Hf_clue)==1; close(Hf_clue); end;
          return; 
      end
pause(1);   
   end;
end
if ishandle(Hf_diffanim==1);
whcon2_usd1=0;
set([Hb1_diffanim Hb3_diffanim],'Enable','on');
set([Hb2_diffanim Hb4_diffanim],'Enable','off');
set(Hp_particles,'Visible','off');
set(Ht_diffanim,'Visible','on');
set(Hph_diff1,'Visible','off'); set(Hph_diff2,'Visible','off'); 
set(Hph_diff3,'Visible','off');
set(Hacctext_diffan,'Visible','off');
end;

end % If whcon1_usd1>0
      if ishandle(Hf_diffanim)==0;
             if ishandle(Hf_clue)==1; close(Hf_clue); end;
          return; 
       end;
       
drawnow;       
end; % End main loop
close;

function clue;
Hf_clue=figure('NumberTitle','off','Name','Here it is!',...
   'Units','normalized','Position',[0.3 0.35 0.4 0.3],'Color','w',...
   'MenuBar','none');
Ha_clue=axes('Xlim',[0 10],'Ylim',[0 10],'Visible','off',...
                 'Position',[0 0 1 1]);
hold on;
Hph_clue1=patch([0 10 10 0],[0 0 5 5],'y'); %Defines Si region
Hph_clue2=patch([0 10 10 0],[5 5 10 10],[0.8 0.8 0.8]); %Defines air region
x_clue=[0.5 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 0.5 1.5 2.5 3.5];
y_clue=[7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 2.5 2.5 12.5 12.5 12.5 12.5];
Hp_clue=plot(x_clue,y_clue,'k.','Markersize',20,'EraseMode','background');
Ht_eqc(1)=text(4.5,6,'0.5*8=4','HorizontalAlignment','left','FontAngle','italic',...
   'FontSize',12);
Ht_eqc(2)=text(8,4,'0.5*2=1','HorizontalAlignment','right','FontAngle','italic',...
   'FontSize',12);
hold off;
Nsteps=100;
delta_y=5/Nsteps;
for ii=1:Nsteps;
   if ishandle(Hf_clue)==1;
   y_clue(1:4)=y_clue(1:4)-delta_y;
   y_clue(5:8)=y_clue(5:8)+delta_y;
   y_clue(9)=y_clue(9)+delta_y;
   y_clue(10)=y_clue(10)-delta_y;
   y_clue(11:14)=y_clue(11:14)-delta_y;
   set(Hp_clue,'YData',y_clue);
   tic;
   while toc<0.05;
      pause(0.01);
   end   
   end
end 
if ishandle(Hf_clue)==1;
set(Hph_clue1,'XData',[0 10 10 0]);
set(Hph_clue2,'XData',[0 10 10 0]);
pause(3);
if ishandle(Hf_clue)==1;
close(Hf_clue);
end;
end;

