Here is a link which has a schematic very close to what you ask for using an Op Amp & an inverter to achieve what a "comparator" will do.
http://www.dakeng.com/threading.html