# Examples

### Use-Case

This example program demonstrates how to use the AndyMark **CAN Color Sensor** in an FRC robot program. It initializes the sensor over the **CAN bus** and continuously reads data while the robot is enabled. The program collects **proximity/distance** information (how close an object is to the sensor), the **ambient light level**, and individual **red, green, and blue** color intensity values. These values are then displayed to the SmartDashboard in real time, allowing users to see exactly what the sensor is detecting and how the readings change as objects move closer or lighting conditions vary. This example is useful for quick bring-up and testing, validating CAN wiring and device IDs, understanding the sensor’s output, and troubleshooting sensor placement on a robot, and it can be used without needing in-depth programming knowledge.

### Mounting Examples

![](https://content.gitbook.com/content/cQgnvayQud3Xf5sZi0wz/blobs/OG1ogAquE0pJX5AcnUSq/image)

### Code Example

You must first install the AndyMark vendor libraries. Instructions can be found [here](https://docs.andymark.com/frc-electronics/wpilib-am-vendor-library-setup)

View [Device API](https://docs.andymark.com/frc-electronics/can-sensors/can-color-sensor-am-5683/device-api) for a comprehensive list of commands

{% tabs %}
{% tab title="Java" %}

```java
import com.andymark.jni.AM_CAN_Color_Sensor;
import com.andymark.jni.AM_CAN_Color_Sensor.AM_ColorSensorData;

//Initialize Device
//The device's default CAN is 0. Change it using AndyMark CAN interface utility
AM_CAN_Color_Sensor colorSensor = new AM_CAN_Color_Sensor(0);
//Reset Report Period to the default of 100ms
colorSensor.resetReportPeriod();

//UPDATE DEVICE FIRMWARE TO 1.1.1 AND ANDYMARK WPILIB
//VERSION TO 2026.0.2 OR NEWER TO TURN LED ON
//Turn on LED for better data in low light conditions
colorSensor.turnLedOn();

//Get data and normalize it using clearC value
AM_ColorSensorData d = colorSensor.getData();
double red = (double) d.red/d.clearC;
double blue = (double) d.blue/d.clearC;
double green = (double) d.green/d.clearC;
//Put data on the smart dashboard
SmartDashboard.putNumber("Color/Red", red);
SmartDashboard.putNumber("Color/Green", green);
SmartDashboard.putNumber("Color/Blue", blue);
SmartDashboard.putNumber("Color/Clear", d.clearC);
SmartDashboard.putNumber("Color/Proximity", d.proximity);
SmartDashboard.putNumber("Color/Timestamp(ms)", d.millisStamp);
```

{% endtab %}

{% tab title="C++" %}

```cpp
//File to include
#include "AM_CAN_Color_Sensor.h"

//Initialize device
//The device's default CAN is 0. Change it using AndyMark CAN interface utility
AM_CANColorSensor myColorSensor{0};
//Reset Report Period to the default of 100ms
myColorSensor.ResetReportPeriod();

//UPDATE DEVICE FIRMWARE TO 1.1.1 AND ANDYMARK WPILIB
//VERSION TO 2026.0.2 OR NEWER TO TURN LED ON
//Turn on LED for better data in low light conditions
myColorSensor.TurnLedOn();

//Get data and normalize it using clearC value
AM_ColorSensorData data = myColorSensor.GetData();
double red = (double) data.red/data.clearC;
double blue = (double) data.blue/data.clearC;
double green = (double) data.green/data.clearC;
//Put data on the smart dashboard
frc::SmartDashboard::PutNumber("Red",red);
frc::SmartDashboard::PutNumber("Green",green);
frc::SmartDashboard::PutNumber("Blue",blue);
frc::SmartDashboard::PutNumber("Clear",data.clearC);
frc::SmartDashboard::PutNumber("Prox.",data.proximity);
frc::SmartDashboard::PutNumber("Millisec",data.millisStamp);
```

{% endtab %}
{% endtabs %}
