We actually did your example: our robot could automatically lift a tote to the appropriate height when it gone one. Here's how we did it:
Our robot got its totes from the human player station. A tote would slide through a ramp inside our robot and come out on the other side. There was a small touch sensor inside the ramp, so we could see when a tote was sliding through the ramp. After a delay of 1-2 seconds, we'd have our lift move to the appropriate position, taking the new tote with it.
We combined that with some more code and created a button that, when held, automatically stacked all totes coming through the human player station.
Generally, automating your robot is about two things: being able to detect whatever you need, and having a robot that can accurately do the task without human input.
The file relating to our automatic stacking can be seen here:
https://github.com/FRC-1902/2015-gam...ackThread.java The programming in the code() function just loops constantly.
Hope this helps!