-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcs230CleanReading.ino
154 lines (129 loc) · 3.23 KB
/
tcs230CleanReading.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
/*
* Project: Color reading with TCS230 module
* Autor: Pedro Igo Sousa Lucas
* Code written over Danilo Nogueira`s code (http://autocorerobotica.blog.br/utilizando-sensor-de-cor-tcs230-com-arduino/). Access 10/02/2020
*/
// ==================================================================================================*/
// --- Pin connection ---
#define S2 9
#define S3 10
#define OUT 8
// --- Reading tolerance ---
#define tolerance 5
// --- How many colors set. Must be at least 1 ---
#define colorNumber 3
// --- Base delay for color reading ---
#define Delay 50
// ==================================================================================================
// --- Variables ---
// Store each color read by sensor
int red;
int green;
int blue;
// Matrix with RGB values set to each color (Color x RGB)
int color [colorNumber][3] = { 0 };
// ==================================================================================================
void setup()
{
// S2 and S3 tell sensor which color to send over
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
// Sensor returns color value
pinMode(OUT, INPUT);
Serial.begin(9600);
// Starting calibration
calibrate();
}
// ==================================================================================================
void loop()
{
Serial.println("To leave, press any key and enter");
Serial.println("Starting in 3 seconds...");
delay(3000);
loopRead();
calibrate();
}
// ==================================================================================================
// --- Color calibration ---
void calibrate()
{
for(int i = 0; i < colorNumber; i++)
{
Serial.print("Calibrating color ");
Serial.print(i);
Serial.println(". Press any key and enter when ready");
getChar();
getRGB();
color[i][0] = red;
color[i][1] = green;
color[i][2] = blue;
Serial.println("Calibrated.");
}
}
// --- Serial input ---
char getChar()
{
while (Serial.available() == 0)
{}
// Returns uppercase version of any alpha char
return(toupper(Serial.read()));
}
// --- Color reading ---
void getRGB()
{
// Reading filter for RED
digitalWrite(S2,LOW);
digitalWrite(S3,LOW);
// Read pulse in LOW
red = pulseIn(OUT, LOW);
delay(10);
// Reading filter for GREEN
digitalWrite(S2,HIGH);
digitalWrite(S3,HIGH);
green = pulseIn(OUT, LOW);
delay(10);
// Reading filter for blue
digitalWrite(S2, LOW);
digitalWrite(S3,HIGH);
blue = pulseIn(OUT, LOW);
delay(10);
}
// --- Tolerance interval check ---
bool inInterval(int value,int center)
{
if ((value <= center + tolerance) && (value >= center - tolerance))
{
return true;
}
return false;
}
// --- Color identification ---
int readColor()
{
getRGB();
for(int i = 0; i < colorNumber; i++)
{
if (inInterval(red, color[i][0]) &&
inInterval(green, color[i][1]) &&
inInterval(blue, color[i][2]))
{
return i;
}
}
return -1; // No match value
}
// --- Color reading (loop) ---
void loopRead()
{
bool loop = true;
while(loop)
{
Serial.println(readColor());
if (Serial.available() > 0)
{
Serial.read();
loop = false;
}
delay(Delay);
}
}