I'm recently having a hard time with my Arduino project. I'm trying to control a seven-segment display (it has a microcontroller on it) with an encoder, and I'm trying so that everytime I rotate the encoder it adds 5 to the number on the display. What am I doing wrong here???

P.S: there are some parts that are for controlling relays, they are not related to the issue. I removed them on this thread on purpose.

Code (without any attempts to change the added amount to the display):

#include <RotaryEncoder.h>
#include <TM1637Display.h>

// Module connection pins (Digital Pins)
#define CLK 10
#define DIO 11

// Setup a RoraryEncoder for pins A2 and A3:
RotaryEncoder encoder(A2, A3);

// The amount of time (in milliseconds) between tests
#define TEST_DELAY   1000

TM1637Display display(CLK, DIO);

void setup()
    Serial.println("SimplePollRotator example for the RotaryEncoder 

// The Interrupt Service Routine for Pin Change Interrupt 1
// This routine will only be called on any signal change on A2 and A3: 
//exactly where we need to check.
ISR(PCINT1_vect) {
    encoder.tick(); // just call tick() to check the state.

// Read the current position of the encoder and print out when changed.
void loop()
    static int pos = 0;
    int newPos = encoder.getPosition();
    if(newPos > -1)
        if (pos != newPos) {
            pos = newPos + 4;
            // All segments on
            // Show decimal numbers with/without leading zeros
            display.showNumberDec(newPos, true); // Expect: 1234
    } // if
} // loop ()
// The End
  • You stated what you want it to do, but what does it actually do instead? Apr 26 '19 at 17:23
  • It instead just skips by one and not by 5 (the code which I published right here is without the attempt to do this
    – OmerFlame
    Apr 26 '19 at 17:37
  • I'd mutliply the pos by five. So display.showNumberDec(pos*5, true). Then you need only this single like instead of the whole if statement (all 10 lines).
    – Gerben
    Apr 26 '19 at 18:48

In your code, you call:

newPos = encoder.getPosition();

which grabs the absolute position of the encoder. When you rotate this one step, the value of newPos goes up (or down) by one.

Then, you make pos = newPos + 4, which simply adds 4 to the value that is going up or down by one step at a time.

newPos: 1    pos: 5
newPos: 2    pos: 6
newPos: 3    pos: 7
newPos: 4    pos: 8
newPos: 5    pos: 9

Instead, it sounds like you wish the display to always read 5 times the encoder position. If so, then you should write pos = newPos*5.

newPos: 1    pos: 5
newPos: 2    pos: 10
newPos: 3    pos: 15
newPos: 4    pos: 20
newPos: 5    pos: 25

Then, when you write to the display, you do so as:

display.showNumberDec(newPos, true); // Expect: 1234

But newPos is not the newly calculated value. Instead, call

display.showNumberDec(pos, true); // Expect: 1234
  • I think I tried that and to no avail, trying again now. EDIT: Nope, still counts one at a time.
    – OmerFlame
    Apr 26 '19 at 18:23
  • Well, you also need to write the correct value to the display: display.showNumberDec(newPos, true); tries to write newPos, but newPos is the encoder position. pos is the calculated display value. I will update my answer. Apr 26 '19 at 18:29
  • Thanks! It woroks like a charm now.
    – OmerFlame
    Apr 26 '19 at 18:32
  • This is a classic debugging problem. You think you wrote your code to do one thing and what you wrote actually does something else. You need to add print statements that display the values you are using and then think through each line of your code, writing down the values of your variables at each step and pretending you're a computer. Look at the code that's written, and calculate the results of each line, not what you expect to happen. It is made much harder on microcontrollers where you don't have a debugger to work with.
    – Duncan C
    Apr 27 '19 at 14:53
  • Good eye Jose, catching the fact that the OP is displaying the encoder position rather than the calculated value. (Voted)
    – Duncan C
    Apr 27 '19 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.