-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_ping_pong.c
72 lines (59 loc) · 1.57 KB
/
random_ping_pong.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
#pragma config(Sensor, S3, light, sensorLightActive)
#pragma config(Sensor, S1, touch, sensorTouch)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
// 48 threshold
void pivot_turn(float degrees);
void forward(float revolutions);
void forward_cm(float cm);
task main()
{
int threshold = 48;
int white_touches = 0;
while (white_touches < 5) {
nSyncedMotors = synchBC;
nSyncedTurnRatio = 100;
while (SensorValue(touch) == 0 && SensorValue(light) <= threshold) {
motor[motorB] = 50;
}
if (SensorValue(touch) == 0) {
motor[motorB] = 0;
if (SensorValue(light) <= threshold) {
white_touches++;
}
motor[motorB] = 20;
while (SensorValue(light) <= threshold) {}
}
else {
motor[motorB] = 0;
forward_cm(-7);
wait1Msec(500);
int turn = random(90) + 90;
if (turn % 2 == 0) {
turn = turn * -1;
}
pivot_turn(turn);
wait1Msec(200);
}
}
}
void pivot_turn(float degrees) {
nSyncedMotors = synchBC;
nSyncedTurnRatio = -100;
nMotorEncoder[motorB] = 0;
nMotorEncoderTarget[motorB] = 360 * (degrees/180);
motor[motorB] = (degrees >= 0) ? 30 : -30;
while(nMotorRunState[motorB] != runStateIdle) {}
nSyncedMotors = synchNone;
motor[motorB] = 0;
}
void forward(float revolutions) {
nSyncedMotors = synchBC;
nMotorEncoder[motorB] = 0;
nMotorEncoderTarget[motorB] = revolutions * 360;
nSyncedTurnRatio = 100;
motor[motorB] = (revolutions > 0) ? 50 : -50;
while(nMotorRunState[motorB] != runStateIdle) {}
}
void forward_cm(float cm) {
forward((5 * (cm / 90)) * 100/98);
}