找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3799|回复: 1
打印 上一主题 下一主题
收起左侧

TSC3200快速检测RGB 直接上单片机代码

[复制链接]
跳转到指定楼层
楼主

//测试颜色识别模块TCS3200
const int s0 = 8;
const int s1 = 9;
const int s2 = 12;
const int s3 = 11;
const int out = 10;
   
//RGB颜色色值
int red = 0;
int green = 0;
int blue = 0;

//信号灯
int inforLED = 13;

String gifKey ="01020121010102";
byte keyCursor = 0;  //核对密码的游标
byte currentColor = -1; //扫描得到的颜色
   
void setup()   
{  
  pinMode(s0, OUTPUT);  
  pinMode(s1, OUTPUT);  
  pinMode(s2, OUTPUT);  
  pinMode(s3, OUTPUT);
  pinMode(inforLED,OUTPUT);
  pinMode(out, INPUT);   
  Serial.begin(9600);  
  digitalWrite(s0, HIGH);  
  digitalWrite(s1, HIGH);  
}  
   
void loop()
{  
  color();
  //输出RGB各色值
  Serial.print("Red:");  
  Serial.print(red, DEC);  
  Serial.print("Green:");  
  Serial.print(green, DEC);  
  Serial.print("Blue:");  
  Serial.print(blue, DEC);  
  Serial.println();  

  //检验结果是否红色
  if (red < blue && red < green && red > 30 && red <1000)  
  {  
//   Serial.println("Red 0");
   currentColor = 0;
  } else if (blue < red && blue < green && blue > 30 && blue <1000) { //检验结果是否绿色  
//   Serial.println("Blue 2");
   currentColor = 2;
  } else if (green < red && green < blue && green > 30 && green <1000) { //检验结果是否蓝色  
//   Serial.println("Green 1");
   currentColor = 1;   
  }
  Serial.print("currentColor:");
  Serial.print(currentColor);
  Serial.print(" ");
  
   if(currentColor == (byte(gifKey[keyCursor])-48) ) {
     keyCursor ++;
   } else {
    if(keyCursor == 0) {
    } else {
        if(currentColor == (byte(gifKey[keyCursor-1])-48) ) {
        }  else {
          keyCursor = 0;
        }
    }
   }
//  if(currentColor == gifKey[keyCursor+1]) {
//    keyCursor ++;  
//  } else {
//    if(keyCursor == -1) {
//    } else {
//      if(currentColor == gifKey[keyCursor]) {
//      } else {
//        keyCursor = -1;
//      }  
//    }
//  }
Serial.print("keyCursor:");
Serial.println(keyCursor);
  if(keyCursor == 14) {
    digitalWrite(inforLED,HIGH);
    Serial.println("open lock successfully!");
    delay(3000);
    digitalWrite(inforLED,LOW);
  }

  
  Serial.println();  
  //延时两秒后关闭所有LED
}  
   
void color()  
{  
  //设置好S2、S3端口,准备读取颜色值
  digitalWrite(s2, LOW);  
  digitalWrite(s3, LOW);  
  //红色光RED
  red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);  
  digitalWrite(s3, HIGH);  
  //蓝色光BLUE  
  blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);  
  digitalWrite(s2, HIGH);  
  //绿色光GREEN  
  green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);  
}
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:224947 发表于 2017-8-5 13:05 | 只看该作者
你好,请问你这个red、green、blue直接是RGB吗?好像范围并不在0-255之间
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表