rotary encoder with detent

hi

i have a rotary encoder with detent and i don't know what code i could use the best for what i want it to do.
when i turn it CW, i want it to send a keyboard output on signal A (since signal A is the first signal that is turned on when turned CW) but ignore signal B
and when i turn it CCW, i want it to send a keyboard output on signal B (since signal B is the first signal that is turned on when turned CCW) but ignore signal A

rotend.jpg

rotend.jpg

https://playground.arduino.cc/Main/RotaryEncoders

this is for me written in an unknown language, i don't want it to be so difficult. i made a code but it's not working like a charm.

  // see if switch E is OFF or ON
  byte switchStateE = digitalRead (switchPinE);

  // has it changed since last time?
  if (switchStateE != oldSwitchStateE)
  {
    oldSwitchStateE =  switchStateE;  // remember for next time
    delay (debounceTime);   // debounce
    if (switchStateE == LOW && count == 0)
    {
      Keyboard.press('2');
      delay (20);
      Keyboard.release('2');
      count = 1;
    }  // end if switchStateE is LOW
    else if (switchStateE == LOW && count == 2)
    {
      Keyboard.print("");
      count = 0;
    }  // end if switchStateE is HIGH
  }  // end of state change

  // see if switch F is OFF or ON
  byte switchStateF = digitalRead (switchPinF);

  // has it changed since last time?
  if (switchStateF != oldSwitchStateF)
  {
    oldSwitchStateF =  switchStateF;  // remember for next time
    delay (debounceTime);   // debounce
    if (switchStateF == LOW && count == 0)
    {
      Keyboard.press('1');
      delay (20);
      Keyboard.release('1');
      count = 2;
    }  // end if switchStateF is LOW
    else if (switchStateE == LOW && count == 1)
    {
      Keyboard.print("");
      count = 0;
    }  // end if switchStateF is HIGH
  }  // end of state change

in this code switchstateE is signal A and switchstateF is signal B i made it so when turned CW it sets a count to 1 so it would ignore the other signal but when i turn it, it sometimes thinks it turning CCW

Install this encoder library and run one of the included examples. Verify that *counter * responds to encoder rotation.

You want to trigger on 3/4 of the way to the next detent? What for? And after you trigger on CW, and then go CCW, you will have go back past the detent and then 3/4 of the way to the next detent. Sounds goofy to me.

Doesn't seem goofy to me. Many encoders work this way: there's more electrical 'positions' than there are detent positions. The OP wants to send a keyboard command as soon as the encoder moves off the detent.

This will be a little weird as usually you would expect that turning the knob part-way and then letting it drop back into the previous position won't have any effect on the output (it does a right-left command) but I can see how this might be useful in some situations.

Looking at the provided diagram, there's 4 electrical transitions between each detent.

Proposed solution: use the Encoder.h library. Only send the key-command when the previous position is divisible by 4. On startup, the encoder can be assumed to be in a detent and not halfway between, so starting the position counter at zero will work. Zero is divisible by 4. Obviously decide which command to send depending if the current position is greater or less than the previous position.

i got this code now and it works like a just how i wanted it to

#include <Rotary.h>

// Rotary encoder is wired with the common to ground and the two
// outputs to pins 6 and 7.
Rotary rotary = Rotary(6, 7);

// Counter that will be incremented or decremented by rotation.
int counter = 0;

void setup() {
  Serial.begin(57600);
}

void loop() {
  unsigned char result = rotary.process();
  if (result == DIR_CW) {
    counter++;
    Keyboard.press('1');
    delay (10);
    Keyboard.release('1');
  } else if (result == DIR_CCW) {
    counter--;
    Keyboard.press('2');
    delay (10);
    Keyboard.release('2');
  }

but what do i need to change to add another encoder to this code? or what name do i need to adjust?

Try this: add another rotary encoder:

Rotary rotary = Rotary(6, 7);
Rotary rotary2 = Rotary2(8, 9);

and add another counter:

int counter2 = 0;

Yes, but give it a better name than rotary2. Call them left and right or name their actual function.