1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
| #define led 7 //led #define S0 6 //物体表面的反射光越强,TCS3002D内置振荡器产生的方波频率越高, #define S1 5 //S0和S1的组合决定输出信号频率比例因子,比例因子为2% //比率因子为TCS3200传感器OUT引脚输出信号频率与其内置振荡器频率之比 #define S2 4 //S2和S3的组合决定让红、绿、蓝,哪种光线通过滤波器 #define S3 3 #define OUT 2
int g_count = 0; // count the frequecy int g_array[3]; // store the RGB value float g_SF[3]; // save the RGB Scale factor
//初始化 void TSC_Init() { pinMode(led, OUTPUT); pinMode(S0, OUTPUT); pinMode(S1, OUTPUT); pinMode(S2, OUTPUT); pinMode(S3, OUTPUT); pinMode(OUT, INPUT);
//比例 //S0 L S1 H 2% //S0 H S1 L 20% //S0 H S1 H 100% changScaling(0); }
//选择颜色过滤 //滤波类型 //S2 L S3 L 红色 //S2 L S3 H 蓝色 //S2 H S3 L 无 //S2 H S3 H 绿色 void TSC_FilterColor(int Level01, int Level02) { if (Level01 != 0) Level01 = HIGH;
if (Level02 != 0) Level02 = HIGH;
digitalWrite(S2, Level01); digitalWrite(S3, Level02); }
//频率加一 void TSC_Count() { g_count ++ ; }
//读取频率 int readFrequecy(int type) { switch (type) { case 0: Serial.print("->Frequency RED = "); g_count = 0; TSC_FilterColor(LOW, LOW);//Filter without Red delay(1000); //保存起来 g_array[0] = g_count; Serial.println(g_array[0]); return g_array[0]; case 1: Serial.print("->Frequency GREEN = "); g_count = 0; TSC_FilterColor(HIGH, HIGH);//Filter without Green delay(1000); //保存起来 g_array[1] = g_count; Serial.println(g_array[1]); return g_array[1]; case 2: Serial.print("->Frequency BLUE = "); g_count = 0; TSC_FilterColor(LOW, HIGH); delay(1000); //保存起来 g_array[2] = g_count; Serial.println(g_array[2]); return g_array[2]; default: Serial.println("----- No Filter -----"); g_count = 0; TSC_FilterColor(HIGH, LOW); //Clear(no filter) return 0; } }
//读取红绿蓝三个频率 void readAllFrequecy() { readFrequecy(0); readFrequecy(1); readFrequecy(2); readFrequecy(3); }
//改变比例 void changScaling(int type) { if (type == 1) { Serial.println("Change Scaling 20%"); //设置为20% digitalWrite(S0, HIGH); digitalWrite(S1, LOW); } else if (type == 2 ) { Serial.println("Change Scaling 100%"); //设置为100 digitalWrite(S0, HIGH); digitalWrite(S1, HIGH); } else { Serial.println("Change Scaling 2%"); //设置为2% digitalWrite(S0, LOW); digitalWrite(S1, HIGH); } }
//白平衡校验 void whiteBlance() { readAllFrequecy();
g_SF[0] = 255.0 / g_array[0]; //R Scale factor g_SF[1] = 255.0 / g_array[1] ; //G Scale factor g_SF[2] = 255.0 / g_array[2] ; //B Scale factor
Serial.print("RED Scale factor :"); Serial.println(g_SF[0]); Serial.print("GREEN Scale factor :"); Serial.println(g_SF[1]); Serial.print("BLUE Scale factor :"); Serial.println(g_SF[2]); }
//读取颜色 void readColor() { readAllFrequecy(); Serial.print("the color : "); for (int i = 0; i < 3; i++) { int v = int(g_array[i] * g_SF[i]); Serial.print(v > 255 ? 255 : v); if (i < 2) Serial.print(" , "); } Serial.println(""); }
/** 获取红色 */ void getRed() { int v = readFrequecy(0); readFrequecy(3); int red = int(v * g_SF[0]); Serial.print("the color : "); Serial.print(red > 255 ? 255 : red); Serial.println(" , 0 , 0"); }
/** 获取绿色 */ void getGreen() { int v = readFrequecy(1); readFrequecy(3); int green = int(v * g_SF[1]); Serial.print("the color : 0 , "); Serial.print(green > 255 ? 255 : green); Serial.println(" , 0"); }
/** 获取蓝色 */ void getBlue() { int v = readFrequecy(2); readFrequecy(3); int blue = int(v * g_SF[2]); Serial.print("the color : 0 , 0 , "); Serial.print(blue > 255 ? 255 : blue); Serial.println(""); }
void( *resetFunc) (void) = 0;
void setup() { TSC_Init(); Serial.begin(9600);
attachInterrupt(0, TSC_Count, RISING);
for (int i = 0; i < 3; i++) { g_SF[i] = 1; } }
void loop() { //通过串口控制 while (Serial.available() > 0) { int v = Serial.parseInt(); Serial.read(); Serial.print("read value = "); Serial.println(v);
//根据读到的值进行操作 switch (v) { case 0: //进行白平衡 whiteBlance(); break; case 1: //读取颜色 readColor(); break; case 2: //只取红色 getRed(); break; case 3: //只取绿色 getGreen(); break; case 4: //只取蓝色 getBlue(); break; case 11://关灯 Serial.println("led low"); digitalWrite(led, LOW); break; case 12: //开灯 Serial.println("led high"); digitalWrite(led, HIGH); break; case 30:// 2% 的比例 changScaling(0); break; case 31: // 20% 的比例 changScaling(1); break; case 32: // 100% 的比例 changScaling(2); break; default: Serial.println("unknown command"); } }
}
|