Make a Motor Move
Before you begin, verify that the following libraries are part of your imports.
import com.qualcomm.robotcore.hardware.DcMotor;
The first thing we need is an instance variable or a member variable to hold on to a representation of one of our motors. Add this immediately following the opening line of your class:
private DcMotor myMotor = null;
We can now use this motor variable throughout the rest of the code in this OpMode.
The init method runs once when you click the “INIT” button on the Driver Hub.
This is a great place to set up the hardware devices on our robot.
Add the following line inside the init method to find a motor called “DT-RF” from our robot configuration -
if yours is named something different, you should also change what you put in your code.
myMotor = hardwareMap.get(DcMotor.class, "DT-RF");
We don’t need to do anything with the next two methods in your code yet.
The init_loop method runs repeatedly after the init method finishes and continues until you press the play button on the Driver Hub.
The start method runs once when you press the play button.
We do need to do something with the loop method, which will run over and over again after the start method finishes,
and will keep going until you press stop on the Driver Hub.
Add this line inside your loop method:
myMotor.setPower(0.8);
The setPower method on a DcMotor takes one argument, a number between -1 and 1, that makes the motor spin with the specified power and direction.
This makes the motor spin at 80% power until you press stop.
We’ve finished our first program! Click the red button with the wrench in the lower-right corner to compile all the programs on your robot and prepare them to run. Once that succeeds, you can choose your OpMode on your Driver Hub and watch a wheel spin!
Try changing the power & direction of your motor in the code, rebuild, and run your OpMode again.