Problem with servo and color sensor

Hello!
I'm trying to get a servo to slowly rotate to a specific place when the color sensor detects different colors. My problem is that the servo only rotates sometimes even though the color sensor gives me values.

This is my first arduino project so be patient...
I would be very grateful for help :slight_smile:

#include <VarSpeedServo.h>

// TCS230 or TCS3200 pins wiring to Arduino
#define S1 4
#define S0 5
#define S3 6
#define S2 7
#define sensorOut 8

VarSpeedServo bottomServo;

int frequency = 0;
int color=0;

void setup() {  
  // Setting the outputs
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  
  // Setting the sensorOut as an input
  pinMode(sensorOut, INPUT);  
  // Setting frequency scaling to 20%  
  digitalWrite(S0,HIGH);
  digitalWrite(S1,LOW);  
  
   bottomServo.attach(3); //SERVON BLIR PINNAD
   // Begins serial communication 
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  
  color = readColor();
  delay(3000);  
 
  switch (color) {
    case 1:
    bottomServo.write(30, 10, true);        // move to 180 degrees, use a speed of 30, wait until move is complete
    break;
    case 2:
    bottomServo.write(60, 10, true);        // move to 180 degrees, use a speed of 30, wait until move is complete
    break;
    case 3:
    bottomServo.write(90, 10, true);        // move to 180 degrees, use a speed of 30, wait until move is complete
    break;
    case 4:
    bottomServo.write(120, 10, true);        // move to 180 degrees, use a speed of 30, wait until move is complete
    break;
    case 5:
    bottomServo.write(150, 10, true);        // move to 180 degrees, use a speed of 30, wait until move is complete
    break;
    case 6:
    bottomServo.write(180, 10, true);        // move to 180 degrees, use a speed of 30, wait until move is complete
    break;
    
    case 0:
    break;
  }

  color=0;
}
// Custom Function - readColor()
int readColor() {
  // Setting red filtered photodiodes to be read
  digitalWrite(S2, LOW);
  digitalWrite(S3, LOW);
  // Reading the output frequency
  frequency = pulseIn(sensorOut, LOW);
  int R = frequency;
  // Printing the value on the serial monitor
  Serial.print("R= ");//printing name
  Serial.print(frequency);//printing RED color frequency
  Serial.print("  ");
  delay(50);
  // Setting Green filtered photodiodes to be read
  digitalWrite(S2, HIGH);
  digitalWrite(S3, HIGH);
  // Reading the output frequency
  frequency = pulseIn(sensorOut, LOW);
  int G = frequency;
  // Printing the value on the serial monitor
  Serial.print("G= ");//printing name
  Serial.print(frequency);//printing RED color frequency
  Serial.print("  ");
  delay(50);
  // Setting Blue filtered photodiodes to be read
  digitalWrite(S2, LOW);
  digitalWrite(S3, HIGH);
  // Reading the output frequency
  frequency = pulseIn(sensorOut, LOW);
  int B = frequency;
  // Printing the value on the serial monitor
  Serial.print("B= ");//printing name
  Serial.print(frequency);//printing RED color frequency
  Serial.println("  ");
  delay(50);
  if(R<20 & R>5 & G<50 & G>35 & B<30 & B>15){
    color = 1; // Red
  }
  if(G<35 & G>20 & B<30 & B>10 & R>10 & R<30){
    color = 2; // Vit
  }
  if(R<30 & R>20 & G<40 & G>20 & B<50 & B>30){
    color = 3; // Green
  }
  if(R<30 & R>15 & G<30 & G>15 & B>30 & B<45){
    color = 4; // Yellow
  }
  if(R<56 & R>46 & G<65 & G>55){
    color = 5; // Svart
  }
  if (G<65 & G>25 & B<40 &B>26){
    color = 6; // Blue
  }
  return color;  

  
}

Hello.

ebsiv:
if(R<20 & R>5 & G<50 & G>35 & B<30 & B>15){
color = 1; // Red
}
if(G<35 & G>20 & B<30 & B>10 & R>10 & R<30){
color = 2; // Vit
}
if(R<30 & R>20 & G<40 & G>20 & B<50 & B>30){
color = 3; // Green
}
if(R<30 & R>15 & G<30 & G>15 & B>30 & B<45){
color = 4; // Yellow
}
if(R<56 & R>46 & G<65 & G>55){
color = 5; // Svart
}
if (G<65 & G>25 & B<40 &B>26){
color = 6; // Blue
}

Here, you are doing a bitwise AND operation, which I doubt you are trying to do. If you want the logical AND, you need to use "&&" or "and". I believe most people like to stick to the conventional double ampersand.

Hope this helps.

Thank you!

I'm trying to do this with a 360-servo (hsr-1425cr), but the servo never stops spinning. Why is that? Is it because of the servo or the code? Sometimes the servo changes direction, but that is all that happens.

Continuous (360) servos have the feedback pot (inside the servo) disconnected so are no longer servos, they are just gear motors that can be controlled in speed and direction by a servo signal. They have lost the ability to have positional control. The motor should stop with an approximately 90 degree or 1500 microseconds signal. The actual value to stop the motor would be determined experimentally.