-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcolor_recognizer.ino
128 lines (106 loc) · 3.41 KB
/
color_recognizer.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
/*********
Rui Santos
Complete project details at http://randomnerdtutorials.com
*********/
// TCS230 or TCS3200 pins wiring to Arduino
#define S0 4
#define S1 5
#define S2 6
#define S3 7
#define sensorOut 8
// Stores frequency read by the photodiodes
int redFrequency = 0;
int greenFrequency = 0;
int blueFrequency = 0;
// Stores the red. green and blue colors
int redColor = 0;
int greenColor = 0;
int blueColor = 0;
void setup() {
// Setting the outputs
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
// Setting the sensorOut as an input
pinMode(sensorOut, INPUT);
// Setting frequency scaling to 20%
digitalWrite(S0,HIGH);
digitalWrite(S1,LOW);
// Begins serial communication
Serial.begin(9600);
}
void loop() {
// Setting RED (R) filtered photodiodes to be read
digitalWrite(S2,LOW);
digitalWrite(S3,LOW);
// Reading the output frequency
redFrequency = pulseIn(sensorOut, LOW);
// Remaping the value of the RED (R) frequency from 0 to 255
// You must replace with your own values. Here's an example:
// redColor = map(redFrequency, 70, 120, 255,0);
//redColor = map(redFrequency, XX, XX, 255,0);
// Printing the RED (R) value
//Serial.print("R = ");
//Serial.print(redColor);
delay(50);
// Setting GREEN (G) filtered photodiodes to be read
digitalWrite(S2,HIGH);
digitalWrite(S3,HIGH);
// Reading the output frequency
greenFrequency = pulseIn(sensorOut, LOW);
// Remaping the value of the GREEN (G) frequency from 0 to 255
// You must replace with your own values. Here's an example:
// greenColor = map(greenFrequency, 100, 199, 255, 0);
//greenColor = map(greenFrequency, XX, XX, 255, 0);
// Printing the GREEN (G) value
//Serial.print(" G = ");
//Serial.print(greenColor);
delay(50);
// Setting BLUE (B) filtered photodiodes to be read
digitalWrite(S2,LOW);
digitalWrite(S3,HIGH);
// Reading the output frequency
blueFrequency = pulseIn(sensorOut, LOW);
// Remaping the value of the BLUE (B) frequency from 0 to 255
// You must replace with your own values. Here's an example:
// blueColor = map(blueFrequency, 38, 84, 255, 0);
//blueColor = map(blueFrequency, XX, XX, 255, 0);
// Printing the BLUE (B) value
//Serial.print(" B = ");
//Serial.print(blueColor);
delay(50);
// Checks the current detected color and prints
// a message in the serial monitor
if (redFrequency > 70 && greenFrequency < 100 && greenFrequency > 50 && blueFrequency >= 26) {
Serial.println("Green");
}
else if (redFrequency > 70 && greenFrequency < 90 && greenFrequency > 50 && blueFrequency <26) {
Serial.println("Blue");
}
else if (redFrequency < 40 && greenFrequency > 95 && blueFrequency < 26) {
Serial.println("Red");
}
else if (redFrequency < 26 && greenFrequency < 40 && blueFrequency < 20) {
Serial.println("Yellow");
}
else {
Serial.print("Color Not Found, ");
Serial.print("R = ");
Serial.print(redFrequency);
Serial.print(", G = ");
Serial.print(greenFrequency);
Serial.print(", B = ");
Serial.println(blueFrequency);
}
/*
if(redColor > greenColor && redColor > blueColor){
Serial.println(" - RED detected!");
}
if(greenColor > redColor && greenColor > blueColor){
Serial.println(" - GREEN detected!");
}
if(blueColor > redColor && blueColor > greenColor){
Serial.println(" - BLUE detected!");
}*/
}