-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge1.cpp
More file actions
196 lines (161 loc) · 3.66 KB
/
challenge1.cpp
File metadata and controls
196 lines (161 loc) · 3.66 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
// motor pins
int leftPin1 = 2;
int leftPin2 = 3;
int rightPin1 = 4;
int rightPin2 = 5;
// color pins
int color0 = 6;
int color1 = 7;
int color2 = 8;
int color3 = 9;
int EN = 13;
int colorOut = 12;
// Ultrasonic
long duration = 0, distance = 0;
int redMin = 67;
int redMax = 900;
int greenMin = 102;
int greenMax = 1300;
int blueMin = 95;
int blueMax = 1000;
int redPW, greenPW, bluePW = 0;
int red, blue, green;
int time180 = 6;
int time90 = 3;
int stop = 0;
typedef enum {
FORWARD,
LEFT,
RIGHT,
STOP
} movement;
movement state = FORWARD;
int seq = 0;
int time = 0;
int rotTime = 0;
int prevColor = 0;
int currColor = 0;
int numRings = 0;
void setup() {
// put your setup code here, to run once:
// motor control
pinMode(leftPin1, OUTPUT);
pinMode(leftPin2, OUTPUT);
pinMode(rightPin1, OUTPUT);
pinMode(rightPin2, OUTPUT);
pinMode(color0, OUTPUT);
pinMode(color1, OUTPUT);
pinMode(color2, OUTPUT);
pinMode(color3, OUTPUT);
pinMode(colorOut, INPUT);
pinMode(EN, OUTPUT);
digitalWrite(EN, LOW);
digitalWrite(color0, HIGH);
digitalWrite(color1, LOW);
Serial.begin(9600);
// speed control
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(A4, OUTPUT);
pinMode(A5, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(A4, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(A4, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(A4, LOW);
duration = pulseIn(A5, HIGH);
distance = (duration/2) / 29.1;
Serial.println(distance);
// Color Sensor
redPW = getRed();
red = map(redPW, redMin, redMax, 255, 0);
delay(200);
greenPW = getGreen();
green = map(greenPW, greenMin, greenMax, 255, 0);
delay(200);
bluePW = getBlue();
blue = map(bluePW, blueMin, blueMax, 255, 0);
delay(200);
/*
Serial.print("Red = ");
Serial.print(red);
Serial.print(" - Green = ");
Serial.print(green);
Serial.print(" - Blue = ");
Serial.println(blue);
*/
if (green < 150 && blue < 150 && red < 150 && stop == 1){
state = STOP;
}
else if (distance > 15 && time == 0){
state = FORWARD;
}
else if (time == 0) {
stop = 1;
if (red > 170){
time = time180;
state = RIGHT;
}
else if (blue > 150 && green > 150){
time = time90;
state = LEFT;
}
else if (green > 150){
time = time90;
state = RIGHT;
}
}
if (time > 0) {
time--;
}
//Controlling speed (0 = off and 255 = max speed):
analogWrite(10, 110); //ENA pin
analogWrite(11, 110); //ENB pin
switch (state) {
case FORWARD: // forward
digitalWrite(leftPin1, HIGH);
digitalWrite(leftPin2, LOW);
digitalWrite(rightPin1, HIGH);
digitalWrite(rightPin2, LOW);
break;
case LEFT: // left
digitalWrite(leftPin1, LOW);
digitalWrite(leftPin2, HIGH);
digitalWrite(rightPin1, HIGH);
digitalWrite(rightPin2, LOW);
break;
case RIGHT: // right
digitalWrite(leftPin1, HIGH);
digitalWrite(leftPin2, LOW);
digitalWrite(rightPin1, LOW);
digitalWrite(rightPin2, HIGH);
break;
case STOP: // stop
digitalWrite(leftPin1, LOW);
digitalWrite(leftPin2, LOW);
digitalWrite(rightPin1, LOW);
digitalWrite(rightPin2, LOW);
break;
}
}
// get pw for RED
int getRed() {
digitalWrite(color2, LOW);
digitalWrite(color3, LOW);
return pulseIn(colorOut, LOW);
}
// get pw for GREEN
int getGreen() {
digitalWrite(color2, HIGH);
digitalWrite(color3, HIGH);
return pulseIn(colorOut, LOW);
}
// get pw for BLUE
int getBlue() {
digitalWrite(color2, LOW);
digitalWrite(color3, HIGH);
return pulseIn(colorOut, LOW);
}