Arduino Servo Sweeping

Hey guys, so I need to have a servo start "sweeping" in a specific direction (similar to the sweep example) if a photo resistor exceeds a certain threshold. The problem is that I cannot use a forloop like the sweep example because it gets stuck in that loop and the servo goes too far. I only need the servo to go until it triggers a different photo resistor, and that distance is not always the same, so it needs to scan until it reaches the correct distance.

Is there a way I can get the servo to start moving in a specific direction until it triggers the photo resistor, then stop until it gets a reading from a different photo resistor and start moving in the other direction until it triggers that one.

Here is the code that I tried:

if(analogRead(Sharp1) > threshold ){
myservo.write(pos +=1);//move the servo up because the note is sharp
delay(15);
}

if(analogRead(Flat1) > threshold ){
myservo.write(pos -=1);//move the servo down because the note is flat
delay(15);
}

Look at how the servo is controlled in the demo several things at a time.

...R

That helped lead me to solving the problem, thank you.