Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 36 additions & 89 deletions CuteBotController.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,42 @@
# 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.

```blocks
// @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.
Expand All @@ -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.
Expand Down Expand Up @@ -109,11 +113,11 @@ let Direction = Stop
}
})
```
## 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 () {
let Direction = Stop
Expand All @@ -128,102 +132,45 @@ let Direction = Stop
}
// @highlight
if (input.rotation(Rotation.Pitch) > 60) {
Direction = Reverse
Direction = Backwards
}
})
```
## Step 6 - Add radio send "Backwards"
Drag ``||radio:Radio:send string||`` into the if-block and send the value "Backwards"
## 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")
}
})
```
## 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 () {
if (input.rotation(Rotation.Pitch) > 60) {
radio.sendString("Backwards")
} else if (true) {

} else {

}
})
```
## 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"
```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!

<script src="https://makecode.com/gh-pages-embed.js"></script><script>makeCodeRender("{{ site.makecode.home_url }}", "{{ site.github.owner_name }}/{{ site.github.repository_name }}");</script>
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# leave empty
3 changes: 2 additions & 1 deletion pxt.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"CodeClubBadge.md",
"QuickDraw.md",
"CuteBotController.md",
"tutorial-info-cache.json"
"tutorial-info-cache.json",
"main.py"
],
"testFiles": [
"test.ts"
Expand Down