-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer1.ino
178 lines (146 loc) · 3.65 KB
/
layer1.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
//distance calibration trigger points in IR
#define block_1_away 225
#define block_2_away 100
//trigger value for motor to stop to pass into phase 2 of game
#define trigger_dist_phase2 500
//infrared 45deg calibration if a block is present on the side
#define irSides 225
void resetSpeed(){
speed_left = max_left, speed_right = max_right;
}
/*calibration functions*/
/*aligns*/
void align(){
forward(100);
while (analogRead(ir_front) < block_1_away);
decelerate(100,0);
}
/*pivots*/
//CALIBRATE pivotLeft_t
void pivotLeft(int deg){
m.setSpeed(0,map(pivot_power,0,100,0,speed_right));
delay(map(deg ,0,360, 0,pivotLeft_t));
brake();
}
//CALIBRATE pivotRight_t
void pivotRight(int deg){
m.setSpeed(map(pivot_power,0,100,0,speed_left),0);
delay(map(deg ,0,360, 0,pivotRight_t));
brake();
}
/*FORWARD AND BACKWARD*/
//maps power as % from 0 to the max_power
void forward(unsigned char power){
resetSpeed(); //statics speed_left and speed_right == max_power
m.setSpeed(map(power,0,100,0,speed_left),map(power,0,100,0,speed_right));
}
void backward(unsigned char power){
resetSpeed();
m.setSpeed(-map(power,0,100,0,speed_left),-map(power,0,100,0,speed_right));
}
/*FULL FUNCTIONS*/
//decelerate by time, 0 means fast as possible
void decelerate(int power, int time){
int i = power;
while(i > 0){
i--;
forward(i);
delay(time/power);
}
}
void accelerate(int power, int time){
int i = 0;
while(i < power){
i++;
forward(i);
delay(time/power);
}
}
void frontTouch(){
forward(50);
attachInterrupt(0, touch_left,RISING);
attachInterrupt(1, touch_right, RISING);
}
/*DO NOT EDIT, INTERRUPTS*/
void touch_left(){
detachInterrupt(0);
detachInterrupt(1);
m.setSpeed(-map(20, 0,100, 0,speed_left), map(20, 0,100, 0,speed_right));
attachInterrupt(1, brake, RISING);
}
void touch_right(){
detachInterrupt(0);
detachInterrupt(1);
m.setSpeed(-map(20, 0,100, 0,speed_left), map(20, 0,100, 0,speed_right));
attachInterrupt(0, brake, RISING);
}
void brake(){
m.setSpeed(0,0);
}
//////////////////////////////
/*no-block (false) or block (true)*/
//delays the program until block or no block is seen
void checkBlock(boolean side, boolean stop_at){
unsigned char side_sense = ir_right_45;
if (side) side_sense = ir_left_45;
if (stop_at) while (analogRead(side_sense) > irSides);
else while(analogRead(side_sense) < irSides);
}
// attach to a distance 1 block away
void kissMe(int dist, int power){
forward(power);
while (analogRead(ir_front) < dist){
//alignment
if (!digitalRead(ir_left)){
if(speed_left < 255 && speed_left < max_left +20)
speed_left++;
if(speed_right > max_right -20)
speed_right--;
m.setSpeed(speed_left,speed_right);
}
if (!digitalRead(ir_right)){
if(speed_left > max_left -20)
speed_left--;
if(speed_right < 255 && speed_right < max_right +20)
speed_right++;
m.setSpeed(speed_left, speed_right);
}
}
decelerate(power,0);
}
////////////Calibrate delays (*Caution hypothetical*)
void cross_bridge(){
forward(100);
while (analogRead(ir_front) < trigger_dist_phase2){
if(!digitalRead(ground_left)){
backward(50);
//calibrate
delay(100);
brake();
m.setSpeed(speed_left*.5,-speed_right*.5);
//calibrate
delay(100);
forward(100);
}
if(!digitalRead(ground_right)){
backward(50);
//calibrate
delay(100);
brake();
m.setSpeed(-speed_left*.5, speed_right*.5);
//calibrate
delay(100);
forward(100);
}
}
}
//from front touch
//inset direction, and how many degrees
void turn(bool dir, int deg){
resetSpeed();
if(dir)m.setSpeed(-speed_left,speed_right);
else m.setSpeed(speed_left,-speed_right);
//calibrate
delay(map(deg, 0,360, 0,max_360));
m.setSpeed(0,0);
}