-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar_car.nxc
64 lines (56 loc) · 1.54 KB
/
car_car.nxc
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
#define SPEEDBOX 4
#define TURNBOX 5
safecall void drive(int speed) {
OnRevReg(OUT_BC,speed,OUT_REGMODE_SPEED);
}
safecall void stopdriving() {
Off(OUT_BC);
}
safecall void turnleft(int turn, int count) {
TextOut(0, LCD_LINE3, "Left " );
NumOut(58, LCD_LINE3, turn );
if ( count > -40 ) { RotateMotor(OUT_A, 80,-12); }
}
safecall void turnright(int turn, int count) {
TextOut(0, LCD_LINE3, "Right " );
NumOut(58, LCD_LINE3, turn );
if ( count < 40 ) { RotateMotor(OUT_A, 80, 12); }
}
safecall void straight() {
TextOut(0, LCD_LINE3, "Straight " );
}
task BeepBack() {
while(true) {
while (MotorPower(OUT_B) > 0) {
PlayTone(440,500);
Wait(750);
}
}
}
task ListenToMercedes() {
int speed;
int turn;
while(true) {
if (ReceiveRemoteNumber(TURNBOX,false,turn) != STAT_MSG_EMPTY_MAILBOX)
{
int count;
count = MotorRotationCount(OUT_A);
if ( turn > 0 ) { turnright(turn,count); }
if ( turn < 0 ) { turnleft(turn,count); }
if ( turn == 0 ) { straight(); }
}
NumOut(58, LCD_LINE3, turn );
if (ReceiveRemoteNumber(SPEEDBOX,false,speed) != STAT_MSG_EMPTY_MAILBOX)
{
if ( speed > 0 ) { TextOut(0, LCD_LINE2, "Forward " ); drive(speed); }
if ( speed < 0 ) { TextOut(0, LCD_LINE2, "Backward " ); drive(speed); }
if ( speed == 0 ) { TextOut(0, LCD_LINE2, "Stopped " ); stopdriving(); }
}
NumOut(58, LCD_LINE2, speed );
Wait(9);
}
}
task main() {
ResetRotationCount(OUT_A);
Precedes(ListenToMercedes,BeepBack);
}