Guest User

KS4036F

a guest
Sep 15th, 2025
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 13.07 KB | Source Code | 0 0
  1. function Play () {
  2.     music.play(music.tonePlayable(196, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
  3.     images.iconImage(IconNames.Square).showImage(0)
  4.     music.play(music.tonePlayable(659, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
  5.     music.play(music.tonePlayable(988, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
  6.     record.playAudio(record.BlockingState.Blocking)
  7.     music.play(music.tonePlayable(988, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
  8.     music.play(music.tonePlayable(659, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
  9.     control.waitMicros(100000)
  10.     IR_Pressed = irRemote.returnIrButton()
  11. }
  12. function Record () {
  13.     music.play(music.tonePlayable(196, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
  14.     images.iconImage(IconNames.SmallSquare).showImage(0)
  15.     music.play(music.tonePlayable(220, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
  16.     music.play(music.tonePlayable(330, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
  17.     record.startRecording(record.BlockingState.Blocking)
  18.     music.play(music.tonePlayable(330, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
  19.     music.play(music.tonePlayable(220, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
  20.     control.waitMicros(100000)
  21.     IR_Pressed = irRemote.returnIrButton()
  22. }
  23. function Obstacle () {
  24.     MiniCar.led_rgb(LED_rgb_L_R.LED_L, LED_color.cyan)
  25.     MiniCar.led_rgb(LED_rgb_L_R.LED_R, LED_color.cyan)
  26.     music.play(music.tonePlayable(196, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
  27.     images.iconImage(IconNames.Butterfly).showImage(0)
  28.     control.waitMicros(100000)
  29.     IR_Pressed = irRemote.returnIrButton()
  30.     while (IR_Pressed != 12) {
  31.         IR_Pressed = irRemote.returnIrButton()
  32.         distance = MiniCar.ultra()
  33.         if (distance > 20) {
  34.             MiniCar.led_rgb(LED_rgb_L_R.LED_L, LED_color.cyan)
  35.             MiniCar.led_rgb(LED_rgb_L_R.LED_R, LED_color.cyan)
  36.             MiniCar.motor(Motorlist.M1, Direction1.Forward, GlobalAvoidSpeed)
  37.             MiniCar.motor(Motorlist.M2, Direction1.Forward, GlobalAvoidSpeed)
  38.         } else {
  39.             MiniCar.motor(Motorlist.M1, Direction1.Backward, GlobalAvoidSpeed)
  40.             MiniCar.motor(Motorlist.M2, Direction1.Forward, GlobalAvoidSpeed)
  41.             MiniCar.led_rgb(LED_rgb_L_R.LED_L, LED_color.red1)
  42.             MiniCar.led_rgb(LED_rgb_L_R.LED_R, LED_color.red1)
  43.             basic.pause(500)
  44.         }
  45.     }
  46. }
  47. function GreetName () {
  48.     basic.clearScreen()
  49.     music.play(music.tonePlayable(196, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
  50.     if (NameChoice == 1) {
  51.         MiniCar.led_rgb(LED_rgb_L_R.LED_L, LED_color.green1)
  52.         MiniCar.led_rgb(LED_rgb_L_R.LED_R, LED_color.green1)
  53.         basic.showString("Hello Name1!")
  54.     } else if (NameChoice == 2) {
  55.         MiniCar.led_rgb(LED_rgb_L_R.LED_L, LED_color.yellow)
  56.         MiniCar.led_rgb(LED_rgb_L_R.LED_R, LED_color.yellow)
  57.         basic.showString("Hello Name2!")
  58.     } else if (NameChoice == 3) {
  59.         MiniCar.led_rgb(LED_rgb_L_R.LED_L, LED_color.purple)
  60.         MiniCar.led_rgb(LED_rgb_L_R.LED_R, LED_color.purple)
  61.         basic.showString("Hello Name3!")
  62.     }
  63.     music.play(music.tonePlayable(175, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
  64.     MiniCar.led_rgb(LED_rgb_L_R.LED_L, LED_color.black)
  65.     MiniCar.led_rgb(LED_rgb_L_R.LED_R, LED_color.black)
  66.     control.waitMicros(GlobalWait)
  67.     NameChoice = 0
  68.     IR_Pressed = irRemote.returnIrButton()
  69. }
  70. function LightFollow () {
  71.     music.play(music.tonePlayable(196, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
  72.     images.iconImage(IconNames.Happy).showImage(0)
  73.     control.waitMicros(100000)
  74.     IR_Pressed = irRemote.returnIrButton()
  75.     while (IR_Pressed != 24) {
  76.         IR_Pressed = irRemote.returnIrButton()
  77.         ldr_l = MiniCar.PH1()
  78.         ldr_r = MiniCar.PH2()
  79.         if (ldr_l > 650 && ldr_r > 650) {
  80.             MiniCar.motor(Motorlist.M1, Direction1.Forward, 100)
  81.             MiniCar.motor(Motorlist.M2, Direction1.Forward, 100)
  82.         } else if (ldr_l > 650 && ldr_r <= 650) {
  83.             MiniCar.motor(Motorlist.M1, Direction1.Backward, 100)
  84.             MiniCar.motor(Motorlist.M2, Direction1.Forward, 100)
  85.         } else if (ldr_l <= 650 && ldr_r > 650) {
  86.             MiniCar.motor(Motorlist.M1, Direction1.Forward, 100)
  87.             MiniCar.motor(Motorlist.M2, Direction1.Backward, 100)
  88.         } else {
  89.             MiniCar.motor(Motorlist.M1, Direction1.Backward, 0)
  90.             MiniCar.motor(Motorlist.M2, Direction1.Backward, 0)
  91.         }
  92.     }
  93. }
  94. function ExitMode () {
  95.     music.play(music.tonePlayable(175, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
  96.     MiniCar.led_rgb(LED_rgb_L_R.LED_L, LED_color.black)
  97.     MiniCar.led_rgb(LED_rgb_L_R.LED_R, LED_color.black)
  98.     MiniCar.motor(Motorlist.M1, Direction1.Backward, 0)
  99.     MiniCar.motor(Motorlist.M2, Direction1.Backward, 0)
  100.     control.waitMicros(GlobalWait)
  101.     IR_Pressed = irRemote.returnIrButton()
  102. }
  103. function DriveHold () {
  104.     MiniCar.led_rgb(LED_rgb_L_R.LED_L, LED_color.blue1)
  105.     MiniCar.led_rgb(LED_rgb_L_R.LED_R, LED_color.blue1)
  106.     music.play(music.tonePlayable(196, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
  107.     images.iconImage(IconNames.Triangle).showImage(0)
  108.     control.waitMicros(GlobalWait)
  109.     IR_Pressed = irRemote.returnIrButton()
  110.     while (IR_Pressed != 25) {
  111.         IR_Pressed = irRemote.returnIrButton()
  112.         while (IR_Pressed == 70) {
  113.             MiniCar.motor(Motorlist.M1, Direction1.Forward, GlobalSpeed)
  114.             MiniCar.motor(Motorlist.M2, Direction1.Forward, GlobalSpeed)
  115.             MiniCar.led_rgb(LED_rgb_L_R.LED_L, LED_color.white)
  116.             MiniCar.led_rgb(LED_rgb_L_R.LED_R, LED_color.white)
  117.             control.waitMicros(GlobalWait)
  118.             IR_Pressed = irRemote.returnIrButton()
  119.         }
  120.         while (IR_Pressed == 68) {
  121.             MiniCar.motor(Motorlist.M1, Direction1.Forward, GlobalTurnLow)
  122.             MiniCar.motor(Motorlist.M2, Direction1.Forward, GlobalTurnHigh)
  123.             MiniCar.led_rgb(LED_rgb_L_R.LED_L, LED_color.black)
  124.             MiniCar.led_rgb(LED_rgb_L_R.LED_R, LED_color.yellow)
  125.             control.waitMicros(GlobalWait)
  126.             IR_Pressed = irRemote.returnIrButton()
  127.         }
  128.         while (IR_Pressed == 67) {
  129.             MiniCar.motor(Motorlist.M1, Direction1.Forward, GlobalTurnHigh)
  130.             MiniCar.motor(Motorlist.M2, Direction1.Forward, GlobalTurnLow)
  131.             MiniCar.led_rgb(LED_rgb_L_R.LED_L, LED_color.yellow)
  132.             MiniCar.led_rgb(LED_rgb_L_R.LED_R, LED_color.black)
  133.             control.waitMicros(GlobalWait)
  134.             IR_Pressed = irRemote.returnIrButton()
  135.         }
  136.         while (IR_Pressed == 21) {
  137.             MiniCar.motor(Motorlist.M1, Direction1.Backward, GlobalSpeed)
  138.             MiniCar.motor(Motorlist.M2, Direction1.Backward, GlobalSpeed)
  139.             MiniCar.led_rgb(LED_rgb_L_R.LED_L, LED_color.red1)
  140.             MiniCar.led_rgb(LED_rgb_L_R.LED_R, LED_color.red1)
  141.             control.waitMicros(GlobalWait)
  142.             IR_Pressed = irRemote.returnIrButton()
  143.         }
  144.         MiniCar.motor(Motorlist.M1, Direction1.Backward, 0)
  145.         MiniCar.motor(Motorlist.M2, Direction1.Backward, 0)
  146.         MiniCar.led_rgb(LED_rgb_L_R.LED_L, LED_color.blue1)
  147.         MiniCar.led_rgb(LED_rgb_L_R.LED_R, LED_color.blue1)
  148.     }
  149. }
  150. function FollowMe () {
  151.     MiniCar.led_rgb(LED_rgb_L_R.LED_L, LED_color.green1)
  152.     MiniCar.led_rgb(LED_rgb_L_R.LED_R, LED_color.green1)
  153.     music.play(music.tonePlayable(196, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
  154.     images.iconImage(IconNames.Diamond).showImage(0)
  155.     control.waitMicros(GlobalWait)
  156.     IR_Pressed = irRemote.returnIrButton()
  157.     while (IR_Pressed != 13) {
  158.         IR_Pressed = irRemote.returnIrButton()
  159.         distance = MiniCar.ultra()
  160.         if (distance >= 10 && distance <= 30) {
  161.             MiniCar.motor(Motorlist.M1, Direction1.Forward, GlobalSpeed)
  162.             MiniCar.motor(Motorlist.M2, Direction1.Forward, GlobalSpeed)
  163.         } else if (distance <= 6) {
  164.             MiniCar.motor(Motorlist.M1, Direction1.Backward, GlobalSpeed)
  165.             MiniCar.motor(Motorlist.M2, Direction1.Backward, GlobalSpeed)
  166.         } else if (distance < 10 && distance > 6 || distance > 30) {
  167.             MiniCar.motor(Motorlist.M1, Direction1.Backward, 0)
  168.             MiniCar.motor(Motorlist.M2, Direction1.Backward, 0)
  169.             basic.pause(200)
  170.         }
  171.     }
  172. }
  173. function LineTrack () {
  174.     music.play(music.tonePlayable(196, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
  175.     images.iconImage(IconNames.Ghost).showImage(0)
  176.     control.waitMicros(100000)
  177.     IR_Pressed = irRemote.returnIrButton()
  178.     while (IR_Pressed != 94) {
  179.         IR_Pressed = irRemote.returnIrButton()
  180.         value = MiniCar.LineTracking()
  181.         if (value == 1) {
  182.             MiniCar.motor(Motorlist.M1, Direction1.Forward, GlobalLineTurnSpeed)
  183.             MiniCar.motor(Motorlist.M2, Direction1.Backward, GlobalLineTurnSpeed)
  184.         } else if (value == 2) {
  185.             MiniCar.motor(Motorlist.M1, Direction1.Backward, GlobalLineTurnSpeed)
  186.             MiniCar.motor(Motorlist.M2, Direction1.Forward, GlobalLineTurnSpeed)
  187.         } else {
  188.             MiniCar.motor(Motorlist.M1, Direction1.Forward, GlobalLineSpeed)
  189.             MiniCar.motor(Motorlist.M2, Direction1.Forward, GlobalLineSpeed)
  190.         }
  191.     }
  192. }
  193. function Drive () {
  194.     MiniCar.led_rgb(LED_rgb_L_R.LED_L, LED_color.blue1)
  195.     MiniCar.led_rgb(LED_rgb_L_R.LED_R, LED_color.blue1)
  196.     music.play(music.tonePlayable(196, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
  197.     images.iconImage(IconNames.Tortoise).showImage(0)
  198.     control.waitMicros(GlobalWait)
  199.     IR_Pressed = irRemote.returnIrButton()
  200.     while (IR_Pressed != 22) {
  201.         IR_Pressed = irRemote.returnIrButton()
  202.         if (IR_Pressed == 70) {
  203.             MiniCar.motor(Motorlist.M1, Direction1.Forward, GlobalSpeed)
  204.             MiniCar.motor(Motorlist.M2, Direction1.Forward, GlobalSpeed)
  205.             MiniCar.led_rgb(LED_rgb_L_R.LED_L, LED_color.white)
  206.             MiniCar.led_rgb(LED_rgb_L_R.LED_R, LED_color.white)
  207.         } else if (IR_Pressed == 68) {
  208.             MiniCar.motor(Motorlist.M1, Direction1.Forward, GlobalTurnLow)
  209.             MiniCar.motor(Motorlist.M2, Direction1.Forward, GlobalTurnHigh)
  210.             MiniCar.led_rgb(LED_rgb_L_R.LED_L, LED_color.black)
  211.             MiniCar.led_rgb(LED_rgb_L_R.LED_R, LED_color.yellow)
  212.         } else if (IR_Pressed == 67) {
  213.             MiniCar.motor(Motorlist.M1, Direction1.Forward, GlobalTurnHigh)
  214.             MiniCar.motor(Motorlist.M2, Direction1.Forward, GlobalTurnLow)
  215.             MiniCar.led_rgb(LED_rgb_L_R.LED_L, LED_color.yellow)
  216.             MiniCar.led_rgb(LED_rgb_L_R.LED_R, LED_color.black)
  217.         } else if (IR_Pressed == 21) {
  218.             MiniCar.motor(Motorlist.M1, Direction1.Backward, GlobalSpeed)
  219.             MiniCar.motor(Motorlist.M2, Direction1.Backward, GlobalSpeed)
  220.             MiniCar.led_rgb(LED_rgb_L_R.LED_L, LED_color.red1)
  221.             MiniCar.led_rgb(LED_rgb_L_R.LED_R, LED_color.red1)
  222.         } else if (IR_Pressed == 64) {
  223.             MiniCar.motor(Motorlist.M1, Direction1.Backward, 0)
  224.             MiniCar.motor(Motorlist.M2, Direction1.Backward, 0)
  225.             MiniCar.led_rgb(LED_rgb_L_R.LED_L, LED_color.blue1)
  226.             MiniCar.led_rgb(LED_rgb_L_R.LED_R, LED_color.blue1)
  227.         }
  228.     }
  229. }
  230. let value = 0
  231. let ldr_r = 0
  232. let ldr_l = 0
  233. let distance = 0
  234. let IR_Pressed = 0
  235. let NameChoice = 0
  236. let GlobalLineTurnSpeed = 0
  237. let GlobalLineSpeed = 0
  238. let GlobalAvoidSpeed = 0
  239. let GlobalTurnHigh = 0
  240. let GlobalTurnLow = 0
  241. let GlobalSpeed = 0
  242. let GlobalWait = 0
  243. music.play(music.stringPlayable("F - F E F - A B ", 400), music.PlaybackMode.InBackground)
  244. GlobalWait = 100000
  245. GlobalSpeed = 100
  246. GlobalTurnLow = 30
  247. GlobalTurnHigh = 100
  248. GlobalAvoidSpeed = 70
  249. GlobalLineSpeed = 70
  250. GlobalLineTurnSpeed = 40
  251. NameChoice = 0
  252. IR_Pressed = 0
  253. irRemote.connectInfrared(DigitalPin.P16)
  254. record.setMicGain(record.AudioLevels.Low)
  255. record.setSampleRate(11000)
  256. basic.forever(function () {
  257.     IR_Pressed = irRemote.returnIrButton()
  258.     if (IR_Pressed == 22) {
  259.         Drive()
  260.         ExitMode()
  261.     }
  262.     if (IR_Pressed == 25) {
  263.         DriveHold()
  264.         ExitMode()
  265.     }
  266.     if (IR_Pressed == 13) {
  267.         FollowMe()
  268.         ExitMode()
  269.     }
  270.     if (IR_Pressed == 12) {
  271.         Obstacle()
  272.         ExitMode()
  273.     }
  274.     if (IR_Pressed == 24) {
  275.         LightFollow()
  276.         ExitMode()
  277.     }
  278.     if (IR_Pressed == 94) {
  279.         LineTrack()
  280.         ExitMode()
  281.     }
  282.     if (IR_Pressed == 8) {
  283.         NameChoice = 1
  284.         GreetName()
  285.     }
  286.     if (IR_Pressed == 28) {
  287.         NameChoice = 2
  288.         GreetName()
  289.     }
  290.     if (IR_Pressed == 90) {
  291.         NameChoice = 3
  292.         GreetName()
  293.     }
  294.     if (IR_Pressed == 66) {
  295.         Record()
  296.     }
  297.     if (IR_Pressed == 74) {
  298.         Play()
  299.     }
  300.     images.iconImage(IconNames.Heart).showImage(0)
  301. })
  302.  
Advertisement
Add Comment
Please, Sign In to add comment