找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 4174|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

TSC3200快速檢測(cè)RGB 直接上單片機(jī)代碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主

//測(cè)試顏色識(shí)別模塊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;

//信號(hào)燈
int inforLED = 13;

String gifKey ="01020121010102";
byte keyCursor = 0;  //核對(duì)密碼的游標(biāo)
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();  

  //檢驗(yàn)結(jié)果是否紅色
  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) { //檢驗(yàn)結(jié)果是否綠色  
//   Serial.println("Blue 2");
   currentColor = 2;
  } else if (green < red && green < blue && green > 30 && green <1000) { //檢驗(yàn)結(jié)果是否藍(lán)色  
//   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();  
  //延時(shí)兩秒后關(guān)閉所有LED
}  
   
void color()  
{  
  //設(shè)置好S2、S3端口,準(zhǔn)備讀取顏色值
  digitalWrite(s2, LOW);  
  digitalWrite(s3, LOW);  
  //紅色光RED
  red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);  
  digitalWrite(s3, HIGH);  
  //藍(lán)色光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空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:224947 發(fā)表于 2017-8-5 13:05 | 只看該作者
你好,請(qǐng)問你這個(gè)red、green、blue直接是RGB嗎?好像范圍并不在0-255之間
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表