Sinkhole Patrol

Rewards
🏆 350 XP
[Zoe Foxlin] OOF. I'm so sorry. 😮💨 I wasn't looking where I was going. Gotta run, we've got a situat— oh, it's you! You have no idea how glad I am to see you, and - ugh, this RAIN! Look, just run with me and I'll explain.
Stardust Springs was a mining town back in the day. There are old mining tunnels everywhere under the industrial district. Nobody's supposed to worry about the tunnels. "Don't worry about the tunnels." Turns out we should've worried about the tunnels.
This storm has opened up a sinkhole right next to a scrap storage building. And it's growing. Like, faster than my laundry pile.

Scrap is starting to wash into the sinkhole. Once it's down there, we can't recycle it. It'll be lost forever in the tunnels, polluting the ground for decades.
The good news: I've got a team of robot battlers coming in as fast as they can to break down the scrap before it washes away. The bad news: they can't get through this storm. They won't make it until tomorrow morning. By then it'll be too late.
We need a robot to buy us some time. A vehicle that can patrol around the edge of the sinkhole all night to prevent scrap from getting washed in. Like a guard dog for scrap.
You've been working on crafting a robot, haven't you? That may be our only hope. Bring it. I'll help you program it to patrol the sinkhole. Just make sure it has wheel motors, like the Purrmenator. And double check that the left motor is plugged into port A on your hub. Otherwise our program will crash.


Bad at Coding
Oh wow, this sinkhole is even worse than I thought. We don't have much time to whip up our code.
Here's a secret. Come in close. I'M BAD AT CODING! Here's another secret. WHO CARES! It's never ever stopped me from programming my robots to do exactly what I want.
It's like... okay, you know how some people are good at making sandwiches? They can slap one together in 30 seconds. Other people have to spend 5 minutes looking up recipes and arranging all the ingredients. Well, both methods end up with a sandwich! One's not more "sandwich-y" than the other.
Coding is the same way. If you're good at it, you can do it fast. If you're bad at it, you might get stuck sometimes and have to search online for answers. That's fine! As long as you're creative, you'll always get there.
You don't have to be a chef to make a sandwich, and you don't have to be a "coder" to code a robot. I'm NOT a coder. I just use code sometimes to make my robots do awesome stuff. This is one of those times! We'll get it done together.
Rotaaaaate Motor
Before you start cooking, you have to decide what you want for lunch. (I'M HUNGRY, OKAY?) And before you start coding, you have to decide what you want your robot to do.
I'm thinking if we just get one wheel motor to rotate, that'll make the robot drive in circles around the sinkhole. Let's do it. Rotate motor.
Start by bringing up a blank program in Pybricks. Visit code.pybricks.com using Chrome or Edge.
Pybricks uses a programming language called Python. 🐍 And now... to code!
Rotate motor. Rooootaaaaaate motor. Aaaand I'm stuck. No problem. That's what the internet is for.
I searched online for "pybricks rotate motor example" and found this code.
# Import modules
from pybricks.hubs import TechnicHub
from pybricks.pupdevices import Motor
from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
from pybricks.tools import wait, StopWatch
from pybricks.iodevices import XboxController
# Set motor power
power = 20
# Set up controller
controller = XboxController()
# Set up motor
motor = Motor(Port.A)
while True:
# Rotate motor
motor.dc(power)
Does it work? Bro, I don't know! Let's load it up and see what happens.
Hover over this code and click the "Copy" button.
Paste it into Pybricks.
Load the program onto your hub. If you forgot how, see Load Program Onto Hub.
Connect your controller to your hub. If you forgot this too, see Using Your Controller.

Okay, the controller is connecting, and...
STOP THAT SPINNING ROBOT! Pounce on it before it falls into the sinkhole! Press the green button!

Close call there. We wanted a rotating motor, and we got a rotating motor. But I don't know if it's rotating with enough power. We're making a scrap fighting machine, not a microwave. It might get stuck on a random piece of scrap.
Comments
Let's take a look at the code and see what's going on. That way we can figure out what to change to make the motor rotate with more power.
Here's the very first line.
# Import modules
This is a comment. A comment begins with a hashtag #
.
Imagine you're baking a cake, and you have a recipe with steps to follow. Someone wrote a little note in the recipe: "Be careful not to spill the flour!" That note isn't part of the recipe. But it's very helpful, because it makes the recipe easier to follow.
Comments are just like that. They're useful for explaining what code does. They make your code easier to follow.
You can write anything in a comment. They're only for humans. The computer completely ignores comments when running the program. Just like I ignore Insta comments from my ex. (#sorrynotsorry)
Let's look through the other comments in this program. Maybe we can find a hint about increasing our motor's power.
# Import modules
from pybricks.hubs import TechnicHub
from pybricks.pupdevices import Motor
from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
from pybricks.tools import wait, StopWatch
from pybricks.iodevices import XboxController
# Set up controller
controller = XboxController()
# Set motor power
power = 20
# Set up motor
motor = Motor(Port.A)
while True:
# Rotate motor
motor.dc(power)
Tinkering
Well this one sure looks promising.
# Set motor power
power = 20
This comment helpfully explains what the next line does. Spoiler alert: it sets the motor's power.
I bet we can make our motor rotate faster by increasing this number. Try changing it from 20
to 100
. Load the new code onto your hub and turn on your controller again. This time, maybe hold onto your robot first...
WOW. Now we're cooking. Press the green button to stop the program before we plow through a wall or something.
When I'm not sure what code does, sometimes I just tinker around. I'll change a number and see what happens, like we did here with the power number. It's a great way to figure out how things work.
Speaking of tinkering... there's just one more problem. This robot is driving backwards! We need to make the motor rotate in the other direction. We can do this by adding a minus sign. Change the number from 100
to -100
. Like this.
# Set motor power
power = -100
The full code should look like this.
# Import modules
from pybricks.hubs import TechnicHub
from pybricks.pupdevices import Motor
from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
from pybricks.tools import wait, StopWatch
from pybricks.iodevices import XboxController
# Set up controller
controller = XboxController()
# Set motor power
power = -100
# Set up motor
motor = Motor(Port.A)
while True:
# Rotate motor
motor.dc(power)
Now set your robot up on the edge of the sinkhole and try running that program.

Scrap Scoop
YESSSS. SUCCESS!
If you want to make this robot an even better scrap defender, craft an arm for it that drags along the ground and pushes scrap away from the sinkhole. You can sprinkle some spare scrap around it to test if the arm works.

Once you're satisfied, fire that robot up and let it patrol! We'll check on it after the storm is over.
Finale
IT WORKED! I can't believe it. Sinkhole patrol robot saves the day. And look, here come the other robot battlers to break down all the scrap!
Next time you need to make a motor turn, you'll have an example to copy from. You'll make that sandwich — I mean program — even faster. Stop by my garage any time if you ever need more coding help.
Last updated