Keyboard kit using CD4021 shift registers and big through hole buttons.
char* letters[] ={"yz ---.-","qrstuvwx","ijklmnop","abcdefgh" };
So that the right buttons correspond to the right letters. The heart of the code is the read_keyboard() function which goes like this:
void read_keyboard(){ digitalWrite(latchPin,0); for (int i = 0; i < num_registers; i++){ button_states[i] = shiftIn(dataPin, clockPin); } digitalWrite(latchPin,1); 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(); }}
This function iterates over the 4 shift registers and reads the data in from each of them. For each shift register it call the translate_buttons_2_keys() function to map a button to a particular character. The translate_buttons_2_keys() function goes like this:
boolean translate_buttons_2_keys(byte buttons, char* keys){ for(int i = 0 ; i < 8; i++){ if ( (1 << i) & buttons ){ Serial.println(keys[i]); return true; } } return false; }
This code receives a byte of button states from the read_keyboard() function as well as a string of 8 letters from letters array. If a particular button has been pressed then that bit in the button state byte will be set to 1, otherwise it will be 0. If we encounter a one we print out the corresponding character to the serial port.
/* LucidTronix DIY Keyboard// Print keys to the serial port// read 4 shift registers in// http://lucidtronix.com/tutorials/44*/int dataPin = 3;int clockPin = 4;int latchPin = 5;unsigned long last_press = 0;unsigned long last_key_press = 0;char* letters[] ={"yz ---.-","qrstuvwx","ijklmnop","abcdefgh" };int num_registers = 4;byte button_states[] = {0x00,0x00,0x00,0x00}; //01001000void setup() { Serial.begin(9600); pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, INPUT);}void loop() { read_keyboard(); //Serial.println("Read keyz"); delay(350);}void read_keyboard(){ digitalWrite(latchPin,0); for (int i = 0; i < num_registers; i++){ button_states[i] = shiftIn(dataPin, clockPin); } digitalWrite(latchPin,1); 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(); }}boolean translate_buttons_2_keys(byte buttons, char* keys){ for(int i = 0 ; i < 8; i++){ if ( (1 << i) & buttons ){ Serial.println(keys[i]); 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 |
| PCB Keyboard DIY Through Hole | Printed circuit board for our DIY Shift register based keyboard. | 1 | $6.0 | Link |
|
| Resistor Array 8 10K 9 pins Through Hole | Resistor Array with 9 pins and 8 resistors through hole, Used in our DIY Keyboard.Value:10k | 4 | $0.62 | Link |
|
| Shift Register CD4021 16-DIP | Parallel-in Serial-Out CD4021 Shift Register from Texas Instruments. Through-hole DIP package.Value:Parallel to Serial | 4 | $0.52 | Link |
|
| Button Through Hole 12mm | Through Hole button with 4 legs square 12mm package and black depressor.Value:SPST-NO | 8 | $0.37 | Link |
|
| Button 6mm Through Hole | Tactile Switch Through Hole SPST-NO 0.05A 12VValue:0.05A @ 12VDC | 24 | $0.1 | Link |
|
Comments: