Issue with color sensor servo interaction

Hello I am writing a program that involves interaction between multiple servos and a color sensor. What I would like is if an object of a certain color gets read through the color sensor, it triggers the servos to move a certain direction. I have the sensor reading the the color of the object fine but I am having trouble with the trigger of the servos. I am not sure if I am initializing the servos wrong or the way I am writing my loops are incorrect. Could someone please help me, I would really appreciate it.

// Define pins
#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                
const int ledpin = 13;
const int GSR1 = 12;
const int GSR0 = 11;
const int GSG1 = 10;
const int GSG0 = 9;
const int GSB1 = 8;
const int GSB0 = 7;

int redpin = A0;
int greenpin = A1;
int bluepin = A2;

// Sensor read values
int red = 0;
int green = 0;
int blue = 0;
int servo1 = 52; // variable to store the servo position
int servo2 = 52; // variable to store the servo position
int servo3 = 52; // variable to store the servo position
void setup() 
{
  Serial.begin(9600);
  
  pinMode(ledpin, OUTPUT);
  pinMode(GSR1, OUTPUT);
  pinMode(GSR0, OUTPUT);
  pinMode(GSG1, OUTPUT);
  pinMode(GSG0, OUTPUT);
  pinMode(GSB1, OUTPUT);
  pinMode(GSB0, OUTPUT);
  
  // Turn on the LED
  digitalWrite(ledpin, HIGH);
  
  // Set the gain of each sensor
  digitalWrite(GSR1, LOW);
  digitalWrite(GSR0, LOW);
  digitalWrite(GSG1, LOW);
  digitalWrite(GSG0, LOW);
  digitalWrite(GSB1, LOW);
  digitalWrite(GSB0, LOW);
  
  servo1 = myservo.attach(3);  // attaches the servo on pin 3 to the servo object
  servo2 = myservo.attach(4);  // attaches the servo on pin 4 to the servo object
  servo3 = myservo.attach(5);  // attaches the servo on pin 5 to the servo object
}
//Servo.begin;
void loop() 
{
  
  // Read sensors
  // On page 7 of the datasheet, there is a graph of the 
  // spectral responsivity of the chip.  Scaling factors were
  // selected based on this graph so that the gain of each 
  // color is closer to being equal
  red = analogRead(redpin) * 10;
  green = analogRead(greenpin) * 14;
  blue = analogRead(bluepin) * 17;

  // Print values to the serial monitor
  Serial.print("Red: ");
  Serial.print(red, DEC);
  Serial.print("\t\tGreen: ");
  Serial.print(green, DEC);
  Serial.print("\tBlue: ");
  Serial.println(blue, DEC);

  delay(300);
  //red skittle
 for(servo1 = 52; servo1 >= 0; servo1-=1)     // goes from 52 degrees to 0 degrees 
{ 
  if(red >= 120 && red <=130 && green <= 182 && green >=196 && blue == 82)
  {
   //digitalWrite(ledY,HIGH);
    servo1= 0;                            
    myservo.write(servo1);              // tell servo to go to position in variable 'servo 1' 
    delay(5);
  }
}
  for(servo2 = 52; servo2 >= 0; servo2-=1)     // goes from 52 degrees to 0 degrees 
  {  
  if(red >= 120 && red <=130 && green <=182 && green >= 197 && blue == 82)
  { 
    servo2 = 0;  
    myservo.write(servo2);              // tell servo to go to position in variable 'servo 2' 
    delay(5);
  }
  }
}
Servo myservo;  // create servo object to control a servo

You need one of those lines for each servo using a diffrent name for each servo. Then you need to attach each one to its own pin.

That's not how you set up for a servo..... have look at this code:

// Sweep for 2 servi
// by BARRAGAN <http://barraganstudio.com> 
// This example code is in the public domain.


#include <Servo.h> 
 
Servo myservoX;  // create servo object to control a servo 
Servo myservoY;                // a maximum of eight servo objects can be created 
 
int posX = 0;    // variable to store the servo position 
int posY = 0; 

void setup() 
{ 
  myservoX.attach(9);  // attaches the servo on pin 9 and 10 to the servo object 
  myservoY.attach(10);
  
  myservoX.write(posX);  //initialise to 0
  myservoY.write(posY);
  
  delay(1000);
} 
 
 
void loop() 
{ 
  //X up
  for(posX = 0; posX < 180; posX += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservoX.write(posX);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  
  //Y up
  for(posY = 0; posY < 180; posY += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservoY.write(posY);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  
  //X down
  for(posX = 180; posX>=1; posX-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservoX.write(posX);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  
  //Y down
  for(posY = 180; posY>=1; posY-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservoY.write(posY);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
}

JimboZA:
That's not how you set up for a servo..... have look at this code:

// Sweep for 2 servi

// by BARRAGAN http://barraganstudio.com
// This example code is in the public domain.

#include <Servo.h>

Servo myservoX;  // create servo object to control a servo
Servo myservoY;                // a maximum of eight servo objects can be created

int posX = 0;    // variable to store the servo position
int posY = 0;

void setup()
{
  myservoX.attach(9);  // attaches the servo on pin 9 and 10 to the servo object
  myservoY.attach(10);
 
  myservoX.write(posX);  //initialise to 0
  myservoY.write(posY);
 
  delay(1000);
}

void loop()
{
  //X up
  for(posX = 0; posX < 180; posX += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservoX.write(posX);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
 
  //Y up
  for(posY = 0; posY < 180; posY += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservoY.write(posY);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
 
  //X down
  for(posX = 180; posX>=1; posX-=1)     // goes from 180 degrees to 0 degrees
  {                               
    myservoX.write(posX);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
 
  //Y down
  for(posY = 180; posY>=1; posY-=1)     // goes from 180 degrees to 0 degrees
  {                               
    myservoY.write(posY);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

If I wanted the servo to move by a specfiic RGB color could I do an if statement before or after the for loop?

You are missing the point. That code is showing you what I said about setting up your servos. Keep your own code but set up the servos correctly.

Grumpy_Mike:
You are missing the point. That code is showing you what I said about setting up your servos. Keep your own code but set up the servos correctly.

Ok I will set up the servos correctly, I just wasn't certain if my for loop if statement order was an issue or not.

I just wasn't certain if my for loop if statement order was an issue or not

You'll find that out once you have the servos configured properly 8). Might be an idea to run the sketch I posted first, adding a third servo and adjusting pins as necessary, just to check the servos and their physical connections are good. That way, if you do have a subsequent problem you'll know what the problem's not.

JimboZA:

I just wasn't certain if my for loop if statement order was an issue or not

You'll find that out once you have the servos configured properly 8). Might be an idea to run the sketch I posted first, adding a third servo and adjusting pins as necessary, just to check the servos and their physical connections are good. That way, if you do have a subsequent problem you'll know what the problem's not.

I'll give it a shot thanks