Best control structure for task of aligning motor with sensor

Maybe have a look at Planning and Implementing a Program

When a program is suitably designed it makes development and troubleshooting much easier. By putting code into a series of single-purpose functions it is possible to test each part separately.

In general the use of the delay() function is inappropriate except for a quick-and-dirty test. See how millis() is used to manage timing without blocking in the tutorial code.

...R