Repository navigation
Expand file tree
/
Copy pathReciever.ino
More file actions
executable file
·211 lines (179 loc) · 5.37 KB
/
Copy pathReciever.ino
File metadata and controls
executable file
·211 lines (179 loc) · 5.37 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Yigit Suoglu Jul 28, 2016
// Reciever code with threshold detection
#include <colorBit_Struct.h>
#include <encode_decode.h>
#include <HammingCode.h>
#include <Adafruit_TCS34725.h>
#include <Wire.h>
#include <RGB_Detection.h>
#define greenLED 12 //bacon
#define blueLED 13 //acknowledgement
#define thErrON 0.9
#define thErrOFF 1.3
#define setupWaitTime 10 //(ms)
#define fbTime 1000 // feedback led high time
#define cycTime 1000 // feedback led low time
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_2_4MS, TCS34725_GAIN_4X);
bool buff[12];
char ch;
colorBit th_Val[8];
uint16_t r, g, b, c;
void setup()
{
th_Val[0].bits[0] = 0; //values for environment
th_Val[0].bits[1] = 0;
th_Val[0].bits[2] = 0;
th_Val[1].bits[0] = 1; //values for R
th_Val[1].bits[1] = 0;
th_Val[1].bits[2] = 0;
th_Val[2].bits[0] = 0; //valurs for G
th_Val[2].bits[1] = 1;
th_Val[2].bits[2] = 0;
th_Val[3].bits[0] = 0; //values for B
th_Val[3].bits[1] = 0;
th_Val[3].bits[2] = 1;
th_Val[4].bits[0] = 1; //values for RG
th_Val[4].bits[1] = 1;
th_Val[4].bits[2] = 0;
th_Val[5].bits[0] = 1; //values for RB
th_Val[5].bits[1] = 0;
th_Val[5].bits[2] = 1;
th_Val[6].bits[0] = 0; //values for GB
th_Val[6].bits[1] = 1;
th_Val[6].bits[2] = 1;
th_Val[7].bits[0] = 1; //values for RGB
th_Val[7].bits[1] = 1;
th_Val[7].bits[2] = 1;
Serial.begin(250000);
if (tcs.begin()) {
Serial.println("Found sensor");
} else {
Serial.println("Cannot find sensor");
while (1);
}
Serial.print("Getting threshold values");
tcs.getRawData(&r, &g, &b, &c); //store environment values
th_Val[0].r_t = (r * thErrOFF);
th_Val[0].g_t = (g * thErrOFF);
th_Val[0].b_t = (b * thErrOFF);
while(r < (1.5 * th_Val[0].r_t))
{
tcs.getRawData(&r, &g, &b, &c); //wait until red is high
}
Serial.print(".");
delay(setupWaitTime);
//set threshold for red high
th_Val[1].r_t = (r * thErrON);
th_Val[1].g_t = (g * thErrOFF);
th_Val[1].b_t = (b * thErrOFF);
while((r > th_Val[1].r_t)/* | (g < (1.5 * th_Val[0].g_t))*/) //commented out due to low program storage space
{
tcs.getRawData(&r, &g, &b, &c); //wait until green is high and red is low
}
Serial.print(".");
delay(setupWaitTime);
//set threshold for green high
th_Val[2].r_t = (r * thErrOFF);
th_Val[2].g_t = (g * thErrON);
th_Val[2].b_t = (b * thErrOFF);
while((g > th_Val[2].g_t)/* | (b < (1.5 * th_Val[0].b_t))*/) //commented out due to low program storage space
{
tcs.getRawData(&r, &g, &b, &c); //wait until blue is high and green is low
}
Serial.print(".");
delay(setupWaitTime);
//set threshold for blue high
th_Val[3].r_t = (r * thErrOFF);
th_Val[3].g_t = (g * thErrOFF);
th_Val[3].b_t = (b * thErrON);
while((b > th_Val[3].b_t))
{
tcs.getRawData(&r, &g, &b, &c); //wait until blue is low
}
Serial.print(".");
delay(setupWaitTime);
//set threshold for red-green high
th_Val[4].r_t = (r * thErrON);
th_Val[4].g_t = (g * thErrON);
th_Val[4].b_t = (b * thErrOFF);
while((g > th_Val[4].g_t))
{
tcs.getRawData(&r, &g, &b, &c); //wait until green is low
}
Serial.print(".");
delay(setupWaitTime);
//set threshold for red-blue high
th_Val[5].r_t = (r * thErrON);
th_Val[5].g_t = (g * thErrOFF);
th_Val[5].b_t = (b * thErrON);
while((r > th_Val[5].r_t))
{
tcs.getRawData(&r, &g, &b, &c); //wait until red is low
}
Serial.print(".");
delay(setupWaitTime);
//set threshold for green-blue high
th_Val[6].r_t = (r * thErrOFF);
th_Val[6].g_t = (g * thErrON);
th_Val[6].b_t = (b * thErrON);
while((r < th_Val[6].r_t))
{
tcs.getRawData(&r, &g, &b, &c); //wait until red is high
}
Serial.print(".");
delay(setupWaitTime);
//set threshold for red-green-blue high
th_Val[7].r_t = (r * thErrON);
th_Val[7].g_t = (g * thErrON);
th_Val[7].b_t = (b * thErrON);
Serial.println("Done");
pinMode(greenLED, OUTPUT);
pinMode(blueLED, OUTPUT);
digitalWrite(greenLED, LOW);
digitalWrite(blueLED, LOW);
digitalWrite(greenLED, LOW);
for(int j = 0; j < 8; j++)
{
Serial.print("Env ");
Serial.print(j);
Serial.print(" R: ");
Serial.print(th_Val[j].r_t);
Serial.print(" G: ");
Serial.print(th_Val[j].g_t);
Serial.print(" B: ");
Serial.println(th_Val[j].b_t);
}
Serial.println("Starting...");
}
void loop()
{
bool err = 0;
digitalWrite(greenLED, HIGH);
delay(fbTime);
digitalWrite(greenLED, LOW);
delay(cycTime);
tcs.getRawData(&r, &g, &b, &c);
detect_rgb(r, g, b, th_Val, buff[0], buff[1], buff[2]);
digitalWrite(blueLED, HIGH);
delay(fbTime);
digitalWrite(blueLED, LOW);
delay(cycTime);
tcs.getRawData(&r, &g, &b, &c);
detect_rgb(r, g, b, th_Val, buff[3], buff[4], buff[5]);
digitalWrite(blueLED, HIGH);
delay(fbTime);
digitalWrite(blueLED, LOW);
delay(cycTime);
tcs.getRawData(&r, &g, &b, &c);
detect_rgb(r, g, b, th_Val, buff[6], buff[7], buff[8]);
digitalWrite(blueLED, HIGH);
delay(fbTime);
digitalWrite(blueLED, LOW);
delay(cycTime);
tcs.getRawData(&r, &g, &b, &c);
detect_rgb(r, g, b, th_Val, buff[9], buff[10], buff[11]);
checkParity(buff[2], buff[4], buff[5], buff[6], buff[8], buff[9], buff[10], buff[11], buff[0], buff[1], buff[3], buff[7], err);
ch = decodeASCII(buff[2], buff[4], buff[5], buff[6], buff[8], buff[9], buff[10], buff[11]);
if(ch != NULL)
Serial.print(ch);
}