Breaking sweep program trouble (SOLVED)

Hi, I’m trying to break the sweep program, so when a photocell picks up a range less than 200 the servo will stop moving, if the photocell picks a range higher than 200 it will continue it’s movement (0- 180 degrees). The following code doesn’t seem to work… Any suggestions?
code:

#include <Servo.h>
int photocellPin = 0;     // the cell and 10K pulldown are connected to a0
int photocellReading;     // the analog reading from the sensor divider        // connect Red LED to pin 11 (PWM pin)
Servo myservo;
int pos = 1;

void setup(void) {
myservo.attach(9);
  Serial.begin(9600);
 
}
 
void loop(void) {
  photocellReading = analogRead(photocellPin); 
  photocellReading = map(photocellReading, 500, 840, 0, 255);
for(pos = 0; pos < 180; pos += 1)
{
  myservo.write(pos);
  delay(15);
}
for(pos = 180; pos>=1; pos-=1)
{myservo.write(pos);
delay(15);

}
  if(photocellReading  < 200 ) {myservo.write(LOW);}
  else { myservo.write(HIGH);}
 
  
 
  Serial.print("Analog reading = ");
  Serial.println(photocellReading);     
 
  
 
  delay(100);
}

Why not?

  photocellReading = analogRead(photocellPin); 
  photocellReading = map(photocellReading, 500, 840, 0, 255);

  if(photocellReading  >= 200 ) {
    for(pos = 0; pos < 180; pos ++) {
      myservo.write(pos);
      delay(15);
    }
    for(pos = 180; pos>=1; pos--){
        myservo.write(pos);
        delay(15);
    }
  }
  Serial.print("Analog reading = ");
  Serial.println(photocellReading);     
  delay(100);

Or better yet?

boolean forward = true;
  photocellReading = analogRead(photocellPin); 
  photocellReading = map(photocellReading, 500, 840, 0, 255);
  
  if(photocellReading  >= 200 ) {
     if (forward) {
        if (++pos >= 180) {
             forward = false;
        }
     } 
     else {
        if (--pos <= 0) {
             forward = true;
        }
    }
    myservo.write(pos);
    delay(15);
  }
  Serial.print("Analog reading = ");
  Serial.println(photocellReading);     
  //delay(100);

EDIT: The advantage of this version is that the sweep stops in the place that the it detects the distance less than 200, and once the distance is greater than or equal to 200 it resume from the place it stops.

After monitoring your programs behavior, this is what happens, servo goes through 180 degree loop (if photocell picks up value that is <=200 it stop, GREAT!) then once the cycle is complete it stops and wait for a while. Then it goes through the cycle of 0 - 180 degree, however much slower than the first time. Once the second cycle is complete the servo doesn’t move at all?? Any more suggestions??

Your first code makes no sense.

In each cycle through loop() , you are reading the photocell once, and then moving the servo through 180 degrees and back again.

If you are scanning the surroundings, surely you need to take a reading of the photocell at each step through the scan ?

can someone please help?, I understand that my first program doesn't make sense and that's the problem...
What i want my program to do is;
Move the servo from 0-180 degrees and back (180-0) continuously, If the photocell picks up change in resistance stop the servo in it's current position.

It sounds like the best version so far is the code you tested after including luisilva’s suggestions in reply #2. Why not post that code (complete program) for people to review. It sounded like it was nearly there.

After monitoring your programs behavior, this is what happens, servo goes through 180 degree loop (if photocell picks up value that is <=200 it stop, GREAT!) then once the cycle is complete it stops and wait for a while. Then it goes through the cycle of 0 - 180 degree, however much slower than the first time. Once the second cycle is complete the servo doesn’t move at all

This is the code From Luisilva (modified version)

#include <Servo.h>
int photocellPin = 0;     // the cell and 10K pulldown are connected to a0
int photocellReading;     // the analog reading from the sensor divider        // connect Red LED to pin 11 (PWM pin)
Servo myservo;
int pos = 0;

void setup(void) {
myservo.attach(9);
  Serial.begin(9600);
 
}
 
void loop(void) {
  photocellReading = analogRead(photocellPin); 
  photocellReading = map(photocellReading, 500, 840, 0, 255);
boolean forward = true;
 if(photocellReading  >= 200 ) {
     if (forward) {
        if (++pos >= 180) {
             forward = false;
        }
     } 
     else {
        if (--pos <= 0) {
             forward = true;
        }
    }
    myservo.write(pos);
    delay(15);
  }
  Serial.print("Analog reading = ");
  Serial.println(photocellReading);     
  //delay(100);
}
boolean forward = true;

Either move the above declaration to before setup() to make it global, or leave it in loop() but declare it as:

static boolean forward = true;

At the moment, it is being reset to true each time round loop().

Thankyou soo much. And thankyou to everyone else that helped.

When I write the 2 boxes of code was thinking that you would copy the first and passes it to the place that you have the other variables defined. Of course that when you define it inside the loop() function the value will be reset and the program didn't work properly.