Hid device programming help

so i want a 5 pin rotary encoder ( encoder b in the code) to perform this action of switch between software in windows (back and forth ) by using the shortcut alt+tab and alt+shift+tab . this is the code and i am doing this on pro micro

#include <Keypad.h>
#include <Encoder.h>
#include "HID-Project.h"

// Encoder A
const int SW_A = 2;
const int DT_A = 3;
const int CLK_A = 4;
Encoder volumeKnob_A(DT_A, CLK_A);

// Encoder B
const int SW_B = 5;
const int DT_B = 6;
const int CLK_B = 7;
Encoder volumeKnob_B(DT_B, CLK_B);

// F10 switch
const int SW_F10 = A3;

// LED pins
const int LED1 = A0; // L1 LED
const int LED2 = A1; // L2 LED
const int LED3 = A2; // L3 LED

// Matrix switch pins
const byte rowPins[] = {15, 14, 16};
const byte colPins[] = {8, 9, 10};
const byte numRows = 3;
const byte numCols = 3;

char keys[numRows * numCols] = {
  '1', '2', '3',
  '4', '5', '6',
  '7', '8', '9'
};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, numRows, numCols);

int timeLimit = 500;
long oldPosition_A = -999;
long oldPosition_B = -999;
int currentState = 0;
const int numStates = 3;

void setup() {
  pinMode(SW_A, INPUT_PULLUP); // Set encoder A switch pin as INPUT_PULLUP
  pinMode(SW_B, INPUT_PULLUP); // Set encoder B switch pin as INPUT_PULLUP
  pinMode(SW_F10, INPUT_PULLUP); // Set F10 switch pin as INPUT_PULLUP

  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);

  // Initialize the HID-Project library
  Consumer.begin();

  // LED Animation
  ledAnimation();
}

void ledAnimation() {
  // LED Animation on startup
  for (int i = 0; i < 3; i++) {
    digitalWrite(LED1, HIGH);
    delay(100);
    digitalWrite(LED1, LOW);
    delay(100);
    digitalWrite(LED2, HIGH);
    delay(100);
    digitalWrite(LED2, LOW);
    delay(100);
    digitalWrite(LED3, HIGH);
    delay(100);
    digitalWrite(LED3, LOW);
    delay(100);
  }
}

void ChangeState() {
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);

  currentState++;
  if (currentState >= numStates) {
    currentState = 0;
  }

  switch (currentState) {
    case 0:
      digitalWrite(LED1, HIGH);
      break;
    case 1:
      digitalWrite(LED2, HIGH);
      break;
    case 2:
      digitalWrite(LED3, HIGH);
      break;
  }

  delay(100);
}

void F1() {
  // Emulate a key combination of Ctrl, Shift, and Esc together
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press(KEY_LEFT_SHIFT);
  Keyboard.press(KEY_ESC);
  delay(100);
  Keyboard.releaseAll();
}

void F2() {
  // Emulate a key combination of Ctrl+A
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press('a');
  delay(100);
  Keyboard.releaseAll();
}

void F3() {
  // Emulate a key combination of Ctrl+C
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press('c');
  delay(100);
  Keyboard.releaseAll();
}

void F4() {
  // Emulate a key combination of Ctrl+X
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press('x');
  delay(100);
  Keyboard.releaseAll();
}

void F5() {
  // Emulate a key combination of Ctrl+V
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press('v');
  delay(100);
  Keyboard.releaseAll();
}

void F6() {
  // Emulate a key combination of Windows key and D
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press('d');
  delay(100);
  Keyboard.releaseAll();
}

void F7() {
  // Emulate a key combination of Ctrl, Windows key, and T
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press('t');
  delay(100);
  Keyboard.releaseAll();
}

void F8() {
  // Emulate a key combination of Windows key and R
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press('r');
  delay(100);
  Keyboard.releaseAll();
}

void F9() {
  // Emulate a key combination of Windows key and E
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press('e');
  delay(100);
  Keyboard.releaseAll();
}

void Layout1(char button) {
  switch (button) {
    case '1':
      F1();
      break;
    case '2':
      F2();
      break;
    case '3':
      F3();
      break;
    case '4':
      F4();
      break;
    case '5':
      F5();
      break;
    case '6':
      F6();
      break;
    case '7':
      F7();
      break;
    case '8':
      F8();
      break;
    case '9':
      F9();
      break;
  }
}

void Layout2(char button) {
  // Layout 2 implementation
}

void Layout3(char button) {
  // Layout 3 implementation
}

void processKeypad(char key) {
  switch (key) {
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
      Layout1(key);
      break;
  }
}

void loop() {
  // Check encoder A button
  if (digitalRead(SW_A) == LOW) {
    int fall = millis();
    while (digitalRead(SW_A) == LOW) {}
    int rise = millis();
    if (rise - fall > timeLimit) {
      Consumer.write(MEDIA_NEXT);
    } else {
      Consumer.write(MEDIA_PLAY_PAUSE);
    }
    Keyboard.releaseAll();
  }

  // Check encoder A rotation
  long newPosition_A = volumeKnob_A.read();
  if (newPosition_A != oldPosition_A) {
    if (newPosition_A > oldPosition_A) {
      Consumer.write(MEDIA_VOLUME_UP);
    } else {
      Consumer.write(MEDIA_VOLUME_DOWN);
    }
    oldPosition_A = newPosition_A;
  }

  // Check encoder B rotation
  long newPosition_B = volumeKnob_B.read();
  if (newPosition_B > oldPosition_B) {
    // Clockwise rotation
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press(KEY_TAB);
    delay(20);
    Keyboard.releaseAll();
  } else if (newPosition_B < oldPosition_B) {
    // Counterclockwise rotation
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press(KEY_TAB);
    Keyboard.press(KEY_LEFT_SHIFT);
    delay(20);
    Keyboard.releaseAll();
  }
  oldPosition_B = newPosition_B;

  // Check encoder B button
  if (digitalRead(SW_B) == LOW) {
    ChangeState();
    while (digitalRead(SW_B) == LOW) {}
  }

  // Check F10 switch
  if (digitalRead(SW_F10) == LOW) {
    while (digitalRead(SW_F10) == LOW) {}
    Keyboard.press(KEY_LEFT_CTRL);
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('d');
    delay(100);
    Keyboard.releaseAll();
  }

  // Check keypad buttons
  char key = keypad.getKey();
  if (key) {
    processKeypad(key);
  }
}

current issue is that the rotation is not smooth at all the switching between program is unpredictable.

I moved your topic to an appropriate forum category @snehilsourabh .

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

1 Like

I'm not sure what happened there, but I moved this topic to "programming", which is a far more appropriate category than "uncategorised"

please read How to get the best out of this forum

➜ it will help us help you if you post the full code and all the necessary information (like schema, power, links to encoder, ...)

ok the connections are as follows ( i am using the pro micro for this project )
the connection includes 3 led 2 five pin rotary encoders and a 3x3 matrix switch and a single switch connected to a3

Here's a summary of the connections provided for the Arduino HID device:

LED Connections:

L1: Connected to pin A0
L2: Connected to pin A1
L3: Connected to pin A2
Single Switch Connection:

Connected to pin A3
Encoder A Connections:

SW pin: Connected to pin 2
DT pin: Connected to pin 3
CLK pin: Connected to pin 4
Encoder B Connections:

SW pin: Connected to pin 5
DT pin: Connected to pin 6
CLK pin: Connected to pin 7
Matrix Switch Connections:

Rows: R1 connected to pin 15, R2 connected to pin 14, R3 connected to pin 16
Columns: C1 connected to pin 8, C2 connected to pin 9, C3 connected to pin 10

and the codes are as follows

#include <Keypad.h>
#include <Encoder.h>
#include "HID-Project.h"

// Encoder A
const int SW_A = 2;
const int DT_A = 3;
const int CLK_A = 4;
Encoder volumeKnob_A(DT_A, CLK_A);

// Encoder B
const int SW_B = 5;
const int DT_B = 6;
const int CLK_B = 7;
Encoder volumeKnob_B(DT_B, CLK_B);

// F10 switch
const int SW_F10 = A3;

// LED pins
const int LED1 = A0; // L1 LED
const int LED2 = A1; // L2 LED
const int LED3 = A2; // L3 LED

// Matrix switch pins
const byte rowPins[] = {15, 14, 16};
const byte colPins[] = {8, 9, 10};
const byte numRows = 3;
const byte numCols = 3;

char keys[numRows * numCols] = {
  '1', '2', '3',
  '4', '5', '6',
  '7', '8', '9'
};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, numRows, numCols);

int timeLimit = 500;
long oldPosition_A = -999;
long oldPosition_B = -999;
int currentState = 0;
const int numStates = 3;

void setup() {
  pinMode(SW_A, INPUT_PULLUP); // Set encoder A switch pin as INPUT_PULLUP
  pinMode(SW_B, INPUT_PULLUP); // Set encoder B switch pin as INPUT_PULLUP
  pinMode(SW_F10, INPUT_PULLUP); // Set F10 switch pin as INPUT_PULLUP

  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);

  // Initialize the HID-Project library
  Consumer.begin();

  // LED Animation
  ledAnimation();
}

void ledAnimation() {
  // LED Animation on startup
  for (int i = 0; i < 3; i++) {
    digitalWrite(LED1, HIGH);
    delay(100);
    digitalWrite(LED1, LOW);
    delay(100);
    digitalWrite(LED2, HIGH);
    delay(100);
    digitalWrite(LED2, LOW);
    delay(100);
    digitalWrite(LED3, HIGH);
    delay(100);
    digitalWrite(LED3, LOW);
    delay(100);
  }
}

void ChangeState() {
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);

  currentState++;
  if (currentState >= numStates) {
    currentState = 0;
  }

  switch (currentState) {
    case 0:
      digitalWrite(LED1, HIGH);
      break;
    case 1:
      digitalWrite(LED2, HIGH);
      break;
    case 2:
      digitalWrite(LED3, HIGH);
      break;
  }

  delay(100);
}

void F1() {
  // Emulate a key combination of Ctrl, Shift, and Esc together
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press(KEY_LEFT_SHIFT);
  Keyboard.press(KEY_ESC);
  delay(100);
  Keyboard.releaseAll();
}

void F2() {
  // Emulate a key combination of Ctrl+A
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press('a');
  delay(100);
  Keyboard.releaseAll();
}

void F3() {
  // Emulate a key combination of Ctrl+C
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press('c');
  delay(100);
  Keyboard.releaseAll();
}

void F4() {
  // Emulate a key combination of Ctrl+X
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press('x');
  delay(100);
  Keyboard.releaseAll();
}

void F5() {
  // Emulate a key combination of Ctrl+V
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press('v');
  delay(100);
  Keyboard.releaseAll();
}

void F6() {
  // Emulate a key combination of Windows key and D
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press('d');
  delay(100);
  Keyboard.releaseAll();
}

void F7() {
  // Emulate a key combination of Ctrl, Windows key, and T
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press('t');
  delay(100);
  Keyboard.releaseAll();
}

void F8() {
  // Emulate a key combination of Windows key and R
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press('r');
  delay(100);
  Keyboard.releaseAll();
}

void F9() {
  // Emulate a key combination of Windows key and E
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press('e');
  delay(100);
  Keyboard.releaseAll();
}

void Layout1(char button) {
  switch (button) {
    case '1':
      F1();
      break;
    case '2':
      F2();
      break;
    case '3':
      F3();
      break;
    case '4':
      F4();
      break;
    case '5':
      F5();
      break;
    case '6':
      F6();
      break;
    case '7':
      F7();
      break;
    case '8':
      F8();
      break;
    case '9':
      F9();
      break;
  }
}

void Layout2(char button) {
  switch (button) {
    case '1':
      // F1 - Alt, Ctrl, Shift, W (Export)
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press(KEY_LEFT_SHIFT);
      Keyboard.press('w');
      delay(100);
      Keyboard.releaseAll();
      break;
    case '2':
      // F2 - Ctrl, Shift, A (Photoshop Raw)
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press(KEY_LEFT_SHIFT);
      Keyboard.press('a');
      delay(100);
      Keyboard.releaseAll();
      break;
    case '3':
      // F3 - Alt, Shift, Ctrl, E (Stamp Visible)
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press(KEY_LEFT_SHIFT);
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press('e');
      delay(100);
      Keyboard.releaseAll();
      break;
    case '4':
      // F4 - Ctrl, Shift, X (Liquify)
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press(KEY_LEFT_SHIFT);
      Keyboard.press('x');
      delay(100);
      Keyboard.releaseAll();
      break;
    case '5':
      // F5 - Ctrl, S (Save)
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press('s');
      delay(100);
      Keyboard.releaseAll();
      break;
    case '6':
      // F6 - Shift, F1 (Action 1)
      Keyboard.press(KEY_LEFT_SHIFT);
      Keyboard.press(KEY_F1);
      delay(100);
      Keyboard.releaseAll();
      break;
    case '7':
      // F7 - Shift, F2 (Action 2)
      Keyboard.press(KEY_LEFT_SHIFT);
      Keyboard.press(KEY_F2);
      delay(100);
      Keyboard.releaseAll();
      break;
    case '8':
      // F8 - Shift, F3 (Action 3)
      Keyboard.press(KEY_LEFT_SHIFT);
      Keyboard.press(KEY_F3);
      delay(100);
      Keyboard.releaseAll();
      break;
    case '9':
      // F9 - Shift, F4 (Action 4)
      Keyboard.press(KEY_LEFT_SHIFT);
      Keyboard.press(KEY_F4);
      delay(100);
      Keyboard.releaseAll();
      break;
  }
}


void Layout3(char button) {
  // Layout 3 implementation
}

void processKeypad(char key) {
  switch (key) {
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
      Layout1(key);
      break;
  }
}

void loop() {
  // Check encoder A button
  if (digitalRead(SW_A) == LOW) {
    int fall = millis();
    while (digitalRead(SW_A) == LOW) {}
    int rise = millis();
    if (rise - fall > timeLimit) {
      Consumer.write(MEDIA_NEXT);
    } else {
      Consumer.write(MEDIA_PLAY_PAUSE);
    }
    Keyboard.releaseAll();
  }

  // Check encoder A rotation
  long newPosition_A = volumeKnob_A.read();
  if (newPosition_A != oldPosition_A) {
    if (newPosition_A > oldPosition_A) {
      Consumer.write(MEDIA_VOLUME_UP);
    } else {
      Consumer.write(MEDIA_VOLUME_DOWN);
    }
    oldPosition_A = newPosition_A;
  }

  // Check encoder B rotation
  long newPosition_B = volumeKnob_B.read();
  if (newPosition_B > oldPosition_B) {
    // Clockwise rotation
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press(KEY_TAB);
    delay(20);
    Keyboard.releaseAll();
  } else if (newPosition_B < oldPosition_B) {
    // Counterclockwise rotation
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press(KEY_TAB);
    Keyboard.press(KEY_LEFT_SHIFT);
    delay(20);
    Keyboard.releaseAll();
  }
  oldPosition_B = newPosition_B;

  // Check encoder B button
  if (digitalRead(SW_B) == LOW) {
    ChangeState();
    while (digitalRead(SW_B) == LOW) {}
  }

  // Check F10 switch
  if (digitalRead(SW_F10) == LOW) {
    while (digitalRead(SW_F10) == LOW) {}
    Keyboard.press(KEY_LEFT_CTRL);
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('d');
    delay(100);
    Keyboard.releaseAll();
  }

  // Check keypad buttons
  char key = keypad.getKey();
  if (key) {
    processKeypad(key);
  }
}

are you using this encoder library?

do you have current limiting resistor in series with the LEDs ?
can your button bounce ?

your delay(100) will slow down the code before returning to the loop where you check the encoders

yes i have added the resistors to the encoders and the leds and diodes to the matrix switches

and still facing the problem

yes i am using the encoder library

tell me if i am missing something

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.