4 Daisy chained CD4021 shift registers gives us 32 inputs.
void read_keyboard(){ //set it to 0 to transmit data serially digitalWrite(latchPin,LOW); //while the shift register is in serial mode //collect each shift register into a byte //the register attached to the chip comes in first for (int i = 0; i < num_registers; i++){ button_states[i] = shiftIn(dataPin, clockPin); } boolean something_in = false; for (int i = 0; i < num_registers; i++){ something_in = translate_buttons_2_keys(button_states[i], letters[i]); if (something_in) last_key_press = millis(); } digitalWrite(latchPin,HIGH);}
This code checks every shift register to see if any buttons are being pressed. If there is one being pressed then the something_in variable is set to true. The read_keyboard() function calls the translate_buttons_2_keys() function to read from each shift register. This function goes like this:
boolean translate_buttons_2_keys(byte buttons, char* keys){ for(int i = 0 ; i < 8; i++){ if ( (1 << i) & buttons ){ cur_char = keys[i]; Keyboard.print(keys[i]); char_index++; return true; } } return false; }
This function iterates over each of the eight input pins on the CD4021 and checking its state in the line:
if ( (1 << i) & buttons )This if statement is true when the ith button is being pressed. in that case we grab the character from the keys array and send it out to the USB keyboard in the line:
Keyboard.print(keys[i]);We use the shiftIn function defined at the end of this file but you could just as well use the Arduino ShiftIn function .Grab the code below!
/* LucidTronix AlphaZed Keyboard * works as standard USB keyboard * Tutorial and details at: * http://www.lucidtronix.com/tutorials/15 * AlphaZed connected to pins 3,4,5 * on Arduino Leonardo. * This code requires Arduino Leonardo */ int dataPin = 3;int clockPin = 4;int latchPin = 5;unsigned long last_press = 0;unsigned long last_key_press = 0;char* letters[] ={"_ zy--.!","tsrqxwvu","lkjiponm","dcbahgfe" };int num_chars = 43;int num_registers = 4;byte button_states[] = {0x00,0x00,0x00,0x00}; //01001000int cursor_index = 0;int char_index = 0;char cur_char = 'X';void setup() { Serial.begin(9600); pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, INPUT); Keyboard.begin();}void loop() { if (millis() - last_key_press > 350) read_keyboard(); delay(50);}void read_keyboard(){ //set it to 0 to transmit data serially digitalWrite(latchPin,LOW); //while the shift register is in serial mode //collect each shift register into a byte //the register attached to the chip comes in first for (int i = 0; i < num_registers; i++){ button_states[i] = shiftIn(dataPin, clockPin); } boolean something_in = false; for (int i = 0; i < num_registers; i++){ something_in = translate_buttons_2_keys(button_states[i], letters[i]); if (something_in) last_key_press = millis(); } digitalWrite(latchPin,HIGH);}boolean translate_buttons_2_keys(byte buttons, char* keys){ for(int i = 0 ; i < 8; i++){ if ( (1 << i) & buttons ){ cur_char = keys[i]; Keyboard.print(keys[i]); char_index++; return true; } } return false; }byte shiftIn(int myDataPin, int myClockPin) { int i; int temp = 0; int pinState; byte myDataIn = 0; pinMode(myClockPin, OUTPUT); pinMode(myDataPin, INPUT); for (i=7; i>=0; i--) { digitalWrite(myClockPin, 0); delayMicroseconds(20); temp = digitalRead(myDataPin); if (temp) { pinState = 1; myDataIn = myDataIn | (1 << i); } else { pinState = 0; } digitalWrite(myClockPin, 1); } return myDataIn;}
| Title | Description | # | Cost | Link | Picture |
| SMD Keyboard PCB | PCB holds 4 CD4021 shift registers and 30 buttons. | 1 | $8.0 | Link |
|
| Shift Register | IC STATIC SHFT REG 8STG 16-TSSOP Parallel or Serial to Serial 3 V ~ 18 VValue: Parallel to Serial | 4 | $0.45 | Link |
|
| Resistor Array | RES ARRAY 10K OHM 4 RES 1206Value:10k | 8 | $0.12 | Link |
|
| Button 6mm Through Hole | Tactile Switch Through Hole SPST-NO 0.05A 12VValue:0.05A @ 12VDC | 32 | $0.1 | Link |
|
Comments: