9. program that reads the color sensor TCS3210 / 3200 through changes in frequency,
10. when it detects the red, green and blue colors , read dye, luminance and RGB
11. program adapted from those published by the manufacturer , Parallax , Taos and stratnum.fr
12 .
13 .
14. settings xxxxxxxxxxxxxxx scale or frequency settings for color detection
15. S0 | S1 xx S2 | S3
16. 0 | 0 power down xx 0 | 0 network
17. 0 | 1 1/50 scale 10Khz xx fre FO = 0 | 1 blue
18. 1 | 0 scaling 1/5 100Khz xx fre FO = 1 | 0 clear ( no filter)
19. 1 | 1 1/1 scaling (default ) fre FO = 500Khz xx 1 | 1 green
20 .
21. s3 s2 out vdd
22. 8 7 6 5
23. I____I____I____I
24. I I
25. I I
26. I_*____________I
27. I I I I
28. 1 2 3 4
29. S0 S1 OE GND
30.
31. * /
32. // Arduino pins to connect the sensor
33. // tsl = pitch ( dye) saturation luminance
34. // dye refers to a solid or flat tone ,
35. // ie containing no degradation.
36. TSL struct {double t; double s ; double l ; } ; // a standard function structure that allows us
to define and take defined
several variables
37. int out = 2; // izlaz
38. int S0 = 3;
39. int S1 = 4;
40. int S2 = 5;
41. int S3 = 6;
42. int OE = 7; // output enable
43. int LED = 8; // pulse activation of an LED for color detection (optional )
45. int LT = 13; // led 13
46. int sombras_nb = 6;
47. double sombras_val[] = {30.0,60.0,120.0,180.0,240.0,330.0};
48. char* sombras_id[] = {"rojo","amarillo","verde","cyan","azul","magenta"};
49.
50. void setup() {
51. TCS3210setup();//salta a configurar el sensor
52. Serial.begin(115200);//configura la velocidad
53. Serial.print("\nleyendo sensor 3210\n");//imprime un mensaje
54. delay(100);
55. }
56.
57. void loop() {
58. struct TSL tsl = detectorColor();//toma los datos de la funcion de los colores y lo
almacena en struct
59. double t = tsl.t;
60.
61. Serial.println();
62. // esta escala es aproximada al valor real. esto depende de la cantidad de luz que incida
en el sensor
63. if (tsl.l < 38.0) { Serial.println("tendencia: negro"); }
64. else if (tsl.s < 4.0 && tsl.l > 60.0) { Serial.println("tendencia: blanco"); }
65. else if (tsl.s < 4.2 && tsl.l > 40.0) { Serial.println("tendencia: gris"); }
66. else // dterminacion de la tinta
67. if(t>=sombras_val[0] && t<sombras_val[sombras_nb-1])
68. {
69. for(int idx=0; idx<sombras_nb-1; ++idx)
70. {
71. if(t>=sombras_val[idx] && t<sombras_val[idx+1])
72. {
73. Serial.print("tendencia: ");
74. Serial.println(sombras_id[idx+1]);
75. break;
76. }
77. }
78. }else{
79. Serial.println("tendencia: rojo");
80. }
81.
82. Serial.println("\n\n");
83. delay(1000);
84. }
85.
86. // comando para detectar si hay color alfrente del sensor.
87. bool isPresent()
88. {
89. //se cataloga el grado de luminosidad segun la hoja de datos
90. //para determinar el grado de luminosdad o oscuridad
91. double isPresentTolerance = 2;
92. // nmero se hace grande cuando algo est en frente del sensor.
93. double isPresent = colorRead(out,0,0)/colorRead(out,0,1);
94.
95. if(isPresent < isPresentTolerance){
96. Serial.println("no hay nada en frente del sensor");
97. return false;
98. }
99. return true;
100. }
101.
102. // retorna TSL segun color detectado
103. //
104. struct TSL detectorColor(){
105. struct TSL tsl;
106. double white = colorRead(out,0,1);//out es el pin 2 del arduino
107. double red = colorRead(out,1,1);
108. double blue = colorRead(out,2,1);
109. double green = colorRead(out,3,1);
110. Serial.print("datos capturados blanco : "); Serial.println(white);
111. Serial.print("datos capturados R : "); Serial.println(red);
112. Serial.print("datos capturados G : "); Serial.println(green);
113. Serial.print("datos capturados B : "); Serial.println(blue);Serial.println();
114.
115. double r,v,b; // r,v,b [0..1]
116. double offset = 3.0/white; // compensacion de luminosidad
117. r=min(1.0, offset+(white/red));
118. v=min(1.0, offset+(white/green));
119. b=min(1.0, offset+(white/blue));
120. Serial.print(" Rojo : "); Serial.println( r);
121. Serial.print(" verde : "); Serial.println( v);
122. Serial.print(" azul : "); Serial.println( b);
123. Serial.println();
124.
125. // transformacion RVB -> TSL
126. // r,v,b [0..1]
127. // t [0..360]; s,l [%]
128. double t,s,l;
129.
130. double maxRVB = max(max(r, b),v);
131. double minRVB = min(min(r, b),v);
132. double delta = maxRVB-minRVB;
133. double somme = maxRVB+minRVB;
134.
135. // luminancia basada en valores de colores segun software de diseo grafico
136. l=(somme/2.0);
137.
138. if(delta==0.0) // gris
139. {
140. t=s=0.0;
141. }else{
142. //saturacion
143. if ( l < 0.5 ) s = delta / somme;
144. else s = delta / ( 2.0 - delta );
145.
146. // tinte
147. double del_R = ( ( ( maxRVB - r ) / 6.0 ) + ( delta / 2.0 ) ) / delta;
148. double del_G = ( ( ( maxRVB - v ) / 6.0 ) + ( delta / 2.0 ) ) / delta;
149. double del_B = ( ( ( maxRVB - b ) / 6.0 ) + ( delta / 2.0 ) ) / delta;
150.
151. if ( r == maxRVB ) t = del_B - del_G;
152. else if ( v == maxRVB ) t = ( 1.0 / 3.0 ) + del_R - del_B;
153. else if ( b == maxRVB ) t = ( 2.0 / 3.0 ) + del_G - del_R;
154.
155. if ( t < 0 ) t += 1.0;
156. if ( t > 1 ) t -= 1.0;
157. }
158.
159. // normalizacion
160. t*=360.0; // [0..360]
161. s*=100.0; // [%]
162. l*=100.0; // [%]
163. tsl.t=t; tsl.s=s; tsl.l=l;
164.
165. Serial.print(" tinte [0..360]: "); Serial.println(t);
166. Serial.print(" saturacion [%]: "); Serial.println(s);
167. Serial.print(" luminancia [%]: "); Serial.println(l);
168.
222. */
223. void setMode(int mode){
224. if(mode == 0){
225. //power OFF
226. digitalWrite(LED, LOW);
227. digitalWrite(LT, LOW);
228. digitalWrite(S0, LOW); //S0
229. digitalWrite(S1, LOW); //S1
230. }else if(mode == 1){
231. //escala 1:1
232. digitalWrite(S0, HIGH); //S0
233. digitalWrite(S1, HIGH); //S1
234. }else if(mode == 2){
235. //escala 1:5
236. digitalWrite(S0, HIGH); //S0
237. digitalWrite(S1, LOW); //S1
238. }else{ //if(mode == 3)
239. //escala 1:50
240. digitalWrite(S0, LOW); //S0
241. digitalWrite(S1, HIGH); //S1
242. }
243. }
244. ///////////////////funcion de configuracion de pines/////////////////
245. void TCS3210setup(){
246. //configuracion de frecuencia en el setup estan los pines respectivos
247. pinMode(S0,OUTPUT);
248. pinMode(S1,OUTPUT);
249. //seleccion de color
250. pinMode(S2,OUTPUT);
251. pinMode(S3,OUTPUT);
252. //salida
253. pinMode(out, INPUT);
254. //habilita la salida
255. pinMode(OE,OUTPUT); //habilita el modulo de luz
256. // activa el led
257. pinMode(LED,OUTPUT);
258. // halita la deteccion de color
259. pinMode(LT,OUTPUT);
260. }