Ascend
Coding
Ascend
  • 🚀Introduction
  • VEX Robotics
    • 🛠️Building
      • 📏CAD Design
      • 🔧Best Practices
      • 🏎️Drive Trains
        • 🔩Screw Joints
        • 🥊Boxing
        • 🔵Bearing flats
        • 🛤️Fixing Friction
        • 🔥450 RPM on 2.75"
      • 🏍️Motors
      • 🪨Metal
      • Plastic
      • 🎈Pneumatics
      • ➕Additional Mechanisms
        • 🖨️3D Printing
        • ↔️Ratchets
        • 🏹Catapults
        • 🎣Intakes
        • 🏋️Lifts
    • 💻Coding
      • ⚖️Choosing Your Coding Environment
      • 🔴VEXCode Pro
        • 🖥️Getting Started
        • 🚗Drive Code
        • 🚴Coding Motors
        • 🌬️Coding Pneumatics
        • 🎛️Advanced
          • 📡Coding Autonomous
          • 📈Coding PIDs
            • ⬆️Drive PID Tutorial
            • ↩️Turn PID Tutorial
            • 🎸Tuning PIDs
              • 🔢Ziegler-Nichols Method
              • 📊Graphing PIDs
          • 📋Tasks
          • 🚝Advanced Drive Code
      • 🟡PROS
    • 📗Engineering Notebook
      • 📔Formatting
      • ⭐Notebook Walkthrough
      • 💠Decision Matrices
        • 📌Decision Point
      • ➕Additional Resources
    • 🏆Tournaments
      • 🗣️Team Interviews
        • 🪙Interview Tips
        • ⛳Interview Scoring
        • 🤽‍♀️Interview Practice Questions
      • 🤹Skills
      • ℹ️Ranking and Stats
      • 📷Filming Matches
  • Other
    • 🧑‍💼Management
      • 🕑Time Management
      • 👨‍👩‍👧‍👦Team Management
        • 👮Delegation
        • ⚓Optimal Team Size
      • 🖇️Code Management (GitHub)
      • 🚋Resource Management
      • ⁉️Stuck?
    • 🍎Physics
      • ⚙️Torque
      • 😑Stress Forces
    • 👾Extra stuff
      • 📸96504 Gallery
      • ⛑️VEX Team Resources (High Stakes)
      • 🏎️Driving Simulator
Powered by GitBook
On this page

Was this helpful?

  1. VEX Robotics
  2. Coding
  3. VEXCode Pro

Coding Motors

PreviousDrive CodeNextCoding Pneumatics

Last updated 11 months ago

Was this helpful?

In this tutorial, we'll show you how to code additional mechanisms on the robot, like an intake or catapult. Specifically, you'll learn how to spin a motor forwards and backwards at the press of a button.

First, add the motor to the devices list. In this example, our motor is named Motor

Next create a function that spins the motor forwards at full voltage:

//Spin the motor forward at full voltage
void spinMotorForward() {
  Motor.spin(forward, 11, volt);
}

Now, let's create another function to spin the motor backwards at full voltage:

//Spin the motor backward at full voltage
void spinMotorBackward() {
  Motor.spin(reverse, 11, volt);
}

Thirdly, we need a function to stop the motor from spinning when the button on the controller is released:

//Stop the motor
void stopMotor() {
  Motor.stop();
}

All of this code is great, but none of it will run unless we set up the proper callbacks. We'll use the buttons L1 and L2 in this example, but feel free to change it to any buttons you would like. Put the callback code in the main function, so that the controller buttons activate the functions that spin or stop the motor.

int main() {
  //Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  
  //Set up the motor callbacks
  Controller1.ButtonL1.pressed(spinMotorForward); //when button L1 is pressed, spin the motor forward
  Controller1.ButtonL2.pressed(spinMotorBackward); //when button L2 is pressed, spin the motor backward

  Controller1.ButtonL1.released(stopMotor); //when button L1 is released, stop the motor
  Controller1.ButtonL2.released(stopMotor); //when button L2 is released, stop the motor

  //there may be more code below, leave it be
}

Now, download the code and test to see if it works!

💻
🔴
🚴