Creating an optical encoder with an ir sensor

I don't like you having if () {} else if () {} else if () { }

what if none of those apply ? If you are having trouble figuring out what your system is doing, add another "else"
at the end, and print a message, so you know which scenarios are not being dealt with.

I'm not clear on why you have three notable outcomes there. You have something happen when you
go from white to black, and from black to white . What is the third one for ? Can you be sure
your device only rotates in one direction ?