|
green.png (14.22 KB, 下載次數: 155)
下載附件
檢測這張圖穩穩的
2017-4-18 16:32 上傳
//測試顏色識別模塊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);
} |
|