From dceee610198cae2022f8d8ccc2a96b3114098cbb Mon Sep 17 00:00:00 2001 From: darraghomalley <97785646+darraghomalley@users.noreply.github.com> Date: Mon, 9 Dec 2024 19:17:59 +0000 Subject: [PATCH] Update pxt.json, CuteBotController.md, main.py --- CuteBotController.md | 140 +++++++++++++++---------------------------- main.py | 1 + pxt.json | 5 +- 3 files changed, 53 insertions(+), 93 deletions(-) create mode 100644 main.py diff --git a/CuteBotController.md b/CuteBotController.md index 4c6e8d6..c31148c 100644 --- a/CuteBotController.md +++ b/CuteBotController.md @@ -2,27 +2,29 @@ # STEM - CuteBot Controller ## Let's build a robot controller @showdialog Let's build a micro:bit project that will drive a CuteBot by sending messages over radio signal. -To help you out, we've created some variables that you will need later: ``||variables: Forwards, Reverse, Left, Right, Stop||``. +To help you out, we've created some variables that you will need later: ``||variables: Forwards, Backwards, Left, Right, Stop||``. ```validation.global # BlocksExistValidator ``` ```template let Forwards = "FORWARDS" -let Reverse = "REVERSE" +let Backwards = "BACKWARDS" let Left = "LEFT" let Right = "RIGHT" let Stop = "STOP" let Direction = "STOP" +let Rainbow = "RAINBOW" ``` ```blocks let Forwards = "FORWARDS" -let Reverse = "REVERSE" +let Backwards = "BACKWARDS" let Left = "LEFT" let Right = "RIGHT" let Stop = "STOP" let Direction = "STOP" +let Rainbow = "RAINBOW" ``` -## Show your micro:bit name +## Show your micro:bit name Let's start by dragging a ``||basic:Basic:show string||`` block into the top of ``||basic:on start||``. Drag ``||control:Control:device name||`` into to ``||basic:show string||``; this will show your micro:bit's name. @@ -30,11 +32,12 @@ Drag ``||control:Control:device name||`` into to ``||basic:show string||``; this // @highlight basic.showString(control.deviceName()) let Forwards = "FORWARDS" -let Reverse = "REVERSE" +let Backwards = "BACKWARDS" let Left = "LEFT" let Right = "RIGHT" let Stop = "STOP" let Direction = "STOP" +let Rainbow = "RAINBOW" ``` ## Set your radio group Drag a ``||radio:Radio:set group||`` block into ``||basic:on start||``; enter your radio group number. Use a ``||basic:Basic:show string||`` block to show your radio group number. @@ -45,11 +48,12 @@ radio.setGroup(1) //@highlight basic.showString("1") let Forwards = "FORWARDS" -let Reverse = "REVERSE" +let Backwards = "BACKWARDS" let Left = "LEFT" let Right = "RIGHT" let Stop = "STOP" let Direction = "STOP" +let Rainbow = "RAINBOW" ``` ## Add a Loop block to your canvas To control your CuteBot, drag a ``||loops:Loops:every 500ms||`` block onto your canvas, change the 500ms to 100ms. @@ -61,8 +65,8 @@ loops.everyInterval(100, function () { ## Set direction to stop Set your ``||variables:Direction||`` to ``||variables:Stop||``. To know which way to send your micro:bit, in the following steps we will check if A, B or A+B are pressed. We will also check if the micro:bit is tilted backwards. ```blocks -// @highlight loops.everyInterval(100, function () { +// @highlight let Direction = Stop }) ``` @@ -70,9 +74,9 @@ let Direction = Stop Drag an ``||logic:if true then||`` block into the ``||loops:every 100ms||`` block. Drag a ``||input:button A is pressed||`` block into the ``||logic:if true then||`` block. Drag a ``||variables:set Direction = 0||`` block inside the ``||logic:if||`` statement. Replace the 0 with ``||variables:Left||``. ```blocks -// @highlight loops.everyInterval(100, function () { let Direction = Stop + // @highlight if (input.buttonIsPressed(Button.A)) { Direction = Left } @@ -86,6 +90,7 @@ let Direction = Stop if (input.buttonIsPressed(Button.A)) { Direction = Left } + // @highlight if (input.buttonIsPressed(Button.B)) { Direction = Right } @@ -102,118 +107,71 @@ let Direction = Stop if (input.buttonIsPressed(Button.B)) { Direction = Right } + // @highlight if (input.buttonIsPressed(Button.AB)) { Direction = Forwards } }) ``` -## Check for tilt backwards (reverse) +## Check for tilt backwards (Backwards) Drag an ``||logic:if true then||`` block into the bottom of the ``||loops:every 100ms||`` block. Drag a ``||logic:Logic:0>0||`` comparision block into the ``||logic:if||`` block. Drag ``||input:Input:rotation||`` into left side of the number comparision and set to ">" 60 (greater than 60). -Drag ``||variables:Variables: set xyz = 0||`` inside the ``||logic:if||`` block and set ``||variables:Direction||`` to ``||variables:Reverse||``. +Drag ``||variables:Variables: set xyz = 0||`` inside the ``||logic:if||`` block and set ``||variables:Direction||`` to ``||variables:Backwards||``. ```blocks loops.everyInterval(100, function () { - if (input.buttonIsPressed(Button.AB)) { - Direction = Forwards +let Direction = Stop + if (input.buttonIsPressed(Button.A)) { + Direction = Left } - if (input.rotation(Rotation.Pitch) > 60) { - Direction = Reverse + if (input.buttonIsPressed(Button.B)) { + Direction = Right } -}) -``` -## Step 6 - Add radio send "Backwards" -Drag ``||radio:Radio:send string||`` into the if-block and send the value "Backwards" -```blocks -loops.everyInterval(100, function () { - if (input.rotation(Rotation.Pitch) > 60) { - radio.sendString("Backwards") + + if (input.buttonIsPressed(Button.AB)) { + Direction = Forwards } -}) -``` -## Step 7 - Click "+" twice to show an else-if placeholder -Click "+" at the bottom of the if-block twice to show an else-if placeholder -```blocks -loops.everyInterval(100, function () { + // @highlight if (input.rotation(Rotation.Pitch) > 60) { - radio.sendString("Backwards") - } else if (true) { - - } else { - + Direction = Backwards } }) ``` -## Step 8 - Add button A+B is pressed to go forwards -Add ``||input:Input:button A+B is pressed||`` to the else-if and add a ``||radio:Radio:send string||`` block to send "Forwards" +## Finally, let's send our command to the Cutebot +Drag ``||radio:Radio:send string||`` into the bottom of the the ``||loops:loops.everyInterval(100)||`` block. +Set the message to ``||variables:Direction||``; this will send a direction command to the CuteBot. ```blocks loops.everyInterval(100, function () { - if (input.rotation(Rotation.Pitch) == 60) { - radio.sendString("Backwards") - } else if (input.buttonIsPressed(Button.AB)) { - radio.sendString("Forwards") - } else { - +let Direction = Stop + if (input.buttonIsPressed(Button.A)) { + Direction = Left } -}) -``` -## Step 9 - Add button A is pressed to go left -Add ``||input:Input:button A is pressed||`` to the else-if and add a ``||radio:Radio:send string||`` block to send "Left" -```blocks -loops.everyInterval(100, function () { - if (input.rotation(Rotation.Pitch) > 60) { - radio.sendString("Backwards") - } else if (input.buttonIsPressed(Button.AB)) { - radio.sendString("Forwards") - } else if (input.buttonIsPressed(Button.A)) { - radio.sendString("Left") - - } else { + if (input.buttonIsPressed(Button.B)) { + Direction = Right } -}) -``` -## Step 10 - Add button B is pressed to go right -Add ``||input:Input:button B is pressed||`` to the else-if and add a ``||radio:Radio:send string||`` block to send "Right" -```blocks -loops.everyInterval(100, function () { - if (input.rotation(Rotation.Pitch) > 60) { - radio.sendString("Backwards") - } else if (input.buttonIsPressed(Button.AB)) { - radio.sendString("Forwards") - } else if (input.buttonIsPressed(Button.A)) { - radio.sendString("Left") - } else if (input.buttonIsPressed(Button.B)) { - radio.sendString("Right") - - } else { + + if (input.buttonIsPressed(Button.AB)) { + Direction = Forwards } -}) -``` -## Step 11 - Complete the if-block by adding the "Stop" radio message -Complete the if-block by adding a ``||radio:Radio:send string||`` block to send "Stop" -```blocks -loops.everyInterval(100, function () { if (input.rotation(Rotation.Pitch) > 60) { - radio.sendString("Backwards") - } else if (input.buttonIsPressed(Button.AB)) { - radio.sendString("Forwards") - } else if (input.buttonIsPressed(Button.A)) { - radio.sendString("Left") - } else if (input.buttonIsPressed(Button.B)) { - radio.sendString("Right") - } else { - radio.sendString("Stop") + Direction = Backwards } + // @highlight + radio.sendString(Direction) }) ``` -## Step 12 - Add radio send string "Rainbow" to on logo pressed -To flash the Cutebot's lights, drag a ``||input:Input:on logo pressed||`` block onto canvas and use ``||radio:Radio:send string||`` to send "Rainbow" +## 100ms Frequency @showdialog +Your micro:bit will send a Direction message to your CuteBot every 100 milliseconds, that's 10 times a second. Wow, that's really fast, try saying forwards 10 times in 1 second. + +## Let's also send a "Rainbow" lights command to the CuteBot by using on logo pressed +To flash the Cutebot's lights, drag a ``||input:Input:on logo pressed||`` block onto canvas and use ``||radio:Radio:send string||`` to send the variable ``||variables:Rainbow||`` ```blocks +// @highlight input.onLogoEvent(TouchButtonEvent.Pressed, function () { - radio.sendString("RAINBOW") + radio.sendString(Rainbow) }) ``` -## Step 13 - download and test -Click ``|Download|`` to download your code onto your micro:bit to see it working +## Download and test - good luck and happy racing!!! +Click ``|Download|`` to download your code onto your micro:bit to see it working. Good luck and happy racing! diff --git a/main.py b/main.py new file mode 100644 index 0000000..f8d8acd --- /dev/null +++ b/main.py @@ -0,0 +1 @@ +# leave empty \ No newline at end of file diff --git a/pxt.json b/pxt.json index e106f44..1286470 100644 --- a/pxt.json +++ b/pxt.json @@ -1,6 +1,6 @@ { "name": "Tutorials", - "version": "0.1.26", + "version": "0.1.27", "description": "", "dependencies": { "core": "*", @@ -13,7 +13,8 @@ "CodeClubBadge.md", "QuickDraw.md", "CuteBotController.md", - "tutorial-info-cache.json" + "tutorial-info-cache.json", + "main.py" ], "testFiles": [ "test.ts"