上次的TCS230小项目大家还玩的开心么,今天我们带来了它的进阶版,如何做出一个Arduino颜色分拣机。话不多说,让我们直接进入正题。
Arduino颜色分拣机
这个Arduino项目所需的是一个颜色传感器(TCS3200)和两个业余伺服电机,这使该项目非常简单容易上手,构建起来十分有趣。首先,我们使用Solidworks 3D建模软件设计出颜色分拣机的大致模型,其工作原理如下:
最初,装在充电器中的彩虹球掉进安装在顶部伺服电机上的平台中
然后伺服电机旋转,并将彩虹球带到检测其颜色的颜色传感器。
构建Arduino颜色分拣机
我们在这里使用的材料是3毫米刻度纤维板。根据图纸在纤维板上重新绘制零件,并使用小型手锯将所有零件切成一定尺寸。
准备好所有零件后,我们便可以开始组装它们了。首先,用胶枪组装好外部的零件。
然后,使用粘性较好一点的胶水将两个伺服电机粘合在它们的平台上,并将它们附加到组件上。
之后,再次使用胶水,将导轨安装在底部伺服电机上,并将顶部伺服电机所需的支撑和平台固定在上面。
接下来,我们插入用于通过5V适配器为Arduino供电的开关和电源插孔,接着在第三个平台上,我们插入颜色传感器。
组件的连接电路原理就如下图所示:
接下来,我们已经离成功不远了,在组装前我们得先需要对Arduino进行编程,然后在进行组装
Arduino颜色分拣器代码
/* Arduino Project - Color Sorting Machine
*
* by Dejan Nedelkovski, www.HowToMechatronics.com
*
*/
#include
#define S0 2
#define S1 3
#define S2 4
#define S3 5
#define sensorOut 6
Servo topServo;
Servo bottomServo;
int frequency = 0;
int color=0;
void setup() {
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(sensorOut, INPUT);
// Setting frequency-scaling to 20%
digitalWrite(S0, HIGH);
digitalWrite(S1, LOW);
topServo.attach(7);
bottomServo.attach(8);
Serial.begin(9600);
}
void loop() {
topServo.write(115);
delay(500);
for(int i = 115; i > 65; i--) {
topServo.write(i);
delay(2);
}
delay(500);
color = readColor();
delay(10);
switch (color) {
case 1:
bottomServo.write(50);
break;
case 2:
bottomServo.write(75);
break;
case 3:
bottomServo.write(100);
break;
case 4:
bottomServo.write(125);
break;
case 5:
bottomServo.write(150);
break;
case 6:
bottomServo.write(175);
break;
case 0:
break;
}
delay(300);
for(int i = 65; i > 29; i--) {
topServo.write(i);
delay(2);
}
delay(200);
for(int i = 29; i
topServo.write(i);
delay(2);
}
color=0;
}
// Custom Function - readColor()
int readColor() {
// Setting red filtered photodiodes to be read
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
// Reading the output frequency
frequency = pulseIn(sensorOut, LOW);
int R = frequency;
// Printing the value on the serial monitor
Serial.print("R= ");//printing name
Serial.print(frequency);//printing RED color frequency
Serial.print(" ");
delay(50);
// Setting Green filtered photodiodes to be read
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
// Reading the output frequency
frequency = pulseIn(sensorOut, LOW);
int G = frequency;
// Printing the value on the serial monitor
Serial.print("G= ");//printing name
Serial.print(frequency);//printing RED color frequency
Serial.print(" ");
delay(50);
// Setting Blue filtered photodiodes to be read
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
// Reading the output frequency
frequency = pulseIn(sensorOut, LOW);
int B = frequency;
// Printing the value on the serial monitor
Serial.print("B= ");//printing name
Serial.print(frequency);//printing RED color frequency
Serial.println(" ");
delay(50);
if(R32 & G55){
color = 1; // Red
}
if(G43 & B35){
color = 2; // Orange
}
if(R40 & G40){
color = 3; // Green
}
if(R24 & G30){
color = 4; // Yellow
}
if(R46 & G55){
color = 5; // Brown
}
if (G45 & B26){
color = 6; // Blue
}
return color;
}
因此,我们需要“ Servo.h”库,定义颜色传感器将连接到的引脚,创建伺服对象并声明程序所需的一些变量。在设置部分,我们需要将引脚定义为输出和输入,设置颜色传感器的频率缩放比例,定义伺服引脚,并开始串行通信以打印在串行监视器上读取的颜色结果。
在循环部分,我们的程序开始于将顶部伺服电机移至彩虹球的位置。请注意,此值115适用于我的零件和我的伺服电机,因此您应根据自己的情况调整此值以及伺服电机的以下值。
接下来,使用“ for”循环,我们将旋转并将彩虹球移至颜色传感器的位置。我们使用“ for”循环,以便我们可以通过更改循环中的延迟时间来控制旋转速度。
接下来,经过半秒的延迟,使用定制函数readColor(),我们将读取彩虹球的颜色。这是自定义函数的代码。使用颜色传感器的四个控制引脚和频率输出引脚,我们可以读取彩虹球的颜色。传感器为每个彩虹球,红色,绿色和蓝色读取3个不同的值,并根据这些值告诉我们实际的颜色是什么。
请注意,这些值可能会有所不同,因为传感器并不总是准确的。因此,使用这些“ if”语句,我们可以使传感器针对特定颜色的测试值的误差约为+ -5。因此,例如,如果我们有一个红色彩虹球,则第一个“ if”语句为true,变量“ color”将获得值1。这就是readColor()自定义函数的工作,然后使用“ switch-case”声明我们将底部伺服器旋转到特定位置。最后,我们进一步旋转顶部伺服电机,直到彩虹球掉入导轨中,然后再次将其送回初始位置,以便可以重复此过程。
上传完代码后,我们将Arduino开发板用胶枪固定住,即可大功告成。
关注风火轮,技术之路常相伴,我们下期见!
领取专属 10元无门槛券
私享最新 技术干货