Big Break
Last updated
Last updated
🏆 350 XP
[Mayor Ishii] You've heard the news, right? You haven't!? Do you even check your DMs!? We're down a competitor for tonight's Bot Battles! The headline match was supposed to feature Ben Carr, one of our volunteer firefighters. But a wildfire sprung up a few hours ago. His whole squad got called out to fight it. I've been scrambling to find someone to take his place.
I know you haven't been here long, but Zoe tells me you're a total natural. This could be your big break. Do you think you can put a bot together in time? Oh, one very important thing. Tonight's battle is custom controls only.
Hurry over to Zoe's Garage and she can help you design your very own control program.
[Zoe Foxlin] Ben Carr dropped out? Well yea, I heard that hours ago. I check my DMs.
Make sure your bot has two wheel motors and a weapon motor, like the Purrmenator. Plug the motors in exactly like this.
We're short on time, so let's start with the control program you wrote for the Martinez Twins.
We're already using both joysticks, so we'll need to use buttons to control our weapons.
First let's add a line to set up a motor on port C:
Buttons work a little differently than joysticks. Joysticks have a range of motion. We get their position, and then set the motor power to that number, like this.
Buttons don't have a range of motion. They only have two positions. They're either pressed, or they aren't. So the code for buttons is a little different. We need to check if the button is pressed. Here's some code from one of my old control programs where I used the A button to rotate a motor.
This is called an if
statement. It's basically like a True/False question. If the answer is True, the code inside it will run.
My code checks if button A is pressed. If so, it sets power to 100
.
Let's add my if
statement to the area where we set motor power.
When button A is pressed, we should set motor C's power to 100
. Let's add that line. Give it an extra indent since it's inside the "if statement."
Now add a line to rotate motorC
.
Your code should look like this.
Load it onto our bot and away we go!
Um, jk. It's not working. Let me ask a chatbot. I'll paste in the full code, and ask:
why isn't this working?
Hurry up hurry up. We've got a battle to get to. Okay, it says:
The variable
powerC
is created inside anif
statement. It can't be used outside of that statement.
Oh right! Well there's the problem. The first time you use a variable, you create it. The place where you create it determines where it can be used. This is called the variable's scope.
We created powerC
inside the if
statement. Later on, we try to use it outside of the statement to rate the motor.
This doesn't work.
If we want to use powerC
outside of the if
statement, we need to create it outside of the statement. Let's add a line above our if
statement to create powerC
.
Here's the final code.
Load it onto your bot and press the A button to rotate your weapon!
Almost there. You'll be facing some tough competition in this bot battle. You might need your weapon motor to rotate backwards. Especially if your weapon is something like a hammer that raises and lowers, or claw that opens and closes.
Let's use the B button for this. Add two lines to our if
statement, like this.
elif
is like saying, "if the previous True/False question was False, then ask this question."
So if the A button isn't pressed, our code will check if the B button is pressed. If it is, it will set powerC
to -100
.
Here's the full program.
Take it for a spin!
You know what? This is YOUR creation now. The best bot crafters I know all have their own... signature, I guess? The way they control their bots is as unique as their designs. The "right" way to control your bot is whatever feels right to you.
Go ahead and replace Button.A
and Button.B
with whatever buttons feel right to you!
Main Buttons
Direction Pad
Bumpers
Face Buttons
Or if you want to use the left and triggers instead, remove the if
statements and just set powerC
with this one line. The trigger positions range from 0 to 100. This line gets the right trigger position and subtract's the left trigger to give the full power range from -100
to 100
.
With these coding skills and a little math, you can make your bot do literally anything. Combine all movement into one joystick. Use a button to activate a turbo boost. Create an ignition or emergency shutoff button. If you can imagine it, you can code it. Come back another time and I'll help you create some really wild stuff. Today, though... no time to spare. The battle is coming right up.
Give your bot some Bot Battles worthy decorations. Remember, recycling points are the key to victory. Decorations should be wild and whimsical, and they should fall apart easily in battle to earn you the most points. Technic™ pieces are usually hard to break apart, so mostly use bricks! Strong chassis, delicate decorations!
If you have a friend or teammate with another bot, challenge them to battle!
Button.A
Button.B
Button.X
Button.Y
Button.UP
Button.DOWN
Button.LEFT
Button.RIGHT
Button.LB
Button.RB
Button.VIEW
Button.GUIDE
Button.MENU
Button.UPLOAD