function rseb_usd;
%Drift Current in Energy-Band Presentation 
%(Section 1.5.3 in Understanding Semiconductor Devices)

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

global debcon1_usd8; global debcon2_usd8; global debcon3_usd8; global debcon4_usd8; 
global debcon5_usd8;

%Open parent figure
Hf_deb=figure('NumberTitle','off','Name','Section 1.5.3: Drift Current in Energy-Band Presentation',...
   'Color',[0.8 0.8 0.8]);
set(gcf,'InvertHardCopy','off');

Ha_deb=axes('Xlim',[0 10],'Ylim',[0 10],'Visible','off',...
                 'Position',[.02 0 .7 1]);
              
%Setting the intro text
Hframe_deb=uicontrol(Hf_deb,'Units','normalized','Position',[0.8 0.2 0.18 0.75],'Style','frame');
introtext_deb={'UNDERSTANDING SEMICONDUCTOR DEVICES ';...
           'Section 1.5.3: Drift Current in Energy-Band Presentation' ;...
      ' ';...
      'This animation is an energy-band presentation of  ';...
      'electron and hole drift through a biased ';...
      'resistor. You can set the bias voltage and  '; ... 
      'follow the "random" motion of an electron or hole.'; ...
      'Note the effects of the bias voltage.'};             
Ht_deb=text(0.1,0.6,char(introtext_deb),'Units','normalized');

%Setting the buttons
%the slider
Hb1_dbe=uicontrol(Hf_deb,'Units','normalized','Position',[0.81 0.83 0.16 0.05],...
   'Style','slider','min',0,'max',5,'Value',3,'CallBack','debcon1_usd8=1;');
Hb1l_dbe=uicontrol(Hf_deb,'Units','normalized','Position',[0.81 0.90 0.03 0.03],...
   'Style','text','String','0 V');
Hb1r_dbe=uicontrol(Hf_deb,'Units','normalized','Position',[0.94 0.90 0.03 0.03],...
   'Style','text','String','5 V');
Hb1c_dbe=uicontrol(Hf_deb,'Units','normalized','Position',[0.86 0.90 0.06 0.03],...
   'Style','text','String',num2str(round(get(Hb1_dbe,'Value'))));
%The radiobuttons
Hb21_dbe=uicontrol(Hf_deb,'Units','normalized','Position',[0.81 0.73 0.16 0.05],...
   'Style','radio','String','Electron','CallBack','debcon2_usd8=1;','Value',1);
Hb22_dbe=uicontrol(Hf_deb,'Units','normalized','Position',[0.81 0.68 0.16 0.05],...
   'Style','radio','String','Hole','CallBack','debcon2_usd8=2;','Value',0);
%The pushbuttons
Hb3_deb=uicontrol(Hf_deb,'Units','normalized','Position',[0.81 0.53 0.16 0.1],...
   'Style','pushbutton','String','START','CallBack','debcon3_usd8=1;');
Hbs_deb=uicontrol(Hf_deb,'Units','normalized','Position',[0.81 0.38 0.16 0.1],...
   'Style','pushbutton','String','STOP','CallBack','debcon5_usd8=1;','Enable','off');
Hb4_deb=uicontrol(Hf_deb,'Units','normalized','Position',[0.81 0.23 0.16 0.1],...
   'Style','pushbutton','String','CLOSE','CallBack','debcon4_usd8=1;');

%CONSTANTS
Eg_dbe=1.2; Ew=1.9; %Vmax+Eg+2*Ew should be 10
st_deb=0.05; lscav=0.5; lscmu=0.1; kt_deb=0.5*lscav;

%Set the energy band handles
hold on;
x=[0 10 10 0]; y=[5 5 10 10]; %x are real, but y are dummy numbers
Hph_dbec=patch(x,y,'g','Visible','off'); %Ec
y=[0 0 5 5]; % dummy numbers
Hph_dbev=patch(x,y,'r','Visible','off'); %Ev
%Set the electron plot 
xel(1)=1; 
Hpel_dbe=plot(xel,6,'k.','Markersize',6,'Visible','off','EraseMode','xor');
Hpel_tr=plot(xel,6,'k-','Visible','off','EraseMode','none');
%Set the hole plot 
xho(1)=9;
Hpho_dbe=plot(xho,4,'w.','Markersize',12,'Visible','off','EraseMode','xor');
Hpho_tr=plot(xel,6,'w-','Visible','off','EraseMode','none');
hold off;

debcon1_usd8=1; debcon2_usd8=0; debcon3_usd8=0; debcon4_usd8=0; debcon5_usd8=0;
debcon22_usd8=1; %Sets the radiobutton value to electron
while debcon4_usd8<1 % Main loop
   xel=1; xho=9;
   while (debcon3_usd8+debcon4_usd8)<1;
   %Set the slider value      
   if debcon1_usd8>0;  
       if ishandle(Hf_deb)==0;
       return; end;
      set(Hb1c_dbe,'String',num2str(round(get(Hb1_dbe,'Value'))));
   end;   
   
   %Select electron/hole on the radio buttons
   if debcon2_usd8>0;
      if debcon2_usd8==1;
       debcon22_usd8=1;
       if ishandle(Hf_deb)==0;
       return; end;
       set(Hb21_dbe,'Value',1); set(Hb22_dbe,'Value',0); 
      else 
       debcon22_usd8=0;   
       if ishandle(Hf_deb)==0;
       return; end;
       set(Hb21_dbe,'Value',0); set(Hb22_dbe,'Value',1); 
      end
   end
   debcon2_usd8=0;
       if ishandle(Hf_deb)==0;
       return; end;
   pause(0.2);
   end   
   
   debcon1_usd8=1; %So that the matrices are initialized later
   if debcon4_usd8==0 %EXIT not activated, debcon3_usd8 must be 1;
       if ishandle(Hf_deb)==0;
       return; end;
   set(Ht_deb,'Visible','off');    
   set([Hb21_dbe Hb22_dbe Hb3_deb Hb4_deb],'Enable','off');
   set(Hbs_deb,'Enable','on');
   n_sc_st=0; %Number of scattering steps performed
   while debcon5_usd8<1; %Loop for scattering steps
      %Enable change of bands during animation
      if debcon1_usd8>0; %First go, or the slider is moved, initialize the graphs
      debcon1_usd8=0;   
      if n_sc_st>0;  
         if debcon22_usd8>0;
            xend=xel(n_sc_st);
            xel=[1:1]*0;
            xel(1)=xend; 
         else 
            xend=xho(n_sc_st);
            xho=[1:1]*0;
            xho(1)=xend;
      end; end    
      n_sc_st=1;   
       if ishandle(Hf_deb)==0;
       return; end;
      set(Hb1c_dbe,'String',num2str(round(get(Hb1_dbe,'Value'))));
      %%Calculate the energy-band numbers
      volt_dbe=round(get(Hb1_dbe,'Value'));
      Ecdl_dbe=5+0.5*(volt_dbe+Eg_dbe); Ecdr_dbe=5-0.5*(volt_dbe-Eg_dbe);
      Ecul_dbe=Ecdl_dbe+Ew; Ecur_dbe=Ecdr_dbe+Ew;
      Evdl_dbe=5+0.5*(volt_dbe-Eg_dbe); Evdr_dbe=5-0.5*(volt_dbe+Eg_dbe);
      Evul_dbe=Evdl_dbe-Ew; Evur_dbe=Evdr_dbe-Ew;
      yc=[Ecdl_dbe Ecdr_dbe Ecur_dbe Ecul_dbe];
      yv=[Evdl_dbe Evdr_dbe Evur_dbe Evul_dbe];
      set(Hph_dbec,'Ydata',yc,'Visible','on'); set(Hph_dbev,'Ydata',yv,'Visible','on');
      %Set Y position at the bot/top of Ec/Ev at whatever x
      yel=Ecdl_dbe-0.1*volt_dbe*xel; yho=Evdr_dbe+0.1*volt_dbe*(10-xho);
      
      if debcon22_usd8>0 %Electron selected
         set(Hpel_dbe,'Xdata',xel,'Ydata',yel,'Visible','on'); 
         set(Hpel_tr,'Xdata',xel,'Ydata',yel,'Visible','on');
      else %Hole selected
         xho(n_sc_st)=xho; yho(n_sc_st)=yho;
         set(Hpho_dbe,'Xdata',xho,'Ydata',yho,'Visible','on');
         set(Hpho_tr,'Xdata',xho,'Ydata',yho,'Visible','on');
      end   
      end
      
      if n_sc_st>100; 
         if debcon22_usd8>0
         xend=xel(n_sc_st); yend=yel(n_sc_st);
         n_sc_st=1;
         xel=[1:1]*xend; yel=[1:1]*yend;
         else
         xend=xho(n_sc_st); yend=yho(n_sc_st);
         n_sc_st=1;
         xho=[1:1]*xend; yho=[1:1]*yend;
      end; end  
      
      n_sc_st=n_sc_st+1;
            
      %Generate random scattering length,
      lsc=randn*lscmu+lscav; if lsc<st_deb; lsc=st_deb; end
      %alternating direction
      if (0.5*n_sc_st)>(round(0.5*n_sc_st-0.1)) dir_deb=-1; else dir_deb=1; end
      %and random energy position.
      Ekin=rand*kt_deb;
      %Set Y position for the current x
      if debcon22_usd8>0; 
      xel(n_sc_st)=xel(n_sc_st-1);
      yel(n_sc_st)=Ecdl_dbe-0.1*volt_dbe*xel(n_sc_st)+Ekin; 
      else 
      xho(n_sc_st)=xho(n_sc_st-1);
      yho(n_sc_st)=Evdr_dbe+0.1*volt_dbe*(10-xho(n_sc_st))-Ekin;
      end
      %Calculate the number of "trace" steps
      nst_deb=round(lsc/st_deb);
      
      %Start the ANIMATION inside a scattering step with electron
      if debcon22_usd8>0 %Electron selected
         if ishandle(Hf_deb)==0;
         return; end;
         set(Hpel_dbe,'Ydata',yel(n_sc_st)); 
         drawnow;
         for i_deb=1:nst_deb;
            n_sc_st=n_sc_st+1;
            xel(n_sc_st)=xel(n_sc_st-1)+dir_deb*st_deb;
            yel(n_sc_st)=yel(n_sc_st-1);
            if yel(n_sc_st)<(Ecdl_dbe-0.1*volt_dbe*xel(n_sc_st)); dir_deb=1; end % Hitting Eg
                   if ishandle(Hf_deb)==0;
                   return; end;
            set(Hpel_dbe,'Xdata',xel(n_sc_st)); 
            set(Hpel_tr,'Xdata',xel,'Ydata',yel);
            drawnow;
            %pause(0.05);
         end   
         if xel>10; if volt_dbe>0; 
               while debcon5_usd8<1;
                  pause(1);
                  if ishandle(Hf_deb)==0;
                  return; end;
               end   
         end; end
                  
      %Start the ANIMATION inside a scattering step with hole
     else % Hole selected
              if ishandle(Hf_deb)==0;
              return; end;
            set(Hpho_dbe,'Ydata',yho(n_sc_st)); 
            drawnow;
            for i_deb=1:nst_deb;
            n_sc_st=n_sc_st+1;   
            xho(n_sc_st)=xho(n_sc_st-1)-dir_deb*st_deb;
            yho(n_sc_st)=yho(n_sc_st-1);
            if yho(n_sc_st)>(Evdr_dbe+0.1*volt_dbe*(10-xho(n_sc_st))); dir_deb=1; end % Hitting Eg
                   if ishandle(Hf_deb)==0;
                   return; end;
            set(Hpho_dbe,'Xdata',xho(n_sc_st)); 
            set(Hpho_tr,'Xdata',xho,'Ydata',yho);
            drawnow;
            %pause(0.05);
            end 
            if xho<0; if volt_dbe>0;
               while debcon5_usd8<1;
                  pause(1);
                  if ishandle(Hf_deb)==0;
                  return; end;
               end   
            end; end
      end
      
       if ishandle(Hf_deb)==0;
       return; end;
      tic; % Delay for less <1s
      while toc<0.2;
      pause(0.1);
      end   
      
   end %STOP goes to START 
       if ishandle(Hf_deb)==0;
       return; end;
   set(Hph_dbec,'Visible','off'); set(Hph_dbev,'Visible','off');
   set(Hpel_tr,'Visible','off'); set(Hpho_tr,'Visible','off'); 
   set(Hpel_dbe,'Visible','off'); set(Hpho_dbe,'Visible','off');
   set(Ht_deb,'Visible','on');
   set([Hb21_dbe Hb22_dbe Hb3_deb Hb4_deb],'Enable','on');
   set(Hbs_deb,'Enable','off');
   debcon3_usd8=0; debcon5_usd8=0;
end % If debcon4_usd8==0
end % End main loop
close
