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 ![]()
#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;Â
Â
}