-
Notifications
You must be signed in to change notification settings - Fork 14
/
motorbit.ts
125 lines (93 loc) · 3.29 KB
/
motorbit.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
//% color=#0fbc11 weight=10 icon="\uf1b9"
namespace motorbit {
/**
* TODO: describe your function here
* @param n the n from 0 (min) to 100 (max), eg:0
*/
//% blockId=motorbit_forward block="move forward with speed %n"
//% n.min=0 n.max=100
export function forward(n: number): void {
// Add code here
pins.digitalWritePin(DigitalPin.P8, 0)
pins.digitalWritePin(DigitalPin.P12, 1)
pins.analogWritePin(AnalogPin.P1, n * 1023 / 100)
pins.analogWritePin(AnalogPin.P2, n * 1023 / 100)
}
/**
* TODO: describe your function here
* @param n the n from 0 (min) to 100 (max), eg:0
*/
//% blockId=motorbit_back block="move back with speed %n"
//% n.min=0 n.max=100
export function back(n: number): void {
// Add code here
pins.digitalWritePin(DigitalPin.P8, 1)
pins.digitalWritePin(DigitalPin.P12, 0)
pins.analogWritePin(AnalogPin.P1, n * 1023 / 100)
pins.analogWritePin(AnalogPin.P2, n * 1023 / 100)
}
/**
* TODO: describe your function here
* @param n the n from 0 (min) to 100 (max), eg:50
*/
//% blockId=motorbit_turnleft block="turn left with speed %n"
//% n.min=0 n.max=100
export function turnleft(n: number): void {
// Add code here
pins.digitalWritePin(DigitalPin.P8, 0)
pins.digitalWritePin(DigitalPin.P12, 1)
pins.analogWritePin(AnalogPin.P1, 0)
pins.analogWritePin(AnalogPin.P2, n * 1023 / 100)
}
/**
* TODO: describe your function here
* @param n the n from 0 (min) to 100 (max), eg:50
*/
//% blockId=motorbit_turnright block="turn right with speed %n"
//% n.min=0 n.max=100
export function turnright(n: number): void {
// Add code here
pins.digitalWritePin(DigitalPin.P8, 0)
pins.digitalWritePin(DigitalPin.P12, 1)
pins.analogWritePin(AnalogPin.P1, n * 1023 / 100)
pins.analogWritePin(AnalogPin.P2, 0)
}
/**
* TODO: describe your function here
*
*/
//% blockId=motorbit_brake block="brake"
export function brake(): void {
// Add code here
pins.digitalWritePin(DigitalPin.P8, 0)
pins.digitalWritePin(DigitalPin.P12, 1)
pins.analogWritePin(AnalogPin.P1, 0)
pins.analogWritePin(AnalogPin.P2, 0)
}
/**
* TODO: describe your function here
* @param m the m from -100 (min) to 100 (max), eg:0
* @param n the n from -100 (min) to 100 (max), eg:0
*/
//% blockId=motorbit_freestyle block="left wheel speed %m| right wheel speed %n"
//% m.min=-100 m.max=100
//% n.min=-100 n.max=100
export function freestyle(m: number, n: number): void {
// Add code here
if (m > 0) {
pins.digitalWritePin(DigitalPin.P8, 0)
pins.analogWritePin(AnalogPin.P1, m * 1023 / 100)
}
else {
pins.digitalWritePin(DigitalPin.P8, 1)
pins.analogWritePin(AnalogPin.P1, - m * 1023 / 100)
}
if (n > 0) {
pins.digitalWritePin(DigitalPin.P12, 1)
pins.analogWritePin(AnalogPin.P2, n * 1023 / 100)
} else {
pins.digitalWritePin(DigitalPin.P12, 0)
pins.analogWritePin(AnalogPin.P2, - n * 1023 / 100)
}
}
}