Help Help ! I have no idea what I have done !

Hi All,
I have made a little remote USB control using a rotary encoder to emulate keyboard keys.

Type: Pro Micro
chipset: MEGA32U4-MU

clockwise is the up arrow
anticlockwise is the down arrow
the press is the enter button

Now it all works sort of ( and it’s not being used on a pc or mac) so it needs to work as an HID device ( i think ) please correct me if I am wrong.
the problem I am having is that I am trying to control volume up/down and enter on a device that uses the above keyboard keys. As I said it works …but jump 4 places each time ( up or down) the enter key is fine.

I am at the end of my knowledge as I have just started with Arduino. Could someone please help me get it working.
here’s what I have code wise.

#include <HID-Project.h>
#include <HID-Settings.h>

#include <HID.h>
#include <Encoder.h>

int pinA = 2; // Rotary Left (Out A pin)
int pinB = 3; // Rotary Right (Out B pin)
int pinC = 4; // Rotary Push (Switch pin)

Encoder myEnc(pinA, pinB);
long oldPosition = -999;

void setup() {
pinMode(pinA, INPUT_PULLUP);
pinMode(pinB, INPUT_PULLUP);
pinMode(pinC, INPUT_PULLUP);
BootKeyboard.begin();
}

long pressed = 0;
long turned = 0;

void loop()
{
long newPosition = myEnc.read();
if (newPosition != oldPosition && turned == 0)
{
turned = 1;
if (newPosition > oldPosition) {
BootKeyboard.write(KEY_UP_ARROW);
}
if (newPosition < oldPosition) {
BootKeyboard.write(KEY_DOWN_ARROW);
}
oldPosition = myEnc.read();
delay(150);
}
else {
turned = 0;
//delay(150);
}

if (digitalRead(pinC) == LOW && pressed == 0)
{
pressed = 1;
BootKeyboard.write(KEY_RETURN);
//Keyboard.release(KEY_RETURN);
}
if (digitalRead(pinC) == HIGH) {
pressed = 0;
}
}

oldPosition = myEnc.read();

Why read the encoder position again when you already have it in the newPosition variable ? By the time that you read it again it might have changed again

Please use </> code-tags to display code.

 long newPosition = myEnc.read();
  if (newPosition != oldPosition && turned == 0)  // you compare newPosition to oldPosition 
  {
    turned = 1;
    if (newPosition > oldPosition) {
      BootKeyboard.write(KEY_UP_ARROW);
    }
    if (newPosition < oldPosition) {
      BootKeyboard.write(KEY_DOWN_ARROW);
    }
    oldPosition = myEnc.read();   // and update oldPosition to a new read
    delay(150);

So… change the update to

oldPosition = newPosition;

Of course if your position changes more than 1, it will only be registered as 1, so i would suggest

while (newPosition > oldPosition) {
      BootKeyboard.write(KEY_UP_ARROW);
      oldPosition++;
    }
    while (newPosition < oldPosition) {
      BootKeyboard.write(KEY_DOWN_ARROW);
      oldPosition--;
    }

Now the update of oldPosition has already been done. I am not sure what happens if or when myEnc.read() rolls over, and you probably should consider and fix that.

Deva_Rishi:
Please use </> code-tags to display code.

 long newPosition = myEnc.read();

if (newPosition != oldPosition && turned == 0)  // you compare newPosition to oldPosition
 {
   turned = 1;
   if (newPosition > oldPosition) {
     BootKeyboard.write(KEY_UP_ARROW);
   }
   if (newPosition < oldPosition) {
     BootKeyboard.write(KEY_DOWN_ARROW);
   }
   oldPosition = myEnc.read();   // and update oldPosition to a new read
   delay(150);



So.. change the update to 


oldPosition = newPosition;



Of course if your position changes more than 1, it will only be registered as 1, so i would suggest


while (newPosition > oldPosition) {
     BootKeyboard.write(KEY_UP_ARROW);
     oldPosition++;
   }
   while (newPosition < oldPosition) {
     BootKeyboard.write(KEY_DOWN_ARROW);
     oldPosition–;
   }



Now the update of oldPosition has already been done. I am not sure what happens if or when myEnc.read() rolls over, and you probably should consider and fix that.

thanks all for your help, I am really new to all of this, someone I know is helping me the coding ( who am i kidding he has done 90% of it)