function Switch_output = Switching_logic_tank_1(e_s_outlet_valve, ...
                e_s_inlet_valve, time)
global Switch_tank_1;
% Switch_tank_1 = 1 --> Controller for outlet valve is active
% Switch_tank_1 = 0 --> Controller for inlet valve is active
global switching_delay_1
 if e_s_outlet_valve ~= 0
    if Switch_tank_1 == 1 % Checking if Z1 controller is active from tank 1
        if time >= switching_delay_1
        Switch_tank_1 = 0; % Makng Z0 controller active from tank 1
        switching_delay_1 = time + 0.1; % Switching cannot occur more than
                                        % every 6 seconds = 0.1 minutes
        end
    end
 end
if e_s_inlet_valve ~= 0
   if Switch_tank_1 == 0 % Checking if Z0 controller is active from tank 1
      if time >= switching_delay_1
        Switch_tank_1 = 1; % Makng Z1 active
        switching_delay_1 = time + 0.1; % Switching cannot occur more than
                                        % every 6 seconds = 0.1 minutes
      end
   end
end
Switch_output = Switch_tank_1; % Binary output
end