Auto bed level uses a single switch. It also requires you to home all axis (G28) before you run the auto level command (G29). This is because the auto bed level uses absolute positioning for the probe points.
Also try to remember the code was written with the idea of using a servo to swing the switch out. So, if you home Z or home All, it swings the switch down and then uses that to 'home'. Then, you run the auto level command and it will (again) swing the switch down and probe three points on your bed. These three points (and not the first one) are used to calculate the correction factor. As a matter of fact, the G28 command will zero out the correction factors. Take a look at some of the introduction youtube videos to see that the switch for homing is the same for the auto level.
Now, Adrian can correct me if I am wrong, but the Z probe function just tells the Z axis to move down (further than it expects to go). The homing functions work the same way. It tells the axis to move 1.5 times farther than the axis length is defined. In other words, it is commanded to purposely hit the endstop. In the stepper.cpp file is the stepper Interrupt routine, which constantly checks the endstops while it writes the 'steps' to the output pins. If an endstop is defined, and is triggered, the MCU no longer writes any more 'step' outputs until the move is finished. The planner then knows what position it is in.
So.
What nobody has told you yet is that you will need to get pretty creative here. If you try to rewrite the stepper interrupt service routine, you will be hosed if you run a G28, since the nozzle will NOT be under the print surface after it homes X and Y. Possible fix would be to add an X/Y move to place the nozzle over the bed before it runs the HOME[Z-AXIS] in the G29 command.
I have no idea how you plan to insert the analog value into this to begin with. It sounds like your using a timer event to read the analog input. At the time you read the analog value, you should set/clear a bool value (as in HIT or NOT HIT). This could be the value you would evaluate in the stepper interrupt (instead of the Z switch pin). My only concern is that if you use one interrupt to read an analog value and another interrupt to write stepper output pulses governed by that analog value, your analog routine better run at nearly the same speed as the stepper routine or else the stepper will move much farther than what your sensor tells you it should stop.
Quite frankly, this is alot of firmware work, with a great opportunity for introducing alot of bugs into the system (analog noise cause and endstop hit when nowhere near the print surface, or what about when the nozzle hits a curled up edge in the middle of a print job?). Honestly, a much easier solution would be to run the sensor into a separate comparator that can output a digital high/low signal that would then be input to the MCU. Its more hardware, but you leave the fimrware alone. Although, this would still require you to perform an alternate method of homing all your axis before running the G29 command.
EDIT: reading what I posted sounds pretty ugly. If you want me to, I can post in laymans terms what is happening during the homing and auto-bed-level functions in another post. Then what I said might make more sense.
Chuck Bittner is a quadriplegic gamer who is petitioning the major console developers to include internal button remapping in all console games. You can help.
Sign Chuck Bittners petition