Encoder Limit switch

I’m currently working on a robot arm with DC motors and encoders for position control.
Since I’m pretty much still at the beginning of this project I am trying to read out the encoders.
By using the “Basic” example from the Encoder.h library I can get a good reading over all the positions of each axis.

My question is how can I integrate a limit switch into this. When the limit switch is pressed I’d like to have the current position to be reset to a 0.

This is what the example looks like:

/* Encoder Library - Basic Example

#include <Encoder.h>

// Change these two numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder myEnc(2, 3);
// avoid using pins with LEDs attached

void setup() {
Serial.println(“Basic Encoder Test:”);

long oldPosition = -999;

void loop() {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;

Use the encoder library function myEnc.write(newPosition);