-
Notifications
You must be signed in to change notification settings - Fork 1
/
Color_Sensor_v2.ino
158 lines (130 loc) · 3.13 KB
/
Color_Sensor_v2.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
#include <MeMCore.h>
#define MAX 255
#define DELAY_TIME 250
// Initialize sensors
MeLightSensor lightSensor(PORT_6);
MeRGBLed led(0,30);
MeBuzzer buzzer;
int colorArr[3] = {0};
int whiteArr[3] = {601, 359, 426};
int blackArr[3] = {256, 141, 185};
int greyDiff[3] = {whiteArr[0]-blackArr[0],
whiteArr[1]-blackArr[1],
whiteArr[2]-blackArr[2]};
void setup() {
// put your setup code here, to run once:
led.setpin(13);
pinMode(7, INPUT); //Define button pin as input
//begin serial communication
Serial.begin(9600);
led.setColor(0,0,0);
led.show();
// PRESS BUTTON WITHIN 5 SECS TO CALIBRATE
delay(5000);
if (analogRead(7) < 100) {
calibrateColor();
}
}
void loop() {
// put your main code here, to run repeatedly:
colorChallenge();
delay(2000);
}
void colorChallenge() {
int colorArr[3];
int r, g, b;
char color = '\0';
for (int i=0; i<3; i++) {
led.setColor( (i==0) ? MAX : 0, (i==1) ? MAX : 0 , (i==2) ? MAX : 0);
led.show();
delay(DELAY_TIME);
colorArr[i] = (lightSensor.read() - blackArr[i]) * 255 / greyDiff[i];
}
led.setColor(0,0,0);
led.show();
r = colorArr[0],
g = colorArr[1],
b = colorArr[2];
// Conditionals to determine color
// Return 0 for no color detected,
// X for black,
// R for red,
// G for green,
// B for blue,
// Y for yellow,
// P for purple.
// $ for error.
if (r+g+b>100*3) {
color = '$';
} else if (r<-80) {
color = 'Y';
} else if (r>65) {
color ='R';
} else if (r>30 && r<55) {
if (g>80)
color = 'B';
else
color = 'P';
} else if (g>30) {
color = 'G';
} else if (r<0 && g<0 && b<0){
color = 'X';
} else {
color = '$';
}
printArr(colorArr);
Serial.println(color);
// INSTRUCTIONS FOR EACH COLOR
switch (color) {
// PLEASE UPDATE INPUT INSTRUCTIONS
case 'X': break;
case 'R': break;
case 'G': break;
case 'B': break;
case 'Y': break;
case 'P': break;
}
}
void calibrateColor () {
int i;
// QUICKLY PUT BLACK DURING THE FIRST BUZZER
buzzer.tone (600, 2000);
for (int i=0; i<3; i++) {
led.setColor( (i==0) ? MAX : 0, (i==1) ? MAX : 0 , (i==2) ? MAX : 0);
led.show();
delay(DELAY_TIME);
blackArr[i] = lightSensor.read();
}
led.setColor(0,0,0);
led.show();
// QUICKLY PUT WHITE DURING THE SECOND BUZZER
buzzer.tone (600, 2000);
for (int i=0; i<3; i++) {
led.setColor( (i==0) ? MAX : 0, (i==1) ? MAX : 0 , (i==2) ? MAX : 0);
led.show();
delay(DELAY_TIME);
whiteArr[i] = lightSensor.read();
}
led.setColor(0,0,0);
led.show();
for (int i=0; i<3; i++) {
greyDiff[i] = whiteArr[i]-blackArr[i];
}
Serial.print("black: ");
printArr(blackArr);
Serial.print("\n");
Serial.print("white: ");
printArr(whiteArr);\
Serial.print("\n");
// PLAY A TUNE AFTER DONE CALIBRATING
buzzer.tone (440, 200);
buzzer.tone (880, 200);
buzzer.tone (1320, 200);
}
void printArr (int arr[]) {
for (int i=0; i<3; i++) {
Serial.print(arr[i]);
Serial.print(" ");
}
// Serial.print("\n");
}