Another way to do this, same principle as the crankshaft but somewhat different, would be an obtusely shaped cam.
A rod guided by a track or guideposts of some sort is held against the rotating cam by a return spring. As the more obtuse (longer) part of the cam is turned by the motor (usually gear reduction), it pushes the rod and stretches the spring, moving it outward in a linear fashion. As the cam moves around toward the shorter, rounder part of the cam, the spring pulls the rod back to its original position.
I wish I had an animation or the ability to animate it myself.