|
green.png (14.22 KB, 下載次數(shù): 155)
下載附件
檢測(cè)這張圖穩(wěn)穩(wěn)的
2017-4-18 16:32 上傳
//測(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);
} |
|