# Setting Robot Preferences The Robot Preferences ([Java](https://github.wpilib.org/allwpilib/docs/beta/java/org/wpilib/preferences/Preferences.html), [C++](https://github.wpilib.org/allwpilib/docs/beta/cpp/classwpi_1_1_preferences.html)) class is used to store values in the flash memory on the roboRIO. The values might be for remembering preferences on the robot such as calibration settings for potentiometers, PID values, setpoints, etc. that you would like to change without having to rebuild the program. The values can be viewed on SmartDashboard or Shuffleboard and read and written by the robot program. This example shows how to utilize Preferences to change the setpoint of a PID controller and the P constant. The code examples are adapted from the Arm Simulation example ([Java](https://github.com/wpilibsuite/allwpilib/blob/v2027.0.0-alpha-7/wpilibjExamples/src/main/java/org/wpilib/examples/armsimulation/Robot.java), [C++](https://github.com/wpilibsuite/allwpilib/blob/v2027.0.0-alpha-7/wpilibcExamples/src/main/cpp/examples/ArmSimulation/cpp/Robot.cpp)). You can run the Arm Simulation example in the Robot Simulator to see how to use the preference class and interact with it using the dashboards without needing a robot. ## Initializing Preferences .. tab-set:: .. tab-item:: Java :sync: Java .. rli:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2027.0.0-alpha-6/wpilibjExamples/src/main/java/org/wpilib/examples/armsimulation/Constants.java :language: java :lines: 15-20 .. rli:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2027.0.0-alpha-6/wpilibjExamples/src/main/java/org/wpilib/examples/armsimulation/subsystems/Arm.java :language: java :lines: 27-29, 73-74, 80-83 .. tab-item:: C++ :sync: C++ .. rli:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2027.0.0-alpha-6/wpilibcExamples/src/main/cpp/examples/ArmSimulation/include/Constants.hpp :language: c++ :lines: 30-34 .. rli:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2027.0.0-alpha-6/wpilibcExamples/src/main/cpp/examples/ArmSimulation/cpp/subsystems/Arm.cpp :language: c++ :lines: 11, 17-21 .. tab-item:: Python :sync: Python .. rli:: https://raw.githubusercontent.com/robotpy/mostrobotpy/2027.0.0a6/examples/robot/ArmSimulation/constants.py :language: python :lines: 18-23 .. rli:: https://raw.githubusercontent.com/robotpy/mostrobotpy/2027.0.0a6/examples/robot/ArmSimulation/subsystems/arm.py :language: python :lines: 18-20,69-73 Preferences are stored using a name, the key. It's helpful to store the key in a constant, like ``kArmPositionKey`` and ``kArmPKey`` in the code above to avoid typing it multiple times and avoid typos. We also declare variables, ``kArmKp`` and ``armPositionDeg`` to hold the data retrieved from preferences. In ``Arm`` constructor, each key is checked to see if it already exists in the Preferences database. The ``containsKey`` method takes one parameter, the key to check if data for that key already exists in the preferences database. If it doesn't exist, a default value is written. The ``setDouble`` method takes two parameters, the key to write and the data to write. There are similar methods for other data types like booleans, ints, and strings. If using the Command Framework, this type of code could be placed in the constructor of a Subsystem or Command. ## Reading Preferences .. tab-set:: .. tab-item:: Java :sync: Java .. rli:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2027.0.0-alpha-6/wpilibjExamples/src/main/java/org/wpilib/examples/armsimulation/subsystems/Arm.java :language: java :lines: 105-112 .. tab-item:: C++ :sync: C++ .. rli:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2027.0.0-alpha-7/wpilibcExamples/src/main/cpp/examples/ArmSimulation/cpp/subsystems/Arm.cpp :language: c++ :lines: 3-51 .. tab-item:: Python :sync: Python .. rli:: https://raw.githubusercontent.com/robotpy/mostrobotpy/2027.0.0a6/examples/robot/ArmSimulation/subsystems/arm.py :language: python :lines: 97-104 Reading a preference is easy. The ``getDouble`` method takes two parameters, the key to read, and a default value to use in case the preference doesn't exist. There are similar methods for other data types like booleans, ints, and strings. Depending on the data that is stored in preferences, you can use it when you read it, such as the proportional constant above. Or you can store it in a variable and use it later, such as the setpoint, which is used in ``telopPeriodic`` below. .. tab-set:: .. tab-item:: Java :sync: Java .. rli:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2027.0.0-alpha-7/wpilibjExamples/src/main/java/org/wpilib/examples/armsimulation/Robot.java :language: java :lines: 28-37 .. rli:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2027.0.0-alpha-7/wpilibjExamples/src/main/java/org/wpilib/examples/armsimulation/subsystems/Arm.java :language: java :lines: 115-120 .. tab-item:: C++ :sync: C++ .. rli:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2027.0.0-alpha-7/wpilibcExamples/src/main/cpp/examples/ArmSimulation/cpp/Robot.cpp :language: c++ :lines: 15-24 .. rli:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2027.0.0-alpha-7/wpilibcExamples/src/main/cpp/examples/ArmSimulation/cpp/subsystems/Arm.cpp :language: c++ :lines: 53-59 .. tab-item:: Python :sync: Python .. rli:: https://raw.githubusercontent.com/robotpy/mostrobotpy/2027.0.0a6/examples/robot/ArmSimulation/robot.py :language: python :lines: 28-34 .. rli:: https://raw.githubusercontent.com/robotpy/mostrobotpy/2027.0.0a6/examples/robot/ArmSimulation/subsystems/arm.py :language: python :lines: 106-111 .. todo:: add information about using preferences in other dashboards