-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolour_sensor.ino
More file actions
217 lines (196 loc) · 4.56 KB
/
colour_sensor.ino
File metadata and controls
217 lines (196 loc) · 4.56 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
212
213
214
215
216
217
#include <Servo.h>
#include <LiquidCrystal.h>
/* RS pin -> 14 A0
enable pin -> 15 A1
D4-D7 -> 16-19 A2-A5
*/
// lcd display
LiquidCrystal lcd(14, 15, 16, 17, 18, 19);
int count_red = 0;
int count_orange = 0;
int count_green = 0;
int count_yellow = 0;
int count_blue = 0;
int count_brown = 0;
int count_other = 0;
int total = 0;
// servo motors
Servo top;
Servo bottom;
//color sensor
#define S0 4
#define S1 5
#define S2 6
#define S3 7
#define sensorOut 8
int freq = 0;
int colour = 0;
void setup() {
// colour sensor
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(sensorOut, INPUT);
// Setting frequency-scaling to 20%
digitalWrite(S0, HIGH);
digitalWrite(S1, LOW);
// servo motor
top.attach(9);
bottom.attach(10);
Serial.begin(9600);
//lcd display
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("START");
lcd.setCursor(0, 1);
lcd.print("LOADING...");
delay(2500);
}
void loop() {
lcd.clear();
lcd.print("START SORTING....");
top.write(65);
delay(500);
//send candy to colour sensor
top.write(30);
delay(1000);
colour = readColour();
delay(50);
switch (colour) {
case 1:
Serial.println("red");
count_red += 1; // counter
total += 1;
//lcd display prints data
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Colour Identify:");
lcd.setCursor(0, 1);
lcd.print("RED");
break;
case 2:
count_orange += 1; //counter
total += 1;
Serial.println("orange");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Colour Identify:");
lcd.setCursor(0, 1);
lcd.print("ORANGE");
break;
case 3:
count_yellow += 1; //counter
total += 1;
Serial.println("yellow");
//lcd display prints data
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Colour Identify:");
lcd.setCursor(0, 1);
lcd.print("YELLOW");
break;
case 4:
count_green += 1; //counter
total += 1;
//lcd display prints data
Serial.println("green");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Colour Identify:");
lcd.setCursor(0, 1);
lcd.print("GREEN");
break;
case 5:
count_blue += 1; //counter
total += 1;
Serial.println("blue");
//lcd display prints data
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Colour Identify:");
lcd.setCursor(0, 1);
lcd.print("BLUE");
break;
case 6:
count_brown += 1; //counter
total += 1;
Serial.println("brown");
//lcd display prints data
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Colour Identify:");
lcd.setCursor(0, 1);
lcd.print("BROWN");
break;
case 0:
count_other += 1; //counter
total += 1;
Serial.println("OUT OF RANGE");
//lcd display prints data
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Colour Identify:");
lcd.setCursor(0, 1);
lcd.print("OUT OF RANGE");
break;
}
delay(1000);
}
int readColour() {
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
//read output frequency
freq = pulseIn(sensorOut, LOW);
int R = freq;
//print value
Serial.print("R= "); //name
Serial.print(freq); // red colour
Serial.println(" ");
delay(50);
// set green photodiodes to be read
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
//read output frequency
freq = pulseIn(sensorOut, LOW);
int G = freq;
//print value
Serial.print("G= "); // name
Serial.print(freq); // colour
Serial.println(" ");
delay(50);
// set blue photodiodes to be read
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
//read output frequency
freq = pulseIn(sensorOut, LOW);
int B = freq;
//print value
Serial.print("B= "); // name
Serial.print(freq); // colour
Serial.println(" ");
delay(50);
// detect colours and return the colur value for sorting
if (R < 85 & R > 79 & G < 115 & G > 111 & B < 90 & B > 88) {
colour = 1; // red
}
else if (R < 28 & R > 25 & G < 37 & G > 34 & B < 31 & B > 28) {
colour = 2; //orange
}
else if (R < 27 & R > 24 & G < 32 & G > 29 & B < 30 & B > 27) {
colour = 3; //yellow
}
else if (R < 38 & R > 35 & G < 41 & G > 39 & B < 36 & B > 34) {
colour = 4; //green
}
else if (R < 37 & R > 34 & G < 37 & G > 33 & B < 29 & B > 26) {
colour = 5; //blue
}
else if (R < 37 & R > 34 & G < 42 & G > 39 & B < 34 & B > 31) {
colour = 6; //brown
}
else {
colour = 0; // out of range colour
}
return colour;
}