How to divide Rotary Encoder value

Hello,
Beginner question, I am was using Paul Stoffregen Encoder library to use 2 rotary encoders. I hooked everything up and it works but the value increases by 4 every time I rotate the encoder but I want it to increase it by just 1 instead. How would I go about doing this?

Here is the code,

#include <Encoder.h>

// Change these pin 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 knobLeft(2, 6);
Encoder knobRight(3, 4);
long newLeft = 0;
long newRight = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("TwoKnobs Encoder Test:");
}

long positionLeft  = -999;
long positionRight = -999;

void loop() {
  newLeft = knobLeft.read();
  newRight = knobRight.read();
  if (newLeft != positionLeft || newRight != positionRight) {
    Serial.print("Left = ");
    Serial.print(newLeft);
    Serial.print(", Right = ");
    Serial.print(newRight);
    Serial.println();
    positionLeft = newLeft;
    positionRight = newRight;
  }
  // if a character is sent from the serial monitor,
  // reset both back to zero.
  if (Serial.available()) {
    Serial.read();
    Serial.println("Reset both knobs to zero");
    knobLeft.write(0);
    knobRight.write(0);
  }
  newLeft = newLeft/4;
  newRight = newRight/4;
  
}
void loop() {
  newLeft = knobLeft.read() / 4;
  newRight = knobRight.read() / 4;
 

Just divide the value returned by 4....

2 Likes

Hello, I tried doing this but the serial monitor still is incriminating by 4's. I am not sure why it didn't work.

Show your updated code.

1 Like

Post the code that does not work. I hooked up an encoder to my Uno and loaded your code with the change posted by @ blh64. In serial monitor, the count changes by 1.

1 Like
/* Encoder Library - TwoKnobs Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 * This example code is in the public domain.
 */

#include <Encoder.h>

// Change these pin 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 knobLeft(2, 6);
Encoder knobRight(3, 4);


void setup() {
  Serial.begin(9600);
  Serial.println("TwoKnobs Encoder Test:");
}

long positionLeft  = -999;
long positionRight = -999;

void loop() {
  long newLeft, newRight;
  newLeft = knobLeft.read();
  newRight = knobRight.read();
  if (newLeft != positionLeft || newRight != positionRight) {
    Serial.print("Left = ");
    Serial.print(newLeft);
    Serial.print(", Right = ");
    Serial.print(newRight);
    Serial.println();
    positionLeft = newLeft;
    positionRight = newRight;
  }
  // if a character is sent from the serial monitor,
  // reset both back to zero.
  if (Serial.available()) {
    Serial.read();
    Serial.println("Reset both knobs to zero");
    knobLeft.write(0);
    knobRight.write(0);
  }
  newLeft = knobLeft.read()/4;
  newRight = knobRight.read()/4;
  
}

See comments.

1 Like

Whoops! I made a dumb mistake, thank you for showing me though it!

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