Untitled

 avatar
unknown
plain_text
17 hours ago
1.4 kB
9
Indexable
#include <Keyboard.h>

// Updated pin assignments for Pro Micro
const int buttonPins[] = {2, 3, 4, 5, 6, 7, 8, 9};  // Buttons mapped to Pro Micro
const int togglePins[] = {10, 16, 14, 15}; // Toggles mapped to Pro Micro

// Key mappings
const char buttonKeys[] = {'W', 'A', 'S', 'D', 'Q', 'E', 'R', 'F'}; 
const char toggleKeys[] = {'1', '2', '3', '4'}; // Same key for ON and OFF

bool toggleLastState[4] = {false, false, false, false}; // Stores last known toggle states

void setup() {
    for (int i = 0; i < 8; i++) {
        pinMode(buttonPins[i], INPUT_PULLUP);
    }
    for (int i = 0; i < 4; i++) {
        pinMode(togglePins[i], INPUT_PULLUP);
    }
    Keyboard.begin();
}

void loop() {
    // Button Handling (momentary keypress)
    for (int i = 0; i < 8; i++) {
        if (digitalRead(buttonPins[i]) == LOW) {  
            Keyboard.press(buttonKeys[i]); 
            delay(50);
            Keyboard.release(buttonKeys[i]); 
        }
    }

    // Toggle Switch Handling (momentary keypress on state change)
    for (int i = 0; i < 4; i++) {
        bool currentState = digitalRead(togglePins[i]) == LOW; 
        
        if (currentState != toggleLastState[i]) { 
            toggleLastState[i] = currentState; 

            Keyboard.press(toggleKeys[i]);  // Send the same key for both ON and OFF
            delay(50);
            Keyboard.releaseAll();  
        }
    }
}
Editor is loading...
Leave a Comment