-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.ts
156 lines (156 loc) · 5.71 KB
/
test.ts
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
JoyCar.initJoyCar(RevisionMainboard.OnepThree)
function standBy () {
JoyCar.stop(StopIntensity.Intense)
JoyCar.brakelight(ToggleSwitch.On)
JoyCar.hazardlights(ToggleSwitch.On)
if (standByRuntime == 0) {
standByRuntime = input.runningTime()
}
if (input.runningTime() - standByRuntime <= 2000) {
basic.showLeds(`
. # . # .
. . . . .
. . . . .
# . . . #
. # # # .
`)
} else {
basic.showLeds(`
. # # # .
# . . . #
. . . . .
. # . # .
. # . # .
`)
standByRuntime = 0
}
}
function obstacleAvoidance () {
JoyCar.brakelight(ToggleSwitch.Off)
JoyCar.hazardlights(ToggleSwitch.Off)
basic.showLeds(`
. . # . .
. . # . .
. . # . .
. . . . .
# # # # #
`)
if (JoyCar.obstacleavoidance(SensorLRSelection.Left)) {
JoyCar.stop(StopIntensity.Intense)
JoyCar.brakelight(ToggleSwitch.On)
basic.pause(200)
JoyCar.brakelight(ToggleSwitch.Off)
JoyCar.reversinglight(ToggleSwitch.On)
JoyCar.drive(FRDirection.Reverse, 50)
basic.pause(300)
JoyCar.reversinglight(ToggleSwitch.Off)
JoyCar.turn(FRDirection.Forward, LRDirection.Right, 40, 0)
basic.pause(300)
} else if (JoyCar.obstacleavoidance(SensorLRSelection.Right)) {
JoyCar.stop(StopIntensity.Intense)
JoyCar.brakelight(ToggleSwitch.On)
basic.pause(200)
JoyCar.brakelight(ToggleSwitch.Off)
JoyCar.reversinglight(ToggleSwitch.On)
JoyCar.drive(FRDirection.Reverse, 50)
basic.pause(300)
JoyCar.reversinglight(ToggleSwitch.Off)
JoyCar.turn(FRDirection.Forward, LRDirection.Left, 40, 0)
basic.pause(300)
} else if (JoyCar.obstacleavoidance(SensorLRSelection.Left) && JoyCar.obstacleavoidance(SensorLRSelection.Right)) {
JoyCar.stop(StopIntensity.Intense)
JoyCar.brakelight(ToggleSwitch.On)
basic.pause(200)
JoyCar.brakelight(ToggleSwitch.Off)
JoyCar.reversinglight(ToggleSwitch.On)
JoyCar.drive(FRDirection.Reverse, 50)
basic.pause(100)
JoyCar.reversinglight(ToggleSwitch.Off)
JoyCar.stop(StopIntensity.Soft)
basic.pause(100)
JoyCar.turn(FRDirection.Forward, LRDirection.Left, 40, 0)
basic.pause(500)
JoyCar.stop(StopIntensity.Soft)
basic.pause(500)
} else {
JoyCar.drive(FRDirection.Forward, 50)
}
}
input.onButtonPressed(Button.A, function () {
if (menuMode < 2) {
menuMode += 1
} else {
menuMode = 0
}
})
function lineTracking () {
JoyCar.brakelight(ToggleSwitch.Off)
JoyCar.hazardlights(ToggleSwitch.Off)
basic.showLeds(`
# . # . #
# . # . #
# . # . #
# . # . #
# . # . #
`)
if (!(JoyCar.linefinder(SensorLCRSelection.Left)) && (!(JoyCar.linefinder(SensorLCRSelection.Center)) && !(JoyCar.linefinder(SensorLCRSelection.Right)))) {
JoyCar.turn(FRDirection.Forward, LRDirection.Left, 50, 0)
} else if (!(JoyCar.linefinder(SensorLCRSelection.Left)) && (!(JoyCar.linefinder(SensorLCRSelection.Center)) && JoyCar.linefinder(SensorLCRSelection.Right))) {
JoyCar.turn(FRDirection.Forward, LRDirection.Right, 50, 0)
} else if (!(JoyCar.linefinder(SensorLCRSelection.Left)) && (JoyCar.linefinder(SensorLCRSelection.Center) && !(JoyCar.linefinder(SensorLCRSelection.Right)))) {
JoyCar.drive(FRDirection.Forward, 50)
} else if (!(JoyCar.linefinder(SensorLCRSelection.Left)) && (JoyCar.linefinder(SensorLCRSelection.Center) && JoyCar.linefinder(SensorLCRSelection.Right))) {
JoyCar.turn(FRDirection.Forward, LRDirection.Right, 50, 2)
} else if (JoyCar.linefinder(SensorLCRSelection.Left) && (!(JoyCar.linefinder(SensorLCRSelection.Center)) && !(JoyCar.linefinder(SensorLCRSelection.Right)))) {
JoyCar.turn(FRDirection.Forward, LRDirection.Left, 50, 0)
} else if (JoyCar.linefinder(SensorLCRSelection.Left) && (!(JoyCar.linefinder(SensorLCRSelection.Center)) && JoyCar.linefinder(SensorLCRSelection.Right))) {
JoyCar.stop(StopIntensity.Intense)
JoyCar.brakelight(ToggleSwitch.On)
} else if (JoyCar.linefinder(SensorLCRSelection.Left) && (JoyCar.linefinder(SensorLCRSelection.Center) && !(JoyCar.linefinder(SensorLCRSelection.Right)))) {
JoyCar.turn(FRDirection.Forward, LRDirection.Left, 50, 2)
} else if (JoyCar.linefinder(SensorLCRSelection.Left) && (JoyCar.linefinder(SensorLCRSelection.Center) && JoyCar.linefinder(SensorLCRSelection.Right))) {
JoyCar.drive(FRDirection.Forward, 50)
} else {
JoyCar.stop(StopIntensity.Intense)
JoyCar.brakelight(ToggleSwitch.On)
JoyCar.hazardlights(ToggleSwitch.On)
}
}
function hazardLights () {
hazardRuntime = input.runningTime()
while (input.runningTime() - hazardRuntime <= 2000) {
JoyCar.hazardlights(ToggleSwitch.On)
}
JoyCar.hazardlights(ToggleSwitch.Off)
}
let hazardRuntime = 0
let standByRuntime = 0
let menuMode = 0
basic.showString("Hello!")
menuMode = 0
standByRuntime = 0
JoyCar.light(ToggleSwitch.On)
basic.pause(300)
JoyCar.light(ToggleSwitch.Off)
basic.pause(300)
JoyCar.light(ToggleSwitch.On)
basic.pause(1000)
JoyCar.light(ToggleSwitch.Off)
basic.pause(300)
JoyCar.brakelight(ToggleSwitch.On)
basic.pause(300)
JoyCar.brakelight(ToggleSwitch.Off)
basic.pause(300)
hazardLights()
JoyCar.buzzer(Melodies.Dadadadum, MelodyOptions.Once)
basic.forever(function () {
if (menuMode == 0) {
standBy()
} else if (menuMode == 1) {
obstacleAvoidance()
} else if (menuMode == 2) {
lineTracking()
} else {
menuMode = 0
}
})