Description:
In this lesson we will add the REV V3 Color Sensor and develop a function to follow a line.
Materials:
Completed Training Robot with Marist 2021 Software installed
REV V3 Color Sensor and signal cable
Assembly:
Mount the Color sensor in front of the drive wheels about 1 inch above the ground. Make sure the sensor is facing the floor.
Plug the Signal Cable into I2C port 2
Programming:
You have the following functions available:
robot.getRed(); // returns a double for "red"
robot.getGreen(); // returns a double for "green"
robot.getBlue(); // returns a double for "blue"
robot.getIntensity(); // returns a float for general brightness
To Add Code to Your Robot:
For line following, place the following function in your AutoTest_2021.java file
// Code Starts here
public void lineFollow() {
double value = robot.getIntensity(); // Read Light Value from Sensor
while(opModeIsActive()) {
value = robot.getIntensity();
if (value > 0.5) {
robot.rightMotor.setPower(0.5);
robot.leftMotor.setPower(0);
}
else {
robot.rightMotor.setPower(0);
robot.leftMotor.setPower(0.5);
}
}
}
Then, call the function on line 44 and run:
lineFollow();