Function InitIR { Interrupt setups; Initialize rising-edge capture mode to the IR Sensor pin; } Function IR_Sensor_Response { Compute period based on this capture and last caputure; Update last capture; Return the period from IR signal; } Function IR_Sensor { Initialize IR_flag to be true; Get signal period from IR_Sensor_Response; If IR_flag is true and the period is in the range around 0.8 ms Post event IR_Detected; Set IR_flag to be false; Else if IR_flag is false Set IR_flag to be true; End if }