-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCS3200.ino
192 lines (171 loc) · 3.58 KB
/
TCS3200.ino
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
#define S0 13
#define S1 12
#define S2 9
#define S3 11
#define sensorOut 5
unsigned int Htime_r;
float Freq_r;
float Ttime_r;
float F_array_r[20];
float F_Sum_r;
float F_r;
int i_r=0;
//
unsigned int Htime_g;
float Freq_g;
float Ttime_g;
float F_array_g[20];
float F_Sum_g;
float F_g;
int i_g=0;
//
unsigned int Htime_b;
float Freq_b;
float Ttime_b;
float F_array_b[20];
float F_Sum_b;
float F_b;
int i_b=0;
//
char c;
float Ftot;
float coeff_r;
float coeff_g;
float coeff_b;
float soglia;
bool calibration=false;
float R;
float G;
float B;
bool sat=false;
void setup() {
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(6, OUTPUT);
pinMode(4, OUTPUT);
pinMode(sensorOut, INPUT);
digitalWrite(S0,LOW);
digitalWrite(S1,HIGH);
Serial.begin(9600);
}
void loop() {
Measure();
if(Serial.available()!=0){
c=Serial.read();
if (c=='c') Calibration();
}
if(calibration){
if(!sat){
R=F_Sum_r*coeff_r;
G=F_Sum_g*coeff_g;
B=F_Sum_b*coeff_b;
Serial.print("R: ");
Serial.print(R);
Serial.print(" ");
Serial.print("G: ");
Serial.print(G);
Serial.print(" ");
Serial.print("B: ");
Serial.println(B);
}
else{
sat=false;
}
}
}
void ChangeFilter(String filter){
if (filter=="red"){
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
}
if (filter=="green"){
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
}
if (filter=="clear"){
digitalWrite(S2, HIGH);
digitalWrite(S3, LOW);
}
if (filter=="blue"){
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
}
}
void Calibration() {
int j;
Serial.println("Put the white paper on the sensor!");
delay(3000);
Serial.println("Calibration..");
delay(1000);
Ftot=F_Sum_b+F_Sum_r+F_Sum_g;
coeff_r=(33.33)/F_Sum_r;
coeff_g=(33.33)/F_Sum_g;
coeff_b=(33.33)/F_Sum_b;
calibration=true;
}
void Measure() {
noInterrupts();
ChangeFilter("red");
Htime_r = pulseIn(sensorOut,HIGH);
Ttime_r = Htime_r *2.0;
Freq_r = 1000/Ttime_r;
F_array_r[i_r] = Freq_r;
if (i_r < 20) i_r++;
else i_r = 0;
F_Sum_r=0;
for (int j=0; j<20; j++) {
F_Sum_r += F_array_r[j];
}
F_Sum_r = F_Sum_r / 20;
if(F_Sum_r >12) {
Serial.println("Saturation error, take distance from the sensor.");
digitalWrite(4,HIGH);
sat=true;
}
else{
digitalWrite(4,LOW);
}
ChangeFilter("green");
Htime_g = pulseIn(sensorOut,HIGH);
Ttime_g = Htime_g *2.0;
Freq_g = 1000/Ttime_g;
F_array_g[i_g] = Freq_g;
if (i_g < 20) i_g++;
else i_g = 0;
F_Sum_g=0;
for (int j=0; j<20; j++) {
F_Sum_g += F_array_g[j];
}
F_Sum_g = F_Sum_g / 20;
if(F_Sum_g>12) {
Serial.println("Saturation error, take distance from the sensor.");
digitalWrite(4,HIGH);
sat=true;
}
else{
digitalWrite(4,LOW);
}
ChangeFilter("blue");
Htime_b = pulseIn(sensorOut,HIGH);
Ttime_b = Htime_b *2.0;
Freq_b = 1000/Ttime_b;
F_array_b[i_b] = Freq_b;
if (i_b < 20) i_b++;
else i_b = 0;
F_Sum_b=0;
for (int j=0; j<20; j++) {
F_Sum_b += F_array_b[j];
}
F_Sum_b = F_Sum_b / 20;
if(F_Sum_b>12) {
Serial.println("Saturation error, take distance from the sensor.");
digitalWrite(4,HIGH);
sat=true;
}
else{
digitalWrite(4,LOW);
}
interrupts();
}