第1步:BoM
RGB LED
10kΩ電阻
3x100Ω電阻器
跳線
面包板電線
步驟2:連接RGB LED
這將是我們電路的發射器部分發出不同的顏色,這些顏色將從物體反彈,通過光學定律將被檢測到我們的光傳感器。
*將引腳2,最長引腳連接到Arduino上的GND引腳。
*連接引腳1, R GB的紅色LED LED指向Arduino上的引腳5。
*將引腳3,R G B LED的貪婪色LED連接到Arduino上的引腳6。
*將引腳4,RG B LED的藍色LED連接到Arduino上的引腳9。
您會注意到所有這些都插入標有tilda符號“?”的PWM引腳,這樣我們就可以獨立控制每個LED的亮度。
步驟3:連接光電傳感器
來自發射器(RGB)LED的反射光從中彈回光傳感器將讀取任何物體,光電傳感器將使用校準值來找到特定顏色的各個RGB顏色值。
確保將光傳感器移近發射器。
*將其中一個引腳(稱為光纖傳感器的引腳1)連接到Arduino上的GND引腳
*將光電傳感器的引腳2連接到上面的3.3V引腳Arduino的。
*將光電傳感器的引腳2連接到Arduino上的A0引腳。
你會注意到最后兩條接線都是平行的。這是因為我們正在制作一個分壓器,以便在反射光強度發生變化時獲得變化的電壓讀數。
步驟4:代碼
// Define colour sensor LED pins int ledArray[] = {5,6,9}; // boolean to know if the balance has been set
boolean balanceSet = false; //place holders for colour detected
int red = 0;
int green = 0;
int blue = 0; //floats to hold colour arrays
float colourArray[] = {0,0,0};
float whiteArray[] = {0,0,0};
float blackArray[] = {0,0,0}; //place holder for average
int avgRead; void setup(){
//setup the outputs for the colour sensor
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
//begin serial communication
Serial.begin(9600); }
void loop(){ checkBalance();
checkColour();
printColour();
}
void checkBalance(){
//check if the balance has been set, if not, set it
if(balanceSet == false){
setBalance();
}
}
void setBalance(){
//set white balance
delay(5000); //delay for five seconds, this gives us time to get a white sample in front of our sensor
//scan the white sample.
//go through each light, get a reading, set the base reading for each colour red, green, and blue to the white array
for(int i = 0;i《=2;i++){
digitalWrite(ledArray[i],HIGH);
delay(100);
getReading(5); //number is the number of scans to take for average, this whole function is redundant, one reading works just as well.
whiteArray[i] = avgRead;
digitalWrite(ledArray[i],LOW);
delay(100);
}
//done scanning white, now it will pulse blue to tell you that it is time for the black (or grey) sample.
//set black balance
delay(5000); //wait for five seconds so we can position our black sample
//go ahead and scan, sets the colour values for red, green, and blue when exposed to black
for(int i = 0;i《=2;i++){
digitalWrite(ledArray[i],HIGH);
delay(100);
getReading(5);
blackArray[i] = avgRead;
//blackArray[i] = analogRead(2);
digitalWrite(ledArray[i],LOW);
delay(100);
}
//set boolean value so we know that balance is set
balanceSet = true;
delay(5000); //delay another 5 seconds to let us catch up
} void checkColour(){
for(int i = 0;i《=2;i++){
digitalWrite(ledArray[i],HIGH); //turn or the LED, red, green or blue depending which iteration
delay(100); //delay to allow CdS to stabalize, they are slow
getReading(5); //take a reading however many times
colourArray[i] = avgRead; //set the current colour in the array to the average reading
float greyDiff = whiteArray[i] - blackArray[i]; //the highest possible return minus the lowest returns the area for values in between
colourArray[i] = (colourArray[i] - blackArray[i])/(greyDiff)*255; //the reading returned minus the lowest value divided by the possible range multiplied by 255 will give us a value roughly between 0-255 representing the value for the current reflectivity(for the colour it is exposed to) of what is being scanned
digitalWrite(ledArray[i],LOW); //turn off the current LED
delay(100);
}
}
void getReading(int times){
int reading;
int tally=0;
//take the reading however many times was requested and add them up
for(int i = 0;i 《 times;i++){
reading = analogRead(0);
tally = reading + tally;
delay(10);
}
//calculate the average and set it
avgRead = (tally)/times;
}
//prints the colour in the colour array, in the next step, we will send this to processing to see how good the sensor works.
void printColour(){
Serial.print(“R = ”);
Serial.println(int(colourArray[0]));
Serial.print(“G = ”);
Serial.println(int(colourArray[1]));
Serial.print(“B = ”);
Serial.println(int(colourArray[2]));
//delay(2000);
}
步驟5:校準
首先準備一張黑白紙上傳代碼。
上傳代碼后,您會注意到在程序運行的前5秒內,RGB LED會發出各種顏色。在前5秒鐘,在LED和光電傳感器上放置一張黑紙。然后在接下來的5秒鐘內將紙張切換到白紙上。
編寫代碼,使前10秒為校準周期。
第6步:測試并享受!
取出不同顏色的紙張并進行測試。它會將各個R,G,B值打印到屏幕上。
-
顏色傳感器
+關注
關注
2文章
95瀏覽量
18171
發布評論請先 登錄
相關推薦
評論