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
  • Create a program file
  • Connect your hub to your computer
  • Transfer the program to your hub
Export as PDF
  1. Tutorial
  2. Controls

Load Program Onto Hub

PreviousInstall Hub FirmwareNextUsing Your Controller

Last updated 4 days ago

[Zoe Foxlin] A program tells your hub and motors how to respond to button presses.

1

Create a program file

  1. Visit code.botbattles.org using Chrome or Edge.

  2. Click the âž• New File button.

  1. If this is your first time, copy my premade control program ↓.

Zoe's Universal Bot Controls

Hover over the code below, click the Copy button, and paste it into your empty file.

from pybricks.iodevices import XboxController
from pybricks.parameters import Direction, Port, Stop
from pybricks.pupdevices import Motor

# Set up all devices.
try:
    left = Motor(Port.A, Direction.COUNTERCLOCKWISE)
except:
    pass

try:        
    right = Motor(Port.B, Direction.CLOCKWISE)
except:
    pass
    
try:        
    weapon = Motor(Port.C, Direction.COUNTERCLOCKWISE)
except:
    pass    

controller = XboxController()

# The main program starts here.


try:
    left.control.limits(acceleration=20000)
except:
    pass

try:        
    right.control.limits(acceleration=20000)
except:
    pass
    
try:        
    weapon.control.limits(acceleration=20000)
except:
    pass

deadzone = 20

while True:
    compX = 0
    compY = 0
    compWL = 0
    compWR = 0

    if (abs(controller.joystick_left()[0]) >= deadzone):
        sign = 1 if controller.joystick_left()[0] > 0 else -1
        compX = (abs(controller.joystick_left()[0]) - deadzone) * sign / 2

    if (abs(controller.joystick_left()[1]) >= deadzone):
        sign = 1 if controller.joystick_left()[1] > 0 else -1
        compY = (abs(controller.joystick_left()[1]) - deadzone) * sign 

    if (abs(controller.triggers()[0]) >= deadzone):
        sign = 1 if controller.triggers()[0] > 0 else -1
        compWL = (abs(controller.triggers()[0]) - deadzone) * sign

    if (abs(controller.triggers()[1]) >= deadzone):
        sign = 1 if controller.triggers()[1] > 0 else -1
        compWR = (abs(controller.triggers()[1]) - deadzone) * sign          
    
    if compX != 0 or compY != 0:
        try:
            left.run(20 * (compY + compX))
        except:
            pass 
        try:        
            right.run(20 * (compY - compX))
        except:
            pass    
    else:
        try:
            left.stop()
        except:
            pass      
        try:    
            right.stop()
        except:
            pass  

    try:
        if compWR - compWL != 0:
            weapon.run(20 * (compWR - compWL))  
        else:
            weapon.stop()
    except:
        pass    
2

Connect your hub to your computer

  1. If your hub is off, press the green button to turn it on.

  2. Make sure your hub's light is blinking.

    1. If it's pulsing instead , press and hold the green button until it starts blinking.

  3. Click the Bluetooth button.

  1. Click the name of your hub, then click Pair.

When the hub is connected, the light will stop blinking and turn solid blue. The Bluetooth button icon will also change.

Troubleshooting

The hub won't light up

Make sure you've installed all 6 AA batteries. The battery holder pops out and stores 2 rows of 3 batteries each. Make sure your batteries are fully charged and facing the right directions.

The hub isn't showing up in the list

  • Make sure your hub is in Bluetooth mode. The blue light should be blinking, not pulsing.

    • If the light is off, your hub is off. Press the green button to turn it on.

    • If the light is slowly puling blue, your hub is running code. Press the green button to switch to pairing mode.

  • Make sure you've installed the Bot Battles firmware.

If it's still not showing up, reinstall the Bot Battles firmware and start over.

3

Transfer the program to your hub

  1. Press the Play button.

The play button will turn into a loading wheel to show progress. If it gets stuck: refresh the page, reconnect bluetooth, and try again.

You only need to transfer a program to your hub once. Your hub will remember it.

Troubleshooting

The play button is grayed out

Make sure your hub is connected to your computer.

I pressed play and the loading wheel froze

  1. Disconnect your hub by clicking the Bluetooth button in the code editor. Reconnect your hub. Load the program again.

  2. If this still doesn't work, disconnect and reconnect Bluetooth again. Before loading the program, make any change to your program (such as adding a space to the end of a line).

  3. If it's still not working, quit and reopen your browser, turn your hub off and on, and try again.