This Arduino sketch is the glide lock for a crank-driven ornithopter. It's meant to stop the wings at a small dihedral angle when the motor is turned off.
The system works by iddling the motor below a certain throttle until the crankshaft reaches a known position. The position of the rotor is measured using a hall sensor and a magnet attached to the crank shaft. When the magnet passes in front of the sensor, the motor turns off. However, the motor will continue to spin even after the throttle is set to zero. To work around this, one can continue to spin the motor for a short time interval after the magnet passes the sensor. If this delay is tuned correctly, the crank will stop in more or less the desired position.
The code can accommodate either an analog or digital hall sensor as both use the same circuit.
Due to some odd behaviour, the input pin for the hall sensor has to change depending on whether it is analog or digital. Use P0 for digital and P4 for analog.
The ATTiny code requires ATTinyCore to compile.