第1步:bom
arduino
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值打印到屏幕上。
工信部将开展互联网应用适老化改造专项行动
2022未来品牌评选出炉:李宁、小鹏汽车、Leader等上榜
汽车芯片为什么短缺什么时候能缓解
可穿戴设备下个方向是情感还是健康
2023年在线视频趋势:它们是什么以及它们如何让你受益(下)
颜色传感器DIY图解
维信诺宣布以全新品牌形象加速其在新型显示领域技术研发和产业化布局
爆了!GPT-4模型架构、训练成本、数据集信息都被扒出来了
VoxelMap++:在线LiDAR惯性里程计实现可合并的体素建图方法
怎样让质量流量计发挥高精度作用
科普贴片电感的感值降低的原因
关于SMT生产线对贴片机的要求,从三个方面来解析
舜宇光学将在2021年开始出货iPhone镜头
陶氏有机硅:重新定义消费电子散热
防雷接地网施工综合方案
基于VB语言的机器人足球仿真比赛与对策研究
DEKRA德凯为博科能源的可充电锂离子电池颁发CB证书
研究人员开发出3D打印柔性外骨骼机器人
日本研发“飞龙”无人机,早室内可通常以矩形布局飞行
KUKA系统变量$err说明