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
}