-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2023 Starting Lights.c
268 lines (246 loc) · 6.69 KB
/
2023 Starting Lights.c
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <kipr/wombat.h>
// #include <kipr/graphics/graphics_characters.h>
#include <stdlib.h>
#include <stdio.h>
#define P1 30,150,130,200
#define P2 190,150,290,200
#define P3 350,150,450,200
#define P4 510,150,610,200
#define P5 670,150,770,200
#define SERVO_STOPPER_POSITION 2000
#define SERVO_DROPPER_CLOSED 2000
#define SERVO_DROPPER_OPEN 100
#define SERVO_FLAG_DOWN 2000
#define SERVO_FLAG_UP 1000
void set_all_servos();
void motors_on();
void flash(int number);
int rand_color();
void count5();
void select_button();
int run_button();
void checklist();
void run();
int button = -1;
int main()
{
printf("Starting tournament UI . . .\n");
msleep(1000);
enable_servos();
run();
return 0;
}
// Sets any plugged in servos
void set_all_servos() {
set_servo_position(0, SERVO_DROPPER_CLOSED);
set_servo_position(1, SERVO_DROPPER_CLOSED);
set_servo_position(2, SERVO_FLAG_DOWN);
set_servo_position(3, SERVO_FLAG_DOWN);
}
// Turns on any plugged in lights
void motors_on() {
motor(0,100);
motor(1,100);
motor(2,100);
motor(3,100);
}
void flash(int number) {
int x = 0;
for(x = 0; x < number; x++){
motors_on();
msleep(500);
ao();
msleep(500);
}
}
int rand_color() {
srand(seconds());
int rand_num = rand() % 2;
int i;
printf("Proof that numbers are random: ");
for(i=0;i<10;i++){
rand_num = rand() % 2;
printf("%d ",rand_num);
}
printf("\n");
return rand_num;
}
void count5() {
graphics_print_string("5",378,150,255,255,255,6);
graphics_update();
msleep(1000);
graphics_clear();
graphics_print_string("4",378,150,255,255,255,6);
graphics_update();
msleep(1000);
graphics_clear();
graphics_print_string("3",378,150,255,255,255,6);
graphics_update();
msleep(1000);
graphics_clear();
graphics_print_string("2",378,150,255,255,255,6);
graphics_update();
msleep(1000);
graphics_clear();
graphics_print_string("1",378,150,255,255,255,6);
graphics_update();
msleep(1000);
graphics_clear();
}
void select_button() {
if(push_button())
button = 0;
if(a_button())
button = 1;
if(b_button())
button = 2;
if(c_button())
printf("Can't use C button\n");
if(left_button())
button = 4;
if(right_button())
button = 5;
}
int run_button() {
select_button();
switch(button) {
case 0:
button = -1;
return push_button();
case 1:
button = -1;
return a_button();
case 2:
button = -1;
return b_button();
case 3:
button = -1;
return c_button();
case 4:
button = -1;
return left_button();
case 5:
button = -1;
return right_button();
default:
return 0;
}
}
void checklist() {
printf("Checklist for starting lights:\n");
printf("- Lights plugged into any motor port.\n");
printf("- Dropper servo plugged into servo port 0.\n");
printf("- Light switch plugged into digital port 0.\n");
printf("- Dropper light switch off.\n");
printf("- Flag servo plugged into servo port 3.\n");
printf("- Flag station touch sensor plugged into digital port 1.\n");
msleep(2000);
printf("Press grey push button to proceed.\n");
while(push_button() == 0) {
msleep(10);
}
}
// Run program on loop
void run() {
while(c_button() == 0) {
printf("Press button to set servos\n");
while(push_button() == 0) {
if(c_button())
return;
msleep(10);
}
set_all_servos(SERVO_STOPPER_POSITION);
msleep(1000);
printf("Servos set . . .\n");
msleep(1000);
checklist();
// Wait to start
printf("Press grey push button to Start\n");
printf("Press c button to stop program\n");
while(push_button() == 0) {
if(c_button())
return;
msleep(10);
}
// Countdown
graphics_open(800,350);
msleep(500);
// Set droppers to open
set_servo_position(0, SERVO_DROPPER_OPEN);
set_servo_position(1, SERVO_DROPPER_OPEN);
count5();
// Start
double start_time = seconds();
// printf("Start time: %lf\n", start_time);
double end_time = start_time + 120;
double curr_time = start_time;
motors_on();
graphics_clear();
graphics_print_string("Timer",296,50,255,255,255,6);
graphics_print_string("119",340,150,255,255,255,6);
printf("Press button to Stop\n");
graphics_update();
char sec[3];
double countdown;
double prev_time = curr_time;
int time;
int decimal;
while(curr_time < end_time) {
if(push_button() == 1) {
break;
}
if(c_button()) {
return;
}
countdown = end_time - curr_time;
if(countdown < 115 && countdown > 5) {
ao();
}
// Check if flag buttons are pressed
if(digital(0) == 1) {
set_servo_position(2, SERVO_FLAG_UP);
printf("Flag sensor in port 0 pressed\n");
}
if(digital(1) == 1) {
set_servo_position(3, SERVO_FLAG_UP);
printf("Flag sensor in port 1 pressed\n");
}
// Show time left
time = (int)countdown;
if(curr_time - prev_time > 1) {
prev_time = curr_time;
sprintf(sec, "%d", time);
printf("%d\n",time);
graphics_clear();
graphics_print_string("Timer",296,50,255,255,255,6);
graphics_print_string(sec,340,150,255,255,255,6);
graphics_print_string("Press button to Stop",50,250,255,255,255,5);
graphics_update();
if(countdown <= 5) {
decimal = countdown - time;
if(decimal > 0.5) {
motors_on();
}
else {
ao();
}
}
}
msleep(900);
curr_time = seconds();
}
// Stopped
graphics_close();
ao();
msleep(3000);
// Wait to restart
printf("Press button to restart\n");
while(push_button() == 0) {
if(c_button())
return;
msleep(10);
}
msleep(1000);
console_clear();
}
}