5518| 1
|
[进阶] 会说话的颜色传感器TCS3200 |
颜色识别传感器TCS3200作为TCS230的升级版,尺寸小,应用方便, 广泛应用于智能玩具,教育类机器人,手机等智能硬件领域今天介绍这个项目是把它对着一个物体,然后它会语音播报出物体颜色。颜色MP3文件是事先录好放在DFPlayer mini的SD卡里的。首先建立一个mp3文件夹,让后放入001.MP3,002.mp3等。 [mw_shl_code=applescript,true] #include <SoftwareSerial.h> #include <DFPlayer_Mini_Mp3.h> int s0=3,s1=4,s2=5,s3=6; int flag=0; int counter=0; int countR=0,countG=0,countB=0; void setup() { Serial.begin(9600); mp3_set_serial (Serial); //set Serial for DFPlayer-mini mp3 module mp3_set_volume (25); delay (400); pinMode(s0,OUTPUT); pinMode(s1,OUTPUT); pinMode(s2,OUTPUT); pinMode(s3,OUTPUT); } void TCS() { digitalWrite(s1,HIGH); digitalWrite(s0,LOW); flag=0; attachInterrupt(0, ISR_INTO, CHANGE); timer2_init(); } void ISR_INTO() { counter++; } void timer2_init(void) { TCCR2A=0x00; TCCR2B=0x07; //the clock frequency source 1024 points TCNT2= 100; //10 ms overflow again TIMSK2 = 0x01; //allow interrupt } int i=0; ISR(TIMER2_OVF_vect)//the timer 2, 10ms interrupt overflow again. Internal overflow interrupt executive function { TCNT2=100; flag++; if(flag==1) { counter=0; } else if(flag==2) { digitalWrite(s2,LOW); digitalWrite(s3,LOW); countR=counter/1.051; Serial.print("red="); Serial.println(countR,DEC); digitalWrite(s2,HIGH); digitalWrite(s3,HIGH); } else if(flag==3) { countG=counter/1.0157; Serial.print("green="); Serial.println(countG,DEC); digitalWrite(s2,LOW); digitalWrite(s3,HIGH); } else if(flag==4) { countB=counter/1.114; Serial.print("blue="); Serial.println(countB,DEC); digitalWrite(s2,LOW); digitalWrite(s3,LOW); } else { flag=0; TIMSK2 = 0x00; } counter=0; delay(2); } void loop() { delay(10); TCS(); if((countR>10)||(countG>10)||(countB>10)) { if((countR>countG)&&(countR>countB)) { Serial.print("red"); Serial.print("\n"); delay(1000); mp3_play (1); delay (200); } else if((countG>=countR)&&(countG>countB)) { Serial.print("green"); Serial.print("\n"); delay(1000); mp3_play (2); delay (200); } else if((countB>countG)&&(countB>countR)) { Serial.print("blue"); Serial.print("\n"); delay(1000); mp3_play (3); delay (200); } } else { delay(1000); } } [/mw_shl_code] |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed