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
display.showNumberDec(pos*5, true)
. Then you need only this single like instead of the wholeif
statement (all 10 lines). – Gerben Apr 26 '19 at 18:48