简体   繁体   中英

coding 3 color sorting machine with 3 servo motors and 1 tcs230 sensor with c++ on arduino

I am now on my project that I need to sort the objects with different colors. I'll put all the objects on a conveyor and the sensor will read the color and the conveyor will transfer the object to the location that the box is place next the conveyor and there is servo motor I have installed to flick the object into the box. I found out that the code can not detect the color of the object(I already check that the sensor work properly).

#include <Servo.h> // include the Servo library

// Define the pins for the TCS230 color sensors
#define TCS230_S0 4
#define TCS230_S1 5
#define TCS230_S2 6
#define TCS230_S3 7

// Define the pins for the servo motors
#define SERVO1 9
#define SERVO2 10
#define SERVO3 11

// Define the RGB color values for each color to be sorted
#define RED_R 200                                 
#define RED_G 0
#define RED_B 0

#define GREEN_R 0
#define GREEN_G 200
#define GREEN_B 0

#define BLUE_R 0
#define BLUE_G 0
#define BLUE_B 200
#define YELLOW_R 200
#define YELLOW_G 200
#define YELLOW_B 0

Servo servo1; // create Servo object for servo1
Servo servo2; // create Servo object for servo2
Servo servo3; // create Servo object for servo3

void setup() {
  // initialize the TCS230 color sensor pins
  pinMode(TCS230_S0, OUTPUT);
  pinMode(TCS230_S1, OUTPUT);
  pinMode(TCS230_S2, OUTPUT);
  pinMode(TCS230_S3, OUTPUT);

  // initialize the servo motor pins
  servo1.attach(9);
  servo2.attach(10);
  servo3.attach(11);
}

void loop() {
  int red, green, blue; // variables to store the color values

  // read the color values from the TCS230 color sensor
  digitalWrite(TCS230_S2, LOW);
  digitalWrite(TCS230_S3, HIGH);
  red = pulseIn(TCS230_S0, LOW);
  green = pulseIn(TCS230_S1, LOW);
  digitalWrite(TCS230_S2, HIGH);
  digitalWrite(TCS230_S3, HIGH);
  blue = pulseIn(TCS230_S0, LOW);

  // compare the color values to the predefined RGB values for each color
  if (red > RED_R && green < RED_G && blue < RED_B) {
    // move servo1 to sort the red object into the corresponding box
    servo1.write(45);
    delay(1000);
    servo1.write(90);
    delay(1000);
  }
  else if (red < GREEN_R && green > GREEN_G && blue < GREEN_B) {
    // move servo2 to sort the green object into the corresponding box
    servo2.write(45);
    delay(1000);
    servo2.write(90);
    delay(1000);
  }
  else if (red < BLUE_R && green < BLUE_G && blue > BLUE_B) {
    // move servo3 to sort the blue object into the corresponding box
  servo3.write(45);
  delay(1000);
  servo3.write(90);
  delay(1000);
  }}```

I assume that you're pin variable names match the sensors pin names.

Then your code is utter nonsense. It clearly shows that you have not spent a minute reading any documentation on that sensor.

First of all you never set TCS230_S0, or TCS230_S1 high. So the sensor is turned off. You need to set at least one of those pins high in order to configure the output frequency.

Then you turn S2 low (S1 is still low) and S3 high. This configures the sensor so it outputs the blue value. You then attempt to read red and green pulse width from two output pins.

Then you turn the sensor to output green only and attempt to read blue, again by reading the pulsewidth from an output pin.

First of all you need to turn the sensor to the desired output frequency. Then you need to select a colour you want to measure and then you measure the pulse width of the sensors out pin. S0-S3 are for configuration only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM