繁体   English   中英

coding 3色选机带3个伺服电机和1个tcs230传感器带c++ on arduino

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

我现在在我的项目中,我需要对具有不同 colors 的物体进行分类。我将把所有物体放在传送带上,传感器将读取颜色,传送带会将 object 传送到下一个放置盒子的位置传送带和我安装的伺服电机,用于将 object 弹入盒子。 我发现代码无法检测物体的颜色(我已经检查过传感器是否正常工作)。

#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);
  }}```

我假设您的引脚变量名称与传感器引脚名称匹配。

那么你的代码完全是胡说八道。 它清楚地表明您没有花一分钟时间阅读有关该传感器的任何文档。

首先,您永远不要将 TCS230_S0 或 TCS230_S1 设置为高电平。 所以传感器被关闭。 您需要将至少其中一个引脚设置为高电平才能配置 output 频率。

然后将 S2 调低(S1 仍为低)并将 S3 调高。 这将配置传感器,使其输出蓝色值。 然后您尝试从两个 output 引脚读取红色和绿色脉冲宽度。

然后,您将传感器转为仅 output 绿色并尝试读取蓝色,再次通过从 output 引脚读取脉冲宽度。

首先,您需要将传感器调到所需的 output 频率。 然后你需要 select 你想要测量的颜色然后你测量传感器输出引脚的脉冲宽度。 S0-S3 仅用于配置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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