-
Notifications
You must be signed in to change notification settings - Fork 0
/
color tracking.ino
214 lines (192 loc) · 4.77 KB
/
color tracking.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
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
// Define color sensor pins
#define S0 4
#define S1 5
#define S2 7
#define S3 6
#define sensorOut 8
int motor1pin1 = 13;
int motor1pin2 = 2;
int motor2pin1 = 10;
int motor2pin2 = 12;
// put your setup code here, to run once:
// Calibration Values
// *Get these from Calibration Sketch
//int redMin = 52; // Red minimum value
//int redMax = 185; // Red maximum value
//int greenMin = 80; // Green minimum value
//int greenMax = 170; // Green maximum value
//int blueMin = 86; // Blue minimum value
//int blueMax = 180; // Blue maximum value
//int redMin = 32; // Red minimum value
//int redMax = 191; // Red maximum value
//int greenMin = 73; // Green minimum value
//int greenMax = 237; // Green maximum value
//int blueMin = 59; // Blue minimum value
//int blueMax = 201;
int redMin =22;
int redMax = 211;
int greenMin = 18;
int greenMax = 267;
int blueMin = 26;
int blueMax = 193;
// Variables for Color Pulse Width Measurements
int redPW = 0;
int greenPW = 0;
int bluePW = 0;
// Variables for final Color values
int redValue;
int greenValue;
int blueValue;
int enA =9;
int enB=11;
void setup() {
// Set S0 - S3 as outputs
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
// Set Sensor output as input
pinMode(sensorOut, INPUT);
// Set Frequency scaling to 100%
digitalWrite(S0,HIGH);
digitalWrite(S1,HIGH);
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(motor2pin1, OUTPUT);
pinMode(motor2pin2, OUTPUT);
//(Optional)
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
// Setup Serial Monitor
Serial.begin(9600);
}
void loop() {
// Read Red value
redPW = getRedPW();
// Map to value from 0-255
redValue = map(redPW, redMin,redMax,255,0);
// Delay to stabilize sensor
delay(200);
// Read Green value
greenPW = getGreenPW();
// Map to value from 0-255
greenValue = map(greenPW, greenMin,greenMax,255,0);
// Delay to stabilize sensor
delay(200);
// Read Blue value
bluePW = getBluePW();
// Map to value from 0-255
blueValue = map(bluePW, blueMin,blueMax,255,0);
// Delay to stabilize sensor
delay(200);
// Print output to Serial Monitor
Serial.print("Red = ");
Serial.print(redValue);
Serial.print(" - Green = ");
Serial.print(greenValue);
Serial.print(" - Blue = ");
Serial.println(blueValue);
if( blueValue > greenValue && blueValue > redValue)
{
Serial.println("Blue Colour is detected!");
while(303<blueValue<334){
Serial.println("FORWARD...");
forward();
break;
}
}
else if( greenValue > blueValue && greenValue > redValue )
{
Serial.println("Green Colour is detected!");
stop();
}
else if(redValue > greenValue && redValue < blueValue)
{
Serial.println("Red Colour is detected!");
stop();
}
else{
Serial.println("Stopped");
stop();
}
}
/*else if( blueValue > greenValue && blueValue > redValue ){
Serial.println("Blue Colour is detected!");
}
else if( greenValue > blueValue && greenValue > redValue ){
Serial.println("Green Colour is detected!");
}
*/
// Function to read Red Pulse Widths
int getRedPW() {
// Set sensor to read Red only
digitalWrite(S2,LOW);
digitalWrite(S3,LOW);
// Define integer to represent Pulse Width
int PW;
// Read the output Pulse Width
PW = pulseIn(sensorOut, LOW);
// Return the value
return PW;
}
// Function to read Green Pulse Widths
int getGreenPW()
{
// Set sensor to read Green only
digitalWrite(S2,HIGH);
digitalWrite(S3,HIGH);
// Define integer to represent Pulse Width
int PW;
// Read the output Pulse Width
PW = pulseIn(sensorOut, LOW);
// Return the value
return PW;
}
// Function to read Blue Pulse Widths
int getBluePW() {
// Set sensor to read Blue only
digitalWrite(S2,LOW);
digitalWrite(S3,HIGH);
// Define integer to represent Pulse Width
int PW;
// Read the output Pulse Width
PW = pulseIn(sensorOut, LOW);
// Return the value
return PW;
}
void forward(){
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
analogWrite(enA,255);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
analogWrite(enB,255);
}
void turn(){
analogWrite(enA,255);
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
analogWrite(enB,255);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
}
void stop(){
analogWrite(enA,0);
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2,HIGH);
analogWrite(enB,150);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
}
void backward(){
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
analogWrite(enA,255);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
analogWrite(enB,255);
}