Lesson 14 - Creating Electronics

NOTE: As more sensors are released, this page may not encompass all types of sensors and how to declare them. If you cannot find what you need, the PROS API (documentation on how to use PROS) should always have what you need. It is truly a great resource!


To tell your program that VEX electronics are connected to the V5 Brain and to use them, you must declare them as objects within your code.


Motors

To declare a motor, PROS asks you to provide it with four pieces of information: the motor port, the gearset inside the motor (the motor cartridge), if the motor is reversed or not, and how motor encoder units should be interpreted. A template would look like:


Italicized words should be changed to fit your motor, sensor, or joystick specifications.


pros::Motor motorName(PORT_NUMBER, MOTOR_GEARSET_18, false, MOTOR_ENCODER_DEGREES); 


Example:

pros::Motor leftLift(5, MOTOR_GEARSET_36, false, MOTOR_ENCODER_DEGREES)
pros::Motor rightLift(10, MOTOR_GEARSET_36, true, MOTOR_ENCODER_DEGREES)


IMPORTANT NOTE: Each motor cartridge has a different gearset “key” that you must use when declaring the motor.

Red Cartridges (High Torque) - MOTOR_GEARSET_36

Green Cartridges (Standard) - MOTOR_GEARSET_18

Blue Cartridges (High Speed) - MOTOR_GEARSET_6


Joystick

To define the joystick in our code, we write:


pros::Controller controllerName(E_CONTROLLER_MASTER);


You can name the controller whatever you want, but it is commonly named “master”.


Example: 

pros::Controller master(E_CONTROLLER_MASTER);


Analog Sensors

To declare analog sensors (like a potentiometer or line tracker), you create an analog sensor object like so:


pros::ADIAnalogIn sensorName(PORT_NUMBER);


Then, you can use this command to get the value read by the sensor:


sensorName.get_value();


Example: 

pros::ADIAnalogIn liftPotentiometer(1);
if(liftPotentiometer.get_value() > 100){
lift_motor.move(0);
}


Digital Sensors

To declare digital sensors (like limit switches), you create a digital sensor object like so:

  pros::ADIDigitalIn sensorName(PORT_NUMBER);


With this, you can get the value from the sensor using the same command:


sensorName.get_value();


Example:

pros::ADIDigitalIn catapultLimit(2);
if(catapultLimit == 1 && controller.get_digital(DIGITAL_L1){
fireCatapult();
}


IMU

To declare an IMU, simply write:

pros::Imu IMUName(PORT_NUMBER);


Example:

pros::Imu imu(19);