Skip to content
Bendeguz edited this page May 28, 2026 · 16 revisions

Functions


Pattern functions

Encoder encoder()

Description

Sets up the pins. If the value goes up for the wrong direction of rotation then flip the pins:

//value goes up for CW
Encoder encoder(4, 5);
//value goes up for CCW
Encoder encoder(5, 4);

Parameters

  • CLK_pin - CLK pin of the encoder

  • DT_pin - DT pin of the encoder

Example

//Set the pins of the encoder
Encoder encoder(4, 5);

Encoder.begin()

Description

Initializes the library.

Parameters

  • none

Example

//initialize the library
encoder.begin();

Encoder.read()

Description

Returns an integer that can be changed by the rotary encoder.

Parameters

  • none

Example

//Returns the value
encoder.read();

Encoder.setLimits()

Description

Returns an integer that can be changed by the rotary encoder but can be given a lower and an upper limit.

Parameters

  • long Minval - lower stop

  • long Maxval - upper stop

Example

//Constrains the encoder output value between 0 and 10
//affects the read() function
encoder.setLimits(0, 10);

Encoder.setPosition()

Description

Sets the value of the encoder to the pos parameter and returns HIGH if succesful else it returns LOW.

Parameters

  • int pos - the position to set the encoder value to

Example

//sets the position of the encoder to zero
encoder.setPosition(0);
//if succesful at setting the value to zero it will print "1"
Serial.println(encoder.setPosition(0));

Encoder.setDirection()

Description

Sets the direction of the encoder used for incrementing/decrementing the output.

Parameters

  • bool direction - can flip the direction of the encoder from the software side. Default value is false.

Example

//flips the input direction
encoder.setDirection(true);
//flips input direction back
encoder.setDirection(false);

Encoder.getDirection()

Important

This function hasn't been released yet

Description

returns the current software direction of the encoder instance.

Parameters

none

Example

//print the current software direction
Serial.println(encoder.getDirection());
//flips encoder software direction
encoder.setDirection(!encoder.getDirection());

Encoder.scale()

Description

Sets increments of the output

Parameters

  • int scale - the amount the position changes per click

Example

//sets the scaling to 10
encoder.scale(10);

Encoder.motion()

Description

Returns true when the encoder was moved and keeps its value until it gets called, after that it goes back to false

Parameters

  • none

Example

//checks for motion
if(encoder.motion()) {  //only print the value of the encoder if it was moved
  Serial.println(encoder.read());
}

Encoder.lastMotionSince()

Description

returns true if the encoder has not had any motion for more than the amount of time you set.

Parameters

  • int noMotionTime - the time of no motion required to return true

Example

//sets the position of the encoder to zero
encoder.setPosition(0);
//if succesful at setting the value to zero it will print "1"
Serial.println(encoder.setPosition(0));

Encoder.setDebounceTime()

Description

Sets the time waiting between checking the encoder's pins

Parameters

  • int debounce_time

Example

//sets the debounce time to 2 milliseconds
encoder.setDebounceTime(2)
}

Encoder.LEDRing()

Description

Initializes the NeoPixel ring.

Parameters

  • int ledPin
  • uint16_t ledCount

Example

//initialize the library
encoder.begin();

Hardware

For testing and development of this library I'm using my custom board manufactured by PCBWay

You can find and download all design and manufacturing files + schematics here.

Special thanks to PCBWay for sponsoring the production of the prototype boards used in this project.

image

Clone this wiki locally