Problem With TC3200 resetting after I scan a color

Im having an issue with my color sensor dimming and reseting after i try and scan a color. Im using the arduino Mega 2560 and the color sensor is the TC3200. Even on the serial monitor it stops running and im not sure what to do about this.

//define connections and terms
#include<Servo.h>
#define middle 90
#define left 0
#define right 180
#define sensorout 8
#define up 0
#define down 90
#define S0 4
#define S1 5
#define S2 6
#define S3 7

int ledR=10;
int ledG=11;
int ledB=12;
int frequency=0;
int R=0;
int G=0;
int B=0;

Servo door;
Servo container;
int delayTime = 1000;

void setup() {
// attach servos
door.attach(2);
container.attach(3);
//center the servos
container.write(middle);
door.write(middle);

//Output/Input
pinMode(S0,OUTPUT);
pinMode(S1,OUTPUT);
pinMode(S2,OUTPUT);
pinMode(S3,OUTPUT);
pinMode(sensorout,INPUT);
pinMode(ledR,OUTPUT);
pinMode(ledG,OUTPUT);
pinMode(ledB,OUTPUT);
//set frequency-scaling
digitalWrite(S0,HIGH);
digitalWrite(S1,LOW);

Serial.begin(9600);
Serial.println(“Color Reader”);
}
bool isRed()
{
if((R<90 && R>80)&&(G<265 && G>245)&&(B<60 && B>45))
{
return true;
}
else
{
return false;
}
}
bool isGreen()
{
if((R<210 && R>190)&&(G<160 && G>140)&&(B<60 && B>50))
{
return true;
}
else
{
return false;
}
}
bool isBlue()
{
if((R<210 && R>190)&&(G<130 && G>110)&&(B<45 && B>30))
{
return true;
}
else
{
return false;
}
}
void loop() {

//setting red filtering
digitalWrite(S2,LOW);
digitalWrite(S3,LOW);
//Read output frequency
R=pulseIn(sensorout,LOW);
//Print value to serial monitor
Serial.print("R= “);//printing name
Serial.print(R);//print red color frequency
Serial.print(” ");
delay(100);

//Change LED Color to Red
if(isRed())
{
Serial.println(“this is the RED Color”);
digitalWrite(ledR,255);
digitalWrite(ledG,0);
digitalWrite(ledB,0);
container.write(right);
door.write(up);
delay(500);
door.write(down);
delay(1000);
digitalWrite(ledR,0);
}

//settine Green filtering
digitalWrite(S2,HIGH);
digitalWrite(S3,HIGH);
//Read output frequency
G=pulseIn(sensorout,LOW);
//Print value to serial monitor
Serial.print("G= “);//printing name
Serial.print(G);//print red color frequency
Serial.print(” ");
delay(100);

//Change LED Color to Green
if(isGreen())
{
Serial.println(“this is the Green Color”);
digitalWrite(ledR,0);
digitalWrite(ledG,255);
digitalWrite(ledB,0);
container.write(middle);
door.write(up);
delay(500);
door.write(down);
delay(1000);
digitalWrite(ledG,0);
}

//settine Blue filtering
digitalWrite(S2,LOW);
digitalWrite(S3,HIGH);
//Read output frequency
B=pulseIn(sensorout,LOW);
//Print value to serial monitor
Serial.print("B= “);//printing name
Serial.print(B);//print red color frequency
Serial.println(” ");
delay(100);

//Change LED Color to Blue
if(isBlue())
{
Serial.println(“this is the Blue Color”);
digitalWrite(ledR,0);
digitalWrite(ledG,0);
digitalWrite(ledB,255);
container.write(left);
delay(100);
door.write(up);
delay(500);
door.write(down);
delay(1000);
digitalWrite(ledB,0);
}
}

Color_Sorter.ino (2.99 KB)

Post the code correctly and also post a schematic, then we can start to address your problem.
Read the how to use this forum sticky post for instructions of how to do these things.

After you have fixed your post by adding code tags, explain how you are powering the servos.

If it is from the 5V Arduino output, that is the problem. Use a separate servo power supply (e.g. 4xAA NiMH batteries). Don't forget to connect the grounds.