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.begin(57600);
Serial.println("SimplePollRotator example for the RotaryEncoder
library.");
}
// 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) {
Serial.print(newPos);
Serial.println();
pos = newPos + 4;
display.setBrightness(0x0f);
// All segments on
// Show decimal numbers with/without leading zeros
display.showNumberDec(newPos, true); // Expect: 1234
delay(100);
}
} // if
} // loop ()
// The End
1 Answer 1
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– OmerFlame2019年04月26日 18:23:06 +00:00Commented Apr 26, 2019 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.jose can u c– jose can u c2019年04月26日 18:29:04 +00:00Commented Apr 26, 2019 at 18:29 -
Thanks! It woroks like a charm now.OmerFlame– OmerFlame2019年04月26日 18:32:29 +00:00Commented Apr 26, 2019 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– Duncan C2019年04月27日 14:53:46 +00:00Commented Apr 27, 2019 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– Duncan C2019年04月27日 14:54:26 +00:00Commented Apr 27, 2019 at 14:54
Explore related questions
See similar questions with these tags.
display.showNumberDec(pos*5, true)
. Then you need only this single like instead of the wholeif
statement (all 10 lines).