Limit Switch code work

So how do you make the head stop turning when it hits the switch? I
tried simply changing direction but that resulted in some chattering as
the cam passed the switch.

I'm going to try calibrating the servo so I can have it just stop when
the switch is hit.