My dashboard would not show the color for my color sensor, please help me

package frc.robot;

import edu.wpi.first.wpilibj.TimedRobot;

import edu.wpi.first.wpilibj.I2C.Port;

import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;

import edu.wpi.first.wpilibj.util.Color;

import com.revrobotics.ColorMatchResult;

import com.revrobotics.ColorSensorV3;

import com.revrobotics.ColorMatch;

import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;

/**

  • The VM is configured to automatically run this class, and to call the

  • functions corresponding to each mode, as described in the TimedRobot

  • documentation. If you change the name of this class or the package after

  • creating this project, you must also update the build.gradle file in the

  • project.

*/

final public class Robot extends TimedRobot {

private static final String kDefaultAuto = “Default”;

private static final String kCustomAuto = “My Auto”;

private String m_autoSelected;

private final SendableChooser m_chooser = new SendableChooser<>();

private Port i2cPort;

/**

  • This function is run when the robot is first started up and should be

  • used for any initialization code.

*/

@Override

final public void robotInit() {

m_chooser.setDefaultOption("Default Auto", kDefaultAuto);

m_chooser.addOption("My Auto", kCustomAuto);

SmartDashboard.putData("Auto choices", m_chooser);

}

@Override

final public void teleopPeriodic() {

ColorMatchResult match = m_colorMatcher.matchClosestColor(detectedColor);

SmartDashboard.putNumber("Red", detectedColor.red);

SmartDashboard.putNumber("Green", detectedColor.green);

SmartDashboard.putNumber("Blue", detectedColor.blue);

SmartDashboard.putNumber("Confidence", match.confidence);

SmartDashboard.putString("Detected Color", colorString);

}

/**

  • This function is called every robot packet, no matter the mode. Use

  • this for items like diagnostics that you want ran during disabled,

  • autonomous, teleoperated and test.

  • This runs after the mode specific periodic functions, but before

  • LiveWindow and SmartDashboard integrated updating.

*/

private final ColorMatch m_colorMatcher = new ColorMatch();

private final ColorSensorV3 m_colorSensor = new ColorSensorV3(i2cPort);

Color detectedColor = m_colorSensor.getColor();

String colorString;

@Override

final public void robotPeriodic() {

}

/**

  • This autonomous (along with the chooser code above) shows how to select

  • between different autonomous modes using the dashboard. The sendable

  • chooser code works with the Java SmartDashboard. If you prefer the

  • LabVIEW Dashboard, remove all of the chooser code and uncomment the

  • getString line to get the auto name from the text box below the Gyro

  • You can add additional auto modes by adding additional comparisons to

  • the switch structure below with additional strings. If using the

  • SendableChooser make sure to add them to the chooser code above as well.

*/

@Override

public void autonomousInit() {

m_autoSelected = m_chooser.getSelected();

// m_autoSelected = SmartDashboard.getString("Auto Selector", kDefaultAuto);

System.out.println("Auto selected: " + m_autoSelected);

}

/**

  • This function is called periodically during autonomous.

/**

  • This function is called periodically during operator control.

*/

}

You’re not actually creating the port. The line Port i2cPort; should be more like Port i2cPort = I2C.Port.kOnboard;. I hope this helps!

Note: your post looks better when you format your code by surrounding it with backticks :wink:

thanks for the help but its not working

Ah, I see the problem. You need to do two things:

  1. Change this import: edu.wpi.first.wpilibj.I2C.Port to this: edu.wpi.first.wpilibj.I2C
  2. Change private Port i2cPort = I2C.Port.kOnboard; to private I2C.Port i2cPort = I2C.Port.kOnboard;

Thank you for the screenshot!

thanks but this still show up in my screen. * Could not instantiate robot edu.wpi.first.wpilibj.I2C!

  • at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:231)

  • at edu.wpi.first.wpilibj.RobotBase.startRobot(RobotBase.java:348)

  • at frc.robot.Main.main(Main.java:27)

  • Warning at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:244): Robots should not quit, but yours did!

  • Error at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:245): Could not instantiate robot edu.wpi.first.wpilibj.I2C!

  • ********** Robot program starting **********

  • ********** Robot program starting **********

  • ********** Robot program starting **********

If there aren’t any red lines in your code, it’s probably that the I2C port is already in use.
Is this a blank color-sensor-only program, or a full robot program?

If you put your code on github and post a link you can get better help.

Does the code build?

The error they’re getting suggests that the code builds, but fails to run on the roboRIO.

Agreed with @NewtonCrosby. Please post your code in github or similar so we can look at it.

Also, please post the error you’re currently getting.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.