The most critical part of this machine is the 3D printer mechanics that actually hold and organize the individual pills. I went through several iterations of this before settling on the current version (that I already want to improve). The first version was a snake of sorts that would slide around the base of the box and could align one of the many individual storage boxes above a pill insertion port. The main challenge I had wasn't getting the pills in but getting them out efficiently.
I went through a couple iterations of the snake version but couldn't converge on anything that worked reliably and could make sure the pills never fall out of their containers by accident.
The latest version I ended up using for this project is based on a rotating barrel. As you can see below in the blown out view, a barrel (blue) with 16x4 slots rotates in a drum (orange). The drum keeps the pills in their individual slots while it rotates. There are holes in the barrel at the bottom and top. Pills can enter the barrel at the top and doors can open to allow the pill to simply fall out with gravity when selected by doors (pink). The selector at the top (green) slides back and forth to select which of the 4 holes to insert the pill into when loaded.
The biggest challenge with actually building this is the tolerances in my 3D printer. For example, when I print the circular barrel it actually ends up being more oval shaped. This makes it hard to maintain a tight tolerance from the drum to the barrel to make sure the pills don't fall out when rotating. Also, the doors at the bottom have to fit perfectly in place to make sure the pills don't get caught at the joints when the drum is rotating. After it was all printed I ended up having to manually un-warp some of the prints and shave off some of the extra material to get the doors at the bottom to fit perfectly.
If you want to explore the design in 3D you can download an STL of the blown out design here: https://www.dropbox.com/s/slu101yfu7tsvtf/full machine blown out.stl?dl=0 and a 123D design file here: https://www.dropbox.com/s/i82wmfc3rblamnl/full_system.123dx?dl=0