Encoder

Hi, I' m working on a project with an ky-040 encoder.

I connected the encoder to one Arduino UNO R3: On the serial monitor i see unit values increase by turning the encoder.

I would like to add a value that I have established to each input and see this vale.
There is a way to do this?

Thank you and sorry for my english!

It would be easier to help if you provided some kind of code that we could help you with...

Do you just mean initializing the counter variable at the start, or being able to alter it later?
Either is simple, its just a variable, assign to it!

Thanks for the attention!

I want initializing the counter variable at the start to 0 and then when i rotate the shaft i want to add up a fixed value set by me.

For ex: 1,4 -> 2,8 -> 4,2 ecc...

Practically i want to convert the value in a lenght in mm.

A similar code where I'm working is like this:

int encoderPin1 = 2;
int encoderPin2 = 3;

volatile int lastEncoded = 0;
volatile long encoderValue = 0;

long lastencoderValue = 0;

int lastMSB = 0;
int lastLSB = 0;

void setup() {
Serial.begin (9600);

pinMode(encoderPin1, INPUT_PULLUP);
pinMode(encoderPin2, INPUT_PULLUP);

attachInterrupt(0, updateEncoder, CHANGE);
attachInterrupt(1, updateEncoder, CHANGE);

}

void loop(){
Serial.println(encoderValue);
}

void updateEncoder(){
int MSB = digitalRead(encoderPin1); //MSB = most significant bit
int LSB = digitalRead(encoderPin2); //LSB = least significant bit

int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value

if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;

lastEncoded = encoded; //store this value for next time
}

Because of round-off errors in floating point value I would recommend that you multiply when you want to use the value:

  Serial.println(encoderValue * 1.2, 1);

thank you very much for helping!