Sinkhole Patrol
Last updated
Last updated
🏆 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 bot 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 bot 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 bot, 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.
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 bots 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 bot. I'm NOT a coder. I just use code sometimes to make my bots do awesome stuff. This is one of those times! We'll get it done together.
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 bot to do.
I'm thinking if we just get one wheel motor to rotate, that'll make the bot 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.
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 BOT! 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.
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.
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.
Well this one sure looks promising.
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 bot 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 bot 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.
The full code should look like this.
Now set your bot up on the edge of the sinkhole and try running that program.
YESSSS. SUCCESS!
If you want to make this bot 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 bot up and let it patrol! We'll check on it after the storm is over.
IT WORKED! I can't believe it. Sinkhole patrol bot saves the day. And look, here come the other bot 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.