Simple IR Sensor Program

Please post your best effort at the code. Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Tell us what the code actually does and how that differs from what you want.

Please post a schematic or wiring diagram. Written descriptions are always more ambiguous than a drawing. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.

Just off the top of my head I might suggest a couple of things that may help.

  1. When you monitor digital inputs it is often more important to detect changes in state instead of levels. With state change detection you can react to a switch that goes from off to on and ignore when it goes off or is off or is on. My state change tutorial may be of interest.

  2. Code using a state machine.