# Examples

### Use-Case

This example program shows how to use a digital pushbutton / magnetic hall effect switch in an FTC OpMode. It initializes a DigitalChannel from the robot’s hardware map, configures it as an input, and continuously reads the switch state while the OpMode is active. The code reports whether the button is currently pressed or released and implements basic software debouncing and edge detection so quick taps are captured reliably. These values are updated in real time and displayed on the Driver Station, giving teams immediate feedback as they press and release the button.

This example is especially useful for testing wiring and verifying the logic level of the input (“active-low,” so pressed = LOW). Teams can use it to prototype user inputs, limit switches on mechanisms, homing routines, and safety interlocks, or to trigger steps in autonomous sequences with a simple button press. Because the program focuses on direct digital input reading and telemetry, it doubles as a diagnostic/learning tool and requires no advanced programming knowledge—just a button wired to a digital port on the Control/Expansion Hub.

### Mounting Examples

### Code Example

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

View [Device API](https://docs.andymark.com/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_Mag_Switch.AM_MagSwitchData;
import com.andymark.jni.AM_CAN_Mag_Switch;

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

//Get data put it on the smart dashboard
AM_MagSwitchData d = magSwitch.getData();
SmartDashboard.putBoolean("MagDetected?", d.magnetDetected);
SmartDashboard.putNumber("Timestamp", d.timeStamp);
```

{% endtab %}

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

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

//Initialize device
//The device's default CAN is 0. Change it using AndyMark CAN interface utility
AM_CAN_Mag_Switch myMagSwitch{0};

//Reset Report Period to the default of 100ms
myMagSwitch.ResetReportPeriod();

//Get data put it on the smart dashboard
AM_MagSwitchData d = myMagSwitch.GetData();
frc::SmartDashboard::PutBoolean("MagDetected?",d.magnetDetected);
frc::SmartDashboard::PutNumber("Timestamp",d.timeStamp);
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.andymark.com/frc-electronics/can-sensors/can-magnetic-switch-am-5746/examples.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
