Color sensor conveyer belt and servo system

Hey, I'm making a project where a colored cube is moved through a color sensor with a conveyer belt, and then is moved to a slide controlled by a servo. The slide is then rotated to different angles according to the color detected. I got it to work before hand, and then the next day it just kinda of broke, the colors values are different then the previous day, so I recalibrated it. After that, the colors are getting detected, but the servo just doesn't want to move, sometimes it does but it's super delayed, delayed by around 5 seconds. So just abit confused on what happened and how to fix it. Any help will be greatly appreciated!!

/*
  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);
  //motor as output
  pinMode(10, OUTPUT);
  
  // Set Pulse Width scaling to 20%
  digitalWrite(S0,HIGH);
  digitalWrite(S1,LOW);
  
  // Setup Serial Monitor
  Serial.begin(9600);
}

// 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;
}


void loop() {
  // digitalWrite(10,HIGH);}

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

  if(redPW>15 && redPW<45 && greenPW>50 && greenPW<80 && bluePW>50 && bluePW<85){
      // red detected)
   Serial.print("red detected");
   Servo1.write(45);
   delay(100);
  }
  else if(redPW>35 && redPW<50 && bluePW>20 && bluePW<50 && greenPW>25 && greenPW<50){ // green detected

    Serial.print ("green detected");
    Servo1.write(135);
    delay(100);
  }

  else  if(redPW>40 && redPW<80 && bluePW>40 && bluePW<80 && greenPW>60 && greenPW<80){
    // blu3e detected
    Serial.print ("blue detected");
    Servo1.write(90);
    delay(100);
  
  }

  
  // Read Red Pulse Width

  
}

Get rid of the delays, change to millis loops.

okay i decided to code it from scratch, and now the sensor is far more consistent, but the serial monitor is spamming "blue detected" and the other colors arent getting detected even though it meets the requirements in the if statements. im still new to arduino coding so simplified answers will be most helpful.
heres the serial monitor, while holding green paper to the sensor. it goes like this even if i hold up red paper or even no paper at all.
blue detectedR= 117 G= 56 B= 85
blue detectedR= 117 G= 56 B= 86
blue detectedR= 111 G= 56 B= 80
blue detectedR= 117 G= 55 B= 85

heres the code

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

#include <Servo.h>
int servoPin = 2;
Servo Servo1;

int red = 0;
int green = 0;
int blue = 0;

int color = 0;

void setup() {
  pinMode(S0, OUTPUT);  // color sensor pins
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(sensorOut, INPUT);

  Servo1.attach(servoPin);  // servo pin and position
  int servoPosition = 90;

  // Setting frequency-scaling to 20%
  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);

  Serial.begin(9600);
}

void loop() {

  delay(200);
  // none // Setting red filtered photodiodes to be read
    digitalWrite(S2, LOW);
    digitalWrite(S3, LOW);
    // Reading the output frequency
    red = pulseIn(sensorOut, LOW);
    // Printing the value on the serial monitor
    Serial.print("R= ");  //printing name
    Serial.print(red);    //printing RED color frequency
    Serial.print("  ");
    delay(100);

    // Setting Green filtered photodiodes to be read
    digitalWrite(S2, HIGH);
    digitalWrite(S3, HIGH);
    // Reading the output frequency
    green = pulseIn(sensorOut, LOW);
    // Printing the value on the serial monitor
    Serial.print("G= ");  //printing name
    Serial.print(green);  //printing RED color frequency
    Serial.print("  ");
    delay(100);

    // Setting Blue filtered photodiodes to be read
    digitalWrite(S2, LOW);
    digitalWrite(S3, HIGH);
    // Reading the output frequency
    green = pulseIn(sensorOut, LOW);
    // Printing the value on the serial monitor
    Serial.print("B= ");  //printing name
    Serial.print(green);  //printing RED color frequency
    Serial.println("  ");
    delay(100);
  
     if (red < blue && red < green && red < 100) {
    // red detected
  Servo1.write(45);
    Serial.write ("red detected");
  }
  if (green < red && green < blue && green < 100) {  // green detected
   Servo1.write(135);
    Serial.write ("green detected");
  }
  if(blue<red && blue<green &&blue<100){
  // blue detected
    Servo1.write(90);
    Serial.write ("blue detected");

}
}

Set an "oldcolor" variable... compare it to "newcolor"... only if they are different, print "color detected"... if you are using multiple "blue" blocks, verify this is a new block and clear the "oldcolor" to allow consecutive "blue" blocks.

You're printing green in the blue section

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