you're looking for a simple control loop. (see Picture below) The content of the middle chest is checked by a comparator and if a previously set signal strength threshold is reached, the upper hopper is locked and the lower hopper unlocked. Once the middle chest is completely emptied, the system will reset. The lower hopper is locked again and the upper hopper is unlocked.The process starts all over again.
Addition: The set signal strength must be at least 2 or higher.