Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int input_pins[5] = { 5, 6, 7, 8, 9 } ;
- int strobe_pins[5] = { 18, 15, 14, 16, 10 };
- unsigned long key_state[5][5];
- int keycode[5][5] = { { 'a', 'b', 'c', 'd', 'e' },
- { 'f', 'g', 'h', 'i', 'j' },
- { 'k', 'l', 'm', 'n', 'o' },
- { 'p', 'q', 'r', 's', 't' },
- { 'u', 'v', 'w', 'x', 'y' } };
- int strobe_row = 0;
- int q = 0;
- void setup() {
- int cnt;
- int cnt2;
- pinMode(17, OUTPUT);
- for (cnt = 0; cnt < 5; cnt++) {
- pinMode(input_pins[cnt], INPUT_PULLUP);
- pinMode(strobe_pins[cnt], OUTPUT);
- digitalWrite(strobe_pins[cnt], HIGH);
- for (cnt2 = 0; cnt2 < 5; cnt2++) key_state[cnt][cnt2] = 0;
- }
- Keyboard.begin();
- Serial.begin(9600);
- Serial.println("Starting keyboard");
- }
- bool debounce(unsigned long t_now, unsigned long t_prev) {
- unsigned long diff;
- diff = t_now - t_prev; // need to check for underflow?
- if (diff <= 20) return true;
- else return false;
- }
- void loop() {
- unsigned long tick_now = millis();
- int cnt;
- if (q == 0) digitalWrite(17, LOW);
- else if (q == 128) digitalWrite(17, HIGH);
- q++;
- if (q == 256) q = 0;
- // since we use non zero to indicate pressed state, we need
- // to handle the edge case where millis() returns 0
- if (tick_now == 0) tick_now = 1;
- if (strobe_row >= 5) strobe_row = 0;
- digitalWrite(strobe_pins[strobe_row], LOW);
- delay(2); // give it some time to stabilize just in case
- for (cnt = 0; cnt < 5; cnt++) {
- // ignore state change for pin if in debounce period
- if (key_state[strobe_row][cnt] != 0)
- if (debounce(tick_now, key_state[strobe_row][cnt]) == true)
- continue;
- if (digitalRead(input_pins[cnt]) == HIGH) {
- Serial.println("released");
- if (key_state[strobe_row][cnt] != 0) {
- Keyboard.release(keycode[strobe_row][cnt]);
- key_state[strobe_row][cnt] = 0;
- }
- } else {
- if (key_state[strobe_row][cnt] == 0) {
- Keyboard.press(keycode[strobe_row][cnt]);
- key_state[strobe_row][cnt] = tick_now;
- }
- }
- }
- digitalWrite(strobe_pins[strobe_row], HIGH);
- strobe_row++;
- delay(5);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement