light sensing servos and stopping after a certain amount

hi all
I'm new to Arduino and I'm trying to make a robot controlled by three servos that is light sensitive ( walks toward light when light is shined on but still move when there is no light) and stop its motion (servos and photcells) after 40 seconds. My servos work but do not stop after 40 seconds. I was wondering any one could help me the motion stop after 40 seconds and will my code work for the photocells.
thanks

#include <Servo.h>

Servo FrontServo; // create servo object to control a servo
Servo MiddleServo; // create servo object to control a servo
Servo RearServo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
int val; // Variable to store the photocell reading
int val2; // Variable to store the photocell2 reading
int startTime = 0; // starting time = 0 seconds
int time; // variable time

void setup()
{
FrontServo.attach(3); // attaches the servo on pin 9 to the servo object
MiddleServo.attach(5); // attaches the servo on pin 9 to the servo object
RearServo.attach(12); // attaches the servo on pin 9 to the servo object
}

void loop()
{
val = analogRead(0); // Reading of photocellPin
val = map(val, 0, 1023, 0, 179); // Maps the value of the photocell from 0-1023 then from 0 to 179
FrontServo.write(val); // Tell FrontServo to go to position in variable 'val'
delay(15); // Waits 15ms for the photocell to read the value (.015 seconds)

val2 = analogRead(1); // Reading of photocellPin2
val2 = map(val, 0, 1023, 0, 179);// Maps the value of the photocell2 from 0-1023 then from 0 to 179
RearServo.write(val2); // Tell RearServo to go to position in variable 'val2'
delay(15); // Waits 15ms for the photocell2 to read the value (.015 seconds)

for(pos = 60; pos <= 120; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
FrontServo.write(pos); // tell servo to go to position in variable 'pos'
MiddleServo.write(pos); // tell servo to go to position in variable 'pos'
RearServo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 30ms for the servo to reach the position

time = millis(); // this gives current time
Serial.print("time: "); // print the time
Serial.println(time); // print ln of time

if (time >= (40000 + startTime)) // if the time is greater then 40 seconds (40000 ms)
{
void end(); // then end
FrontServo.attach(3); // front servo attached to digital pin 3
MiddleServo.attach(5); // front servo attached to digital pin 5
RearServo.attach(12); // front servo attached to digital pin 12
val = analogRead(0); // photocell atached to anaolog pin 0
val2 = analogRead(1); // photocell atached to anaolog pin 1
}
}
}

One answer is to learn a technique and approach that will let you see your project in a different way.

At the bottom of my post are 3 links.
The first teaches how to do multiple things together instead of one at a time.
The second teaches about control and state machines.

Both are stripped down to simple examples as is the third that teaches interrupts.

You are doing analogReading of pin 1 YET you are also using Serial.print. Do you realise that the Serial sends it's data over pin 1 (it also recieves it on pin 0);

Perhaps it would be an idea to select another couple of pins for your light sensors and try again.

Get rid of all the delay()s in your program and use millis() to manage your timing. See the demo several things at a time and the Thread planning and implementing a program

And please use code tags (the button like a scroll with <> on top) to make your code look like this.

...R

You are doing analogReading of pin 1 YET you are also using Serial.print. Do you realise that the Serial sends it's data over pin 1 (it also recieves it on pin 0);

Analog pin 1 and digital pin 1 are on opposite sides of the board. The Serial instance does NOT use analog pin 1.

PaulS:
Analog pin 1 and digital pin 1 are on opposite sides of the board. The Serial instance does NOT use analog pin 1.

You know more than I do. But then you were here long before me.

I thought that analogRead(1) meant try to analog read Arduino pin 1.
I thought you'd have to use (A1) to get analog pin 1.

GoForSmoke:
I thought that analogRead(1) meant try to analog read Arduino pin 1.
I thought you'd have to use (A1) to get analog pin 1.

analogRead(1) does an analogRead on pin A1; there's no confusion possible since you can't ever do an analogRead on a digital pin.

On the other hand, you can use an analog pin as a digital pin, and in that case you would say digitalRead(A1) since digitalRead(1) would mean the digital pin. But you can also refer to the analog pins by continuing the numbering from 13 (on a Uno), as in digitalRead(14).