We use an IR distance sensor on our robot, which I think is what you want... I'm not sure how well its going to work for you, depending on the color of the carpet, etc.
This, I think is the exact one we use
http://www.acroname.com/robotics/par...6-GP2D120.html
That should be pretty simple to wire up.. programming it is even simpler, its an analog sensor, get Get_Analog_Value() it
Using a distance sensor might be overkill for what you want though.. are you sure theres no way you can do it with some type of switch?