I want to create a system that, when a barrel is full, allows the items from the barrel to pass through a hopper to a chest. But only when it's full. I would also like it to be stackable horizontally. I've been trying, but I haven't found any solution. Is it possible? If so, how do I do this?
This circuit does what you need. The two droppers below face into each other, with a single item between the two, serving as RS Latch; if the item is in the right dropper, the hopper is unlocked and the barrel drains. If the item is in the left dropper, the hopper is locked by the redstone torch. The two droppers are triggered by observers above (through quasi-connectivity to prevent cross-talk which QC would cause if they were powered directly). The right observer observes the comparator reading the barrel, in essence producing pulses on changes "empty/non-empty". The left observer watches redstone dust, which is 1 block away from a redstone block, thus it has power level 14. When the barrel is full, the comparator outputs power level 15 and the observer notices the level change, initiating the emptying cycle (sending the item to right dropper). Once the barrel empties, the comparator changes state and the item is sent to left dropper, locking the hopper.
(note there are two spurious pulses: when first item enters into the barrel the lock is re-engaged despite being already engaged, and once the first item is drained from the barrel, the drain cycle start signal is re-sent as no-op as the emptying cycle is already in progress).
Despite use of redstone dust this device is 1-tileable, as the power level 15 applied above the observer will not spill over to level 14 dust in neighboring slices, and QC power to the droppers only powers the droppers directly below their respective observers.
Note use of QC makes this a Java Edition only contraption. To make it work on Bedrock place solid blocks between the observers and the droppers.
You can use a comparator (a Minecraft redstone block, with one of its uses being detection on any storage container), and when it reaches the maximum power of redstone dust (15) you can make a trail of redstone dust back to the barrel and make a repeating loop (called a "redstone clock" in the Minecraft community) using 2 observers having their faces across from each other. Looks like this:
If you want more information on how to make it more "usable", here's a good YouTube tutorial for that: