-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathColor_Detect_Action.ino
172 lines (145 loc) · 3.08 KB
/
Color_Detect_Action.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
/**
* Drives to a black strip, detects the color above and carries out an action
*/
#include "MeMCore.h"
#include "Wire.h"
#define RED colorarr[0]
#define GREEN colorarr[1]
#define BLUE colorarr[2]
MeLineFollower lineFinder(PORT_2);
MeUltrasonicSensor ultraSensor(PORT_3);
MeLightSensor lightsensor(PORT_6);
MeRGBLed rgbled(PORT_7);
MeDCMotor motor_right(M1);
MeDCMotor motor_left(M2);
uint8_t motorSpeed = 100;
double red;
double green;
double blue;
float blackcalibarr[]={376,280,309};
float grayarr[]={229,173,184};
float colorarr[]={0,0,0};
void setup() {
}
void loop() {
mbotactions();
}
void mbotactions(){
//check for black strip
int sensorState = lineFinder.readSensors();
switch(sensorState)
{
case S1_IN_S2_IN:
motor_right.stop();
motor_left.stop();
colour_check();
break;
case S1_IN_S2_OUT:
case S1_OUT_S2_IN:
case S1_OUT_S2_OUT:
motor_right.run(+motorSpeed);
motor_left.run(-motorSpeed);
break;
default:
break;
}
}
void turn_left() {//change delay to change left turn angle
motor_right.run(+motorSpeed);
motor_left.run(+motorSpeed);
delay(890);
}
void turn_right() {
motor_right.run(-motorSpeed);
motor_left.run(-motorSpeed);
delay(890);
}
void black(){
//play music
}
void red(){
turn_left();
}
void green() {
turn_right();
}
void blue() {
turn_right();
int dist = ultraSensor.distanceCm();
delay(100);
while(dist > 4) {
motor_right.run(+motorSpeed);
motor_left.run(-motorSpeed);
dist = ultraSensor.distanceCm();
delay(100);
}
motor_right.stop();
motor_left.stop();
turn_right();
}
void yellow(){
turn_left();
turn_left();
}
void purple(){
turn_left();
int dist = ultraSensor.distanceCm();
delay(100);
while(dist > 4) {
motor_right.run(+motorSpeed);
motor_left.run(-motorSpeed);
dist = ultraSensor.distanceCm();
delay(100);
}
motor_right.stop();
motor_left.stop();
turn_left();
}
void colour_check(){
int count = 0;
while (count < 3) { // checks a max of 3 times. If fail detect all 3 times, the robot will just continue going forward and crash
rgbled.setColor(255,0,0);
rgbled.show();
delay(1000);
red = lightsensor.read();
rgbled.setColor(0,255,0);
rgbled.show();
delay(1000);
green = lightsensor.read();
rgbled.setColor(0,0,255);
rgbled.show();
delay(1000);
blue = lightsensor.read();
colorarr[0]= 255 * (red-blackcalibarr[0])/grayarr[0];
colorarr[1]= 255 * (green-blackcalibarr[1])/grayarr[1];
colorarr[2]= 255 * (blue-blackcalibarr[2])/grayarr[2];
rgbled.setColor(0,0,0);
rgbled.show();
if (RED < 20 && GREEN < 20 && BLUE < 20) {
black();
count = 3;
}
else if (abs(GREEN-BLUE) < 20 && RED > GREEN) {
red();
count = 3;
}
else if (RED > 200 & RED > GREEN && GREEN > BLUE) {
yellow();
count = 3;
}
else if(abs(RED-GREEN) < 20 && BLUE > RED) {
purple();
count = 3;
}
else if(abs(GREEN-BLUE) < 20 && RED < GREEN) {
blue();
count = 3;
}
else if(abs(RED-BLUE) < 20 && GREEN > BLUE) {
green();
count = 3
}
else {
count += 1;
}
}