Need help with colour sensor, servo system!

heyo, im super new to arduino stuff, and this is my first project. i need to make a servo move to a certain angle depending on what color is detected by the colour sensor (the TCS230, the small black one) , ive done a bit of research and tried to code it myself but the servo doesnt seem to respond to the code. the serial monitor says that its detecting colors, and i have somewhat calibrated it, but the servos dont move even though the paramaters are met, help is greatly appreciated!!

heres the code that i used,

/*
  Color Sensor Calibration
  color-sensor-calib.ino
  Calibrate RGB Color Sensor output Pulse Widths
  Uses values obtained for RGB Sensor Demo sketch 

  DroneBot Workshop 2020
  https://dronebotworkshop.com
*/

// Define color sensor pins
#include <Servo.h>
int servoPin = 2;
Servo Servo1;

#define S0 4
#define S1 5
#define S2 6
#define S3 7
#define sensorOut 8



int redPW = 0;
int greenPW = 0;
int bluePW = 0;

void setup() {

  // Set S0 - S3 as outputs
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
   Servo1.attach(servoPin);
  int servoPosition = 90;
  
  // Set Sensor output as input
  pinMode(sensorOut, INPUT);
  
  // Set Pulse Width scaling to 20%
  digitalWrite(S0,HIGH);
  digitalWrite(S1,LOW);
  
  // Setup Serial Monitor
  Serial.begin(9600);
}

void loop() {
  
  // Read Red Pulse Width
  redPW = getRedPW();
  // Delay to stabilize sensor
  delay(200);
  
  // Read Green Pulse Width
  greenPW = getGreenPW();
  // Delay to stabilize sensor
  delay(200);
  
  // Read Blue Pulse Width
  bluePW = getBluePW();
  // Delay to stabilize sensor
  delay(200);
  // Print output to Serial Monitor
  Serial.print("Red  = ");
  Serial.print(redPW);
  Serial.print(" - Green  = ");
  Serial.print(greenPW);
  Serial.print(" - Blue  = ");
  Serial.println(bluePW);
  
}


// Function to read Red Pulse Widths
int getRedPW() {

  // Set sensor to read Red only
  digitalWrite(S2,LOW);
  digitalWrite(S3,LOW);
  // Define integer to represent Pulse Width
  int PW;
  // Read the output Pulse Width
  PW = pulseIn(sensorOut, LOW);
  // Return the value
  return PW;

}

// Function to read Green Pulse Widths
int getGreenPW() {

  // Set sensor to read Green only
  digitalWrite(S2,HIGH);
  digitalWrite(S3,HIGH);
  // Define integer to represent Pulse Width
  int PW;
  // Read the output Pulse Width
  PW = pulseIn(sensorOut, LOW);
  // Return the value
  return PW;

}

// Function to read Blue Pulse Widths
int getBluePW() {

  // Set sensor to read Blue only
  digitalWrite(S2,LOW);
  digitalWrite(S3,HIGH);
  // Define integer to represent Pulse Width
  int PW;
  // Read the output Pulse Width
  PW = pulseIn(sensorOut, LOW);
  // Return the value
  return PW;

if(redPW>50 && redPW<65){  // red detected
   Servo1.write(45);
   delay(5);
  }
  else if(redPW>105 && redPW<125){  // green detected
    
    Servo1.write(135);
    delay(5);
  }
  else if(redPW<95 && redPW>85){  // blue detected
    Servo1.write(90);
    delay(5);
  }
  else{
    Servo1.write(90);
    delay(5) ; // none of the three colors above detec
  }
}

ah i seee, thanks for the reply btw, how can i fix this problem?

okay wow, i got it to work, thanks so much hahahah, much appreciated

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.