-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRGB_Check.c
More file actions
158 lines (128 loc) · 3.23 KB
/
Copy pathRGB_Check.c
File metadata and controls
158 lines (128 loc) · 3.23 KB
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
#include <Wire.h>
#include "Adafruit_TCS34725.h"
// set to false if using a common cathode LED
#define commonAnode true
#define NONE 0
#define RED_COLOR 1
#define GREEN_COLOR 2
#define BLUE_COLOR 3
#define YELLOW_COLOR 4
#define WHITE_COLOR 5
uint8_t colorDetected;
// our RGB -> eye-recognized gamma color
byte gammatable[256];
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_24MS, TCS34725_GAIN_4X);
void setup() {
Serial.begin(9600);
Serial.println("Color View Test!");
pinMode(13,OUTPUT);
if (tcs.begin()) {
Serial.println("Found sensor");
} else {
Serial.println("No TCS34725 found ... check your connections");
while (1); // halt!
}
// thanks PhilB for this gamma table!
// it helps convert RGB colors to what humans see
for (int i=0; i<256; i++) {
float x = i;
x /= 255;
x = pow(x, 2.5);
x *= 255;
if (commonAnode) {
gammatable[i] = 255 - x;
} else {
gammatable[i] = x;
}
//Serial.println(gammatable[i]);
}
}
uint32_t t0;
uint32_t rgbHigh;
uint32_t rgbLow;
uint16_t clear, red, green, blue;
void loop() {
t0 = millis();
tcs.setInterrupt(false); // turn on LED
delay(30); // takes 50ms to read
tcs.getRawData(&red, &green, &blue, &clear);
tcs.setInterrupt(true); // turn off LED
if (red < 75) {
red = 0;
} else {
red -=75;
}
if (green < 40) {
green = 0;
} else {
green -= 40;
}
// Serial.print("C:\t"); Serial.print(clear);
Serial.println(String(blue) + "\t" + String(red) + "\t" + String(green) + "\t0");
rgbHigh = max(red, max(green, blue));
rgbLow = min(red, min(green,blue));
if (rgbHigh < 100) {
colorDetected = NONE;
} else if (rgbHigh == red) {
if (green < .9*red) {
colorDetected = RED_COLOR;
} else {
colorDetected = YELLOW_COLOR;
}
} else if (rgbHigh == green) {
if (red < .9*green) {
colorDetected = GREEN_COLOR;
} else {
colorDetected = YELLOW_COLOR;
}
} else if (rgbHigh == blue) {
if (green < .8*blue){
colorDetected = BLUE_COLOR;
} else {
colorDetected = GREEN_COLOR;
}
} else {
Serial.println("Failed");
}
Serial.println(getColorName());
//flashLED();
/* // Figure out some basic hex code for visualization
uint32_t sum = clear;
float r, g, b;
r = red; r /= sum;
g = green; g /= sum;
b = blue; b /= sum;
r *= 256; g *= 256; b *= 256;
/*Serial.print("\t");
Serial.print((int)r, HEX); Serial.print((int)g, HEX); Serial.print((int)b, HEX);
Serial.println();*/
//Serial.print((int)r ); Serial.print(" "); Serial.print((int)g);Serial.print(" "); Serial.println((int)b );
//Serial.println("Loop took " + String(millis() - t0) + "ms");
}
String getColorName() {
switch (colorDetected) {
case NONE:
return "no color";
case RED_COLOR:
return "red";
case GREEN_COLOR:
return "green";
case BLUE_COLOR:
return "blue";
case YELLOW_COLOR:
return "yellow";
case WHITE_COLOR:
return "white";
default:
return "failed";
}
}
void flashLED() {
for (uint8_t i=0; i<colorDetected; i++) {
digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,LOW);
delay(100);
}
delay(250);
}