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.