LogoLogo
Follow Us
  • Introduction
    • Overview
    • Welcome
      • Create Your Character
      • Visit the Garage
    • Get Parts
  • Tutorial
    • Your First Bot
      • Parts
      • Chassis
      • Decoration
    • Controls
      • Install Hub Firmware
      • Load Program Onto Hub
      • Using Your Controller
    • Next Steps
  • Battle
    • Rules
      • Crafting Rules
      • Battle Rules
    • Build Your Arena
  • Adventure
    • Quests
      • Sinkhole Patrol
      • Dance Video
      • Martinez Twins vs VentureState
      • Big Break
    • Lore
      • Places
        • Stardust Springs
        • Zoe's Garage
        • Bot Battles Stadium
      • Characters
        • Lily Ishii
        • Zoe Foxlin
        • Ethan Hill
Powered by GitBook
On this page
Export as PDF
  1. Adventure
  2. Quests

Big Break

PreviousMartinez Twins vs VentureStateNextLore

Last updated 3 months ago

Rewards

  • 🏆 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.

# 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 up motor
motorA = Motor(Port.A)
motorB = Motor(Port.B)

while True:

    # Set motor power
    powerA = controller.joystick_left()[1]
    powerB = controller.joystick_right()[1]

    # Rotate motor
    motorA.dc(powerA * -1)
    motorB.dc(powerB)

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:

# Set up motor
motorA = Motor(Port.A)
motorB = Motor(Port.B)
motorC = Motor(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.

powerA = controller.joystick_left()[1]

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.

if Button.A in controller.buttons.pressed():
    power = 100

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.

    # Set motor power
    powerA = controller.joystick_left()[1]
    powerB = controller.joystick_right()[1]
    if Button.A in controller.buttons.pressed():

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."

    # Set motor power
    powerA = controller.joystick_left()[1]
    powerB = controller.joystick_right()[1]
    if Button.A in controller.buttons.pressed():
        powerC = 100

Now add a line to rotate motorC.

    # Rotate motor
    motorA.dc(powerA * -1)
    motorB.dc(powerB)
    motorC.dc(powerC)

Your 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 up motor
motorA = Motor(Port.A)
motorB = Motor(Port.B)
motorC = Motor(Port.C)

while True:

    # Set motor power
    powerA = controller.joystick_left()[1]
    powerB = controller.joystick_right()[1]
    if Button.A in controller.buttons.pressed():
        powerC = 100

    # Rotate motor
    motorA.dc(powerA * -1)
    motorB.dc(powerB)
    motorC.dc(powerC)

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 an if 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.

    if Button.A in controller.buttons.pressed():
        powerC = 100
```  
    motorC.dc(powerC)

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 .

    powerC = 0
    if Button.A in controller.buttons.pressed():
        powerC = 100

Here's the final 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 up controller
controller = XboxController()

# Set up motor
motorA = Motor(Port.A)
motorB = Motor(Port.B)
motorC = Motor(Port.C)

while True:

    # Set motor power
    powerA = controller.joystick_left()[1]
    powerB = controller.joystick_right()[1]
    powerC = 0
    if Button.A in controller.buttons.pressed():
        powerC = 100

    # Rotate motor
    motorA.dc(powerA * -1)
    motorB.dc(powerB)
    motorC.dc(powerC)

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.

    if Button.A in controller.buttons.pressed():
        powerC = 100
    elif Button.B in controller.buttons.pressed():
        powerC = -100

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.

# 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 up motor
motorA = Motor(Port.A)
motorB = Motor(Port.B)
motorC = Motor(Port.C)

while True:

    # Set motor power
    powerA = controller.joystick_left()[1]
    powerB = controller.joystick_right()[1]
    powerC = 0
    if Button.A in controller.buttons.pressed():
        powerC = 100
    elif Button.B in controller.buttons.pressed():
        powerC = -100

    # Rotate motor
    motorA.dc(powerA * -1)
    motorB.dc(powerB)
    motorC.dc(powerC)

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.

    powerC = controller.triggers()[1] - controller.triggers()[0]

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

1
2
3