-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
1086 lines (913 loc) · 33 KB
/
main.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C): 2010-2019, Shenzhen Yahboom Tech
modified from liusen
load dependency
"mbit": "file:../pxt-mbit"
*/
//% color="#C814B8" weight=25 icon="\uf1d4"
namespace mbit_Display {
export enum enColor {
//% blockId="OFF" block="off"
OFF = 0,
//% blockId="Red" block="red"
Red,
//% blockId="Green" block="green"
Green,
//% blockId="Blue" block="blue"
Blue,
//% blockId="White" block="white"
White,
//% blockId="Cyan" block="cyan"
Cyan,
//% blockId="Pinkish" block="magenta"
Pinkish,
//% blockId="Yellow" block="yellow"
Yellow,
}
export enum enLED1 {
//% blockId="OFF" block="off"
OFF = 0,
//% blockId="ON" block="on"
ON =1
}
//% blockId=mbit_LED1 block="LED1|pin %pin|value %value"
//% weight=5
//% blockGap=8
//% color="#C814B8"
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=1
export function LED1(pin: DigitalPin, value: enLED1): void {
pins.digitalWritePin(pin, value);
}
//% blockId=mbit_LED2 block="LED2|pin %pin|value %value"
//% weight=4
//% blockGap=8
//% color="#C814B8"
//% value.min=0 value.max=255
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=2
export function LED2(pin: AnalogPin, value: number): void {
pins.analogWritePin(pin, value * 1024 / 256);
}
//% blockId=mbit_BreathLED block="BreathLED|pin %pin"
//% weight=3
//% blockGap=8
//% color="#C814B8"
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=3
export function BreathLED(pin: AnalogPin): void {
for (let i: number = 0; i < 1023; i++) {
pins.analogWritePin(pin, i);
//basic.pause(1);
control.waitMicros(1000);
}
basic.pause(10);
for (let i: number = 1023; i > 0; i--) {
pins.analogWritePin(pin, i);
//basic.pause(1);
control.waitMicros(1000);
}
}
//% blockId=mbit_RGB block="RGB|pin1 %pin1|pin2 %pin2|pin3 %pin3|value1 %value1|value2 %value2|value3 %value3"
//% weight=2
//% blockGap=8
//% color="#C814B8"
//% value1.min=0 value1.max=255 value2.min=0 value2.max=255 value3.min=0 value3.max=255
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function RGB(pin1: AnalogPin, pin2: AnalogPin, pin3: AnalogPin, value1: number, value2: number, value3: number): void {
pins.analogWritePin(pin1, value1 * 1024 / 256);
pins.analogWritePin(pin2, value2 * 1024 / 256);
pins.analogWritePin(pin3, value3 * 1024 / 256);
}
//% blockId=mbit_RGB2 block="RGB|pin1 %pin1|pin2 %pin2|pin3 %pin3|value %value"
//% weight=1
//% blockGap=8
//% color="#C814B8"
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function RGB2(pin1: DigitalPin, pin2: DigitalPin, pin3: DigitalPin, value: enColor): void {
switch (value) {
case enColor.OFF: {
pins.digitalWritePin(pin1, 0);
pins.digitalWritePin(pin2, 0);
pins.digitalWritePin(pin3, 0);
break;
}
case enColor.Red: {
pins.digitalWritePin(pin1, 1);
pins.digitalWritePin(pin2, 0);
pins.digitalWritePin(pin3, 0);
break;
}
case enColor.Green: {
pins.digitalWritePin(pin1, 0);
pins.digitalWritePin(pin2, 1);
pins.digitalWritePin(pin3, 0);
break;
}
case enColor.Blue: {
pins.digitalWritePin(pin1, 0);
pins.digitalWritePin(pin2, 0);
pins.digitalWritePin(pin3, 1);
break;
}
case enColor.White: {
pins.digitalWritePin(pin1, 1);
pins.digitalWritePin(pin2, 1);
pins.digitalWritePin(pin3, 1);
break;
}
case enColor.Cyan: {
pins.digitalWritePin(pin1, 0);
pins.digitalWritePin(pin2, 1);
pins.digitalWritePin(pin3, 1);
break;
}
case enColor.Pinkish: {
pins.digitalWritePin(pin1, 1);
pins.digitalWritePin(pin2, 0);
pins.digitalWritePin(pin3, 1);
break;
}
case enColor.Yellow: {
pins.digitalWritePin(pin1, 1);
pins.digitalWritePin(pin2, 1);
pins.digitalWritePin(pin3, 0);
break;
}
}
}
}
/*****************************************************************************************************************************************
* 传感器类 *****************************************************************************************************************************
****************************************************************************************************************************************/
//% color="#87CEEB" weight=24 icon="\uf1b6"
namespace mbit_Sensor {
export enum enVoice {
//% blockId="Voice" block="sound"
Voice = 0,
//% blockId="NoVoice" block="silence"
NoVoice = 1
}
export enum enIR {
//% blockId="Get" block="detected"
Get = 0,
//% blockId="NoVoice" block="undetected"
NoGet = 1
}
//% blockId=mbit_Voice_Sensor block="Voice_Sensor|pin %pin|value %value"
//% weight=100
//% blockGap=10
//% color="#87CEEB"
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function Voice_Sensor(pin: DigitalPin, value: enVoice): boolean {
pins.setPull(pin, PinPullMode.PullUp);
if (pins.digitalReadPin(pin) == value) {
return true;
}
else {
return false;
}
}
function IR_send_38k() {
for (let i: number = 0; i < 8; i++) {
pins.digitalWritePin(DigitalPin.P9, 1);
control.waitMicros(13);
pins.digitalWritePin(DigitalPin.P9, 0);
control.waitMicros(13);
}
}
//% blockId=mbit_IR_Sensor block="IR_Sensor|pin %pin| |%value|obstacle"
//% weight=100
//% blockGap=10
//% color="#87CEEB"
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function IR_Sensor(pin: DigitalPin, value: enIR): boolean {
pins.setPull(pin, PinPullMode.PullUp);
//IR_send_38k();
if (pins.digitalReadPin(pin) == value) {
return true;
}
else {
return false;
}
}
//% blockId=mbit_IR_Send block="IR_Send|pin %pin"
//% weight=100
//% blockGap=10
//% color="#87CEEB"
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function IR_Send(pin: DigitalPin): void {
IR_send_38k();
}
//% blockId=mbit_ultrasonic block="Ultrasonic|Trig %Trig|Echo %Echo"
//% color="#87CEEB"
//% weight=100
//% blockGap=10
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function Ultrasonic(Trig: DigitalPin, Echo: DigitalPin): number {
// send pulse
let list:Array<number> = [0, 0, 0, 0, 0];
for (let i = 0; i < 5; i++) {
pins.setPull(Trig, PinPullMode.PullNone);
pins.digitalWritePin(Trig, 0);
control.waitMicros(2);
pins.digitalWritePin(Trig, 1);
control.waitMicros(15);
pins.digitalWritePin(Trig, 0);
// read pulse
let d = pins.pulseIn(Echo, PulseValue.High, 43200);
list[i] = Math.floor(d / 40)
}
list.sort();
let length = (list[1] + list[2] + list[3])/3;
return Math.floor(length);
}
}
/*****************************************************************************************************************************************
* Input *****************************************************************************************************************************
****************************************************************************************************************************************/
//% color="#808080" weight=23 icon="\uf11c"
namespace mbit_Input {
export enum enRocker {
//% blockId="Nostate" block="No"
Nostate = 0,
//% blockId="Up" block="up"
Up,
//% blockId="Down" block="down"
Down,
//% blockId="Left" block="left"
Left,
//% blockId="Right" block="right"
Right,
//% blockId="Press" block="press"
Press
}
export enum enTouch {
//% blockId="NoTouch" block="untouched"
NoTouch = 0,
//% blockId="Touch" block="touched"
Touch = 1
}
export enum enButton {
//% blockId="Press" block="press"
Press = 0,
//% blockId="Realse" block="release"
Realse = 1
}
//% blockId=mbit_TouchPad block="TouchPad|pin %pin|value %value"
//% weight=100
//% blockGap=10
//% color="#808080"
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=5
export function TouchPad(pin: DigitalPin, value: enTouch): boolean {
pins.setPull(pin, PinPullMode.PullUp);
if (pins.digitalReadPin(pin) == value) {
return true;
}
else {
return false;
}
}
//% blockId=mbit_Rocker block="Rocker|VRX %pin1|VRY %pin2|SW %pin3|value %value"
//% weight=100
//% blockGap=10
//% color="#808080"
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=6
export function Rocker(pin1: AnalogPin, pin2: AnalogPin, pin3: DigitalPin, value: enRocker): boolean {
pins.setPull(pin3, PinPullMode.PullUp);
let x = pins.analogReadPin(pin1);
let y = pins.analogReadPin(pin2);
let z = pins.digitalReadPin(pin3);
let now_state = enRocker.Nostate;
if (x < 100) // 上
{
now_state = enRocker.Up;
}
else if (x > 700) //
{
now_state = enRocker.Down;
}
else // 左右
{
if (y < 100) //右
{
now_state = enRocker.Right;
}
else if (y > 700) //左
{
now_state = enRocker.Left;
}
}
if (z == 0)
now_state = enRocker.Press;
if (now_state == value)
return true;
else
return false;
}
//% blockId=mbit_Button block="Button|pin %pin|value %value"
//% weight=100
//% blockGap=10
//% color="#808080"
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=5
export function Button(pin: DigitalPin, value: enButton): boolean {
pins.setPull(pin, PinPullMode.PullUp);
if (pins.digitalReadPin(pin) == value) {
return true;
}
else {
return false;
}
}
}
/*****************************************************************************************************************************************
* 音乐类 *****************************************************************************************************************************
****************************************************************************************************************************************/
//% color="#D2691E" weight=22 icon="\uf001"
namespace mbit_Music {
export enum enBuzzer {
//% blockId="NoBeep" block="silence"
NoBeep = 0,
//% blockId="Beep" block="sound"
Beep
}
//% blockId=mbit_Buzzer block="Buzzer|pin %pin|value %value"
//% weight=100
//% blockGap=10
//% color="#D2691E"
//% value.min=0 value.max=1
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=8
export function Buzzer(pin: DigitalPin, value: enBuzzer): void {
pins.setPull(pin, PinPullMode.PullNone);
pins.digitalWritePin(pin, value);
}
}
/*****************************************************************************************************************************************
* 电机类 *****************************************************************************************************************************
****************************************************************************************************************************************/
//% color="#0000CD" weight=21 icon="\uf185"
namespace mbit_Motor {
//% blockId=mbit_Fan block="Fan|pin %pin|speed %value"
//% weight=100
//% blockGap=10
//% color="#0000CD"
//% value.min=0 value.max=1023
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=9
export function Fan(pin: AnalogPin, value: number): void {
pins.analogWritePin(pin, value);
}
//% blockId=mbit_Servo block="Servo|pin %pin|value %value"
//% weight=100
//% blockGap=10
//% color="#0000CD"
//% value.min=0 value.max=180
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=9
export function Servo(pin: AnalogPin, value: number): void {
pins.servoWritePin(pin, value);
}
}
//% color="#006400" weight=20 icon="\uf1b9"
namespace mbit_Robot {
const PCA9685_ADD = 0x41
const MODE1 = 0x00
const MODE2 = 0x01
const SUBADR1 = 0x02
const SUBADR2 = 0x03
const SUBADR3 = 0x04
const LED0_ON_L = 0x06
const LED0_ON_H = 0x07
const LED0_OFF_L = 0x08
const LED0_OFF_H = 0x09
const ALL_LED_ON_L = 0xFA
const ALL_LED_ON_H = 0xFB
const ALL_LED_OFF_L = 0xFC
const ALL_LED_OFF_H = 0xFD
const PRESCALE = 0xFE
let initialized = false
let yahStrip: neopixel.Strip;
export enum enColor {
//% blockId="OFF" block="off"
OFF = 0,
//% blockId="Red" block="red"
Red,
//% blockId="Green" block="green"
Green,
//% blockId="Blue" block="blue"
Blue,
//% blockId="White" block="white"
White,
//% blockId="Cyan" block="cyan"
Cyan,
//% blockId="Pinkish" block="magenta"
Pinkish,
//% blockId="Yellow" block="yellow"
Yellow,
}
export enum enMusic {
dadadum = 0,
entertainer,
prelude,
ode,
nyan,
ringtone,
funk,
blues,
birthday,
wedding,
funereal,
punchline,
baddy,
chase,
ba_ding,
wawawawaa,
jump_up,
jump_down,
power_up,
power_down
}
export enum enPos {
//% blockId="LeftState" block="left state"
LeftState = 0,
//% blockId="RightState" block="right state"
RightState = 1
}
export enum enLineState {
//% blockId="White" block="white"
White = 0,
//% blockId="Black" block="black"
Black = 1
}
export enum enAvoidState {
//% blockId="OBSTACLE" block="with obstacles"
OBSTACLE = 0,
//% blockId="NOOBSTACLE" block="without obstacles"
NOOBSTACLE = 1
}
export enum enServo {
S1 = 1,
S2,
S3
}
export enum CarState {
//% blockId="Car_Run" block="forward"
Car_Run = 1,
//% blockId="Car_Back" block="back"
Car_Back = 2,
//% blockId="Car_Left" block="turn left"
Car_Left = 3,
//% blockId="Car_Right" block="turn right"
Car_Right = 4,
//% blockId="Car_Stop" block="stop"
Car_Stop = 5,
//% blockId="Car_SpinLeft" block="rotate left"
Car_SpinLeft = 6,
//% blockId="Car_SpinRight" block="rotate right"
Car_SpinRight = 7
}
function i2cwrite(addr: number, reg: number, value: number) {
let buf = pins.createBuffer(2)
buf[0] = reg
buf[1] = value
pins.i2cWriteBuffer(addr, buf)
}
function i2ccmd(addr: number, value: number) {
let buf = pins.createBuffer(1)
buf[0] = value
pins.i2cWriteBuffer(addr, buf)
}
function i2cread(addr: number, reg: number) {
pins.i2cWriteNumber(addr, reg, NumberFormat.UInt8BE);
let val = pins.i2cReadNumber(addr, NumberFormat.UInt8BE);
return val;
}
function initPCA9685(): void {
i2cwrite(PCA9685_ADD, MODE1, 0x00)
setFreq(50);
initialized = true
}
function setFreq(freq: number): void {
// Constrain the frequency
let prescaleval = 25000000;
prescaleval /= 4096;
prescaleval /= freq;
prescaleval -= 1;
let prescale = prescaleval; //Math.Floor(prescaleval + 0.5);
let oldmode = i2cread(PCA9685_ADD, MODE1);
let newmode = (oldmode & 0x7F) | 0x10; // sleep
i2cwrite(PCA9685_ADD, MODE1, newmode); // go to sleep
i2cwrite(PCA9685_ADD, PRESCALE, prescale); // set the prescaler
i2cwrite(PCA9685_ADD, MODE1, oldmode);
control.waitMicros(5000);
i2cwrite(PCA9685_ADD, MODE1, oldmode | 0xa1);
}
function setPwm(channel: number, on: number, off: number): void {
if (channel < 0 || channel > 15)
return;
if (!initialized) {
initPCA9685();
}
let buf = pins.createBuffer(5);
buf[0] = LED0_ON_L + 4 * channel;
buf[1] = on & 0xff;
buf[2] = (on >> 8) & 0xff;
buf[3] = off & 0xff;
buf[4] = (off >> 8) & 0xff;
pins.i2cWriteBuffer(PCA9685_ADD, buf);
}
function Car_run(speed1: number, speed2: number) {
speed1 = speed1 * 16; // map 350 to 4096
speed2 = speed2 * 16;
if (speed1 >= 4096) {
speed1 = 4095
}
if (speed2 >= 4096) {
speed2 = 4095
}
setPwm(12, 0, speed1);
setPwm(13, 0, 0);
setPwm(15, 0, speed2);
setPwm(14, 0, 0);
//pins.digitalWritePin(DigitalPin.P16, 1);
// pins.analogWritePin(AnalogPin.P1, 1023-speed); //速度控制
// pins.analogWritePin(AnalogPin.P0, speed);//速度控制
// pins.digitalWritePin(DigitalPin.P8, 0);
}
function Car_back(speed1: number, speed2: number) {
speed1 = speed1 * 16; // map 350 to 4096
speed2 = speed2 * 16;
if (speed1 >= 4096) {
speed1 = 4095
}
if (speed2 >= 4096) {
speed2 = 4095
}
setPwm(12, 0, 0);
setPwm(13, 0, speed1);
setPwm(15, 0, 0);
setPwm(14, 0, speed2);
//pins.digitalWritePin(DigitalPin.P16, 0);
//pins.analogWritePin(AnalogPin.P1, speed); //速度控制
//pins.analogWritePin(AnalogPin.P0, 1023 - speed);//速度控制
//pins.digitalWritePin(DigitalPin.P8, 1);
}
function Car_left(speed1: number, speed2: number) {
speed1 = speed1 * 16; // map 350 to 4096
speed2 = speed2 * 16;
if (speed1 >= 4096) {
speed1 = 4095
}
if (speed2 >= 4096) {
speed2 = 4095
}
setPwm(12, 0, 0);
setPwm(13, 0, 0);
setPwm(15, 0, speed2);
setPwm(14, 0, 0);
//pins.analogWritePin(AnalogPin.P0, speed);
//pins.digitalWritePin(DigitalPin.P8, 0);
//pins.digitalWritePin(DigitalPin.P16, 0);
//pins.digitalWritePin(DigitalPin.P1, 0);
}
function Car_right(speed1: number, speed2: number) {
speed1 = speed1 * 16; // map 350 to 4096
speed2 = speed2 * 16;
if (speed1 >= 4096) {
speed1 = 4095
}
if (speed2 >= 4096) {
speed2 = 4095
}
setPwm(12, 0, speed1);
setPwm(13, 0, 0);
setPwm(15, 0, 0);
setPwm(14, 0, 0);
//pins.digitalWritePin(DigitalPin.P0, 0);
//pins.digitalWritePin(DigitalPin.P8, 0);
//pins.digitalWritePin(DigitalPin.P16, 1);
// pins.analogWritePin(AnalogPin.P1, 1023 - speed);
}
function Car_stop() {
setPwm(12, 0, 0);
setPwm(13, 0, 0);
setPwm(15, 0, 0);
setPwm(14, 0, 0);
//pins.digitalWritePin(DigitalPin.P0, 0);
//pins.digitalWritePin(DigitalPin.P8, 0);
//pins.digitalWritePin(DigitalPin.P16, 0);
//pins.digitalWritePin(DigitalPin.P1, 0);
}
function Car_spinleft(speed1: number, speed2: number) {
speed1 = speed1 * 16; // map 350 to 4096
speed2 = speed2 * 16;
if (speed1 >= 4096) {
speed1 = 4095
}
if (speed2 >= 4096) {
speed2 = 4095
}
setPwm(12, 0, 0);
setPwm(13, 0, speed1);
setPwm(15, 0, speed2);
setPwm(14, 0, 0);
//pins.analogWritePin(AnalogPin.P0, speed);
//pins.digitalWritePin(DigitalPin.P8, 0);
//pins.digitalWritePin(DigitalPin.P16, 0);
//pins.analogWritePin(AnalogPin.P1, speed);
}
function Car_spinright(speed1: number, speed2: number) {
speed1 = speed1 * 16; // map 350 to 4096
speed2 = speed2 * 16;
if (speed1 >= 4096) {
speed1 = 4095
}
if (speed2 >= 4096) {
speed2 = 4095
}
setPwm(12, 0, speed1);
setPwm(13, 0, 0);
setPwm(15, 0, 0);
setPwm(14, 0, speed2);
//pins.analogWritePin(AnalogPin.P0, 1023-speed);
//pins.digitalWritePin(DigitalPin.P8, 1);
//pins.digitalWritePin(DigitalPin.P16, 1);
//pins.analogWritePin(AnalogPin.P1, 1023-speed);
}
/**
* *****************************************************************
* @param index
*/
//% blockId=mbit_RGB_Car_Big2 block="RGB_Car_Big2|value %value"
//% weight=101
//% blockGap=10
//% color="#C814B8"
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function RGB_Car_Big2(value: enColor): void {
switch (value) {
case enColor.OFF: {
setPwm(0, 0, 0);
setPwm(1, 0, 0);
setPwm(2, 0, 0);
break;
}
case enColor.Red: {
setPwm(0, 0, 4095);
setPwm(1, 0, 0);
setPwm(2, 0, 0);
break;
}
case enColor.Green: {
setPwm(0, 0, 0);
setPwm(1, 0, 4095);
setPwm(2, 0, 0);
break;
}
case enColor.Blue: {
setPwm(0, 0, 0);
setPwm(1, 0, 0);
setPwm(2, 0, 4095);
break;
}
case enColor.White: {
setPwm(0, 0, 4095);
setPwm(1, 0, 4095);
setPwm(2, 0, 4095);
break;
}
case enColor.Cyan: {
setPwm(0, 0, 0);
setPwm(1, 0, 4095);
setPwm(2, 0, 4095);
break;
}
case enColor.Pinkish: {
setPwm(0, 0, 4095);
setPwm(1, 0, 0);
setPwm(2, 0, 4095);
break;
}
case enColor.Yellow: {
setPwm(0, 0, 4095);
setPwm(1, 0, 4095);
setPwm(2, 0, 0);
break;
}
}
}
//% blockId=mbit_RGB_Car_Big block="RGB_Car_Big|value1 %value1|value2 %value2|value3 %value3"
//% weight=100
//% blockGap=10
//% color="#C814B8"
//% value1.min=0 value1.max=255 value2.min=0 value2.max=255 value3.min=0 value3.max=255
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function RGB_Car_Big(value1: number, value2: number, value3: number): void {
let R = value1 * 16;
let G = value2 * 16;
let B = value3 * 16;
if (R > 4096)
R = 4095;
if (G > 4096)
G = 4095;
if (B > 4096)
B = 4095;
setPwm(0, 0, R);
setPwm(1, 0, G);
setPwm(2, 0, B);
}
//% blockId=mbit_RGB_Car_Program block="RGB_Car_Program"
//% weight=99
//% blockGap=10
//% color="#C814B8"
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function RGB_Car_Program(): neopixel.Strip {
if (!yahStrip) {
yahStrip = neopixel.create(DigitalPin.P16, 3, NeoPixelMode.RGB);
}
return yahStrip;
}
//% blockId=mbit_ultrasonic_car block="ultrasonic return distance(cm)"
//% color="#006400"
//% weight=98
//% blockGap=10
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function Ultrasonic_Car(): number {
// send pulse
let list:Array<number> = [0, 0, 0, 0, 0];
for (let i = 0; i < 5; i++) {
pins.setPull(DigitalPin.P14, PinPullMode.PullNone);
pins.digitalWritePin(DigitalPin.P14, 0);
control.waitMicros(2);
pins.digitalWritePin(DigitalPin.P14, 1);
control.waitMicros(15);
pins.digitalWritePin(DigitalPin.P14, 0);
let d = pins.pulseIn(DigitalPin.P15, PulseValue.High, 43200);
list[i] = Math.floor(d / 40)
}
list.sort();
let length = (list[1] + list[2] + list[3])/3;
return Math.floor(length);
}
//% blockId=mbit_Music_Car block="Music_Car|%index"
//% weight=97
//% blockGap=10
//% color="#006400"
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function Music_Car(index: enMusic): void {
switch (index) {
case enMusic.dadadum: music.beginMelody(music.builtInMelody(Melodies.Dadadadum), MelodyOptions.Once); break;
case enMusic.birthday: music.beginMelody(music.builtInMelody(Melodies.Birthday), MelodyOptions.Once); break;
case enMusic.entertainer: music.beginMelody(music.builtInMelody(Melodies.Entertainer), MelodyOptions.Once); break;
case enMusic.prelude: music.beginMelody(music.builtInMelody(Melodies.Prelude), MelodyOptions.Once); break;
case enMusic.ode: music.beginMelody(music.builtInMelody(Melodies.Ode), MelodyOptions.Once); break;
case enMusic.nyan: music.beginMelody(music.builtInMelody(Melodies.Nyan), MelodyOptions.Once); break;
case enMusic.ringtone: music.beginMelody(music.builtInMelody(Melodies.Ringtone), MelodyOptions.Once); break;
case enMusic.funk: music.beginMelody(music.builtInMelody(Melodies.Funk), MelodyOptions.Once); break;
case enMusic.blues: music.beginMelody(music.builtInMelody(Melodies.Blues), MelodyOptions.Once); break;
case enMusic.wedding: music.beginMelody(music.builtInMelody(Melodies.Wedding), MelodyOptions.Once); break;
case enMusic.funereal: music.beginMelody(music.builtInMelody(Melodies.Funeral), MelodyOptions.Once); break;
case enMusic.punchline: music.beginMelody(music.builtInMelody(Melodies.Punchline), MelodyOptions.Once); break;
case enMusic.baddy: music.beginMelody(music.builtInMelody(Melodies.Baddy), MelodyOptions.Once); break;
case enMusic.chase: music.beginMelody(music.builtInMelody(Melodies.Chase), MelodyOptions.Once); break;
case enMusic.ba_ding: music.beginMelody(music.builtInMelody(Melodies.BaDing), MelodyOptions.Once); break;
case enMusic.wawawawaa: music.beginMelody(music.builtInMelody(Melodies.Wawawawaa), MelodyOptions.Once); break;
case enMusic.jump_up: music.beginMelody(music.builtInMelody(Melodies.JumpUp), MelodyOptions.Once); break;
case enMusic.jump_down: music.beginMelody(music.builtInMelody(Melodies.JumpDown), MelodyOptions.Once); break;
case enMusic.power_up: music.beginMelody(music.builtInMelody(Melodies.PowerUp), MelodyOptions.Once); break;
case enMusic.power_down: music.beginMelody(music.builtInMelody(Melodies.PowerDown), MelodyOptions.Once); break;
}
}
//% blockId=mbit_Servo_Car block="Servo_Car|num %num|value %value"
//% weight=96
//% blockGap=10
//% color="#006400"
//% num.min=1 num.max=3 value.min=0 value.max=180
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=9
export function Servo_Car(num: enServo, value: number): void {
// 50hz: 20,000 us
let us = (value * 1800 / 180 + 600); // 0.6 ~ 2.4
let pwm = us * 4096 / 20000;
setPwm(num + 2, 0, pwm);
}
//% blockId=mbit_Avoid_Sensor block="Avoid_Sensor|value %value"
//% weight=95
//% blockGap=10
//% color="#006400"
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=12
export function Avoid_Sensor(value: enAvoidState): boolean {
let temp: boolean = false;
pins.digitalWritePin(DigitalPin.P9, 0);
switch (value) {
case enAvoidState.OBSTACLE: {
if (pins.analogReadPin(AnalogPin.P3) < 800) {
temp = true;
setPwm(8, 0, 0);
}
else {
temp = false;
setPwm(8, 0, 4095);
}
break;
}
case enAvoidState.NOOBSTACLE: {
if (pins.analogReadPin(AnalogPin.P3) > 800) {
temp = true;
setPwm(8, 0, 4095);
}
else {
temp = false;
setPwm(8, 0, 0);
}
break;
}
}
pins.digitalWritePin(DigitalPin.P9, 1);
return temp;
}
//% blockId=mbit_Line_Sensor block="Line_Sensor|direct %direct|value %value"
//% weight=94
//% blockGap=10
//% color="#006400"
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=12
export function Line_Sensor(direct: enPos, value: enLineState): boolean {
let temp: boolean = false;
switch (direct) {