//defineing the serial data pins //Pin connected to shift registers int latchPin = 8; //both shift registers int clockPin = 12;//both shift registers int dataOutPin = 11; //the data out pin 74HC595 only int dataInPin = 5;//the data in CD4021BE only //variables int shiftR1=0; //holds value for the first 74HC595 shift register. control single by sending(0,1,2,4,8,16,32,64,128) int shiftR2=0; //holds value for the second 74HC595 shift register (0,1,2,4,8,16,32,64,128) int shiftPin[9] ={0,1,2,4,8,16,32,64,128};// when sent to the shift register will only turn on 1 pin byte dataIn =72; //data coming from CD4021BE register /* { 74HC595 0 - all pins low 1 - pin 15 high 2 - pin 1 high 4 - pin 2 high 8 - pin 3 high 16 - pin 4 high 32 - pin 5 high 64 - pin 6 high 128 - pin 7 high } */ char btMatrix[3][10]={//keyboard rows are pins(1,15,14) on CD4021BE columns are 74HC595 {'Q','W','E','R','T','Y','U','I','O','P'},//keyboard will be left align {'A','S','D','F','G','H','J','K','L','0'}, {'Z','X','C','V','B','N','M','0','0',' '}};//the zeros are just place holders char userIn= '0';//holds the user inputted letter char oldIn='0';//holds that last value of userIn. for comparison bool noInput =true;// used to reset and be able to press the same button a second time(true = no input, false = input) void setup() { pinMode(latchPin,OUTPUT); pinMode(clockPin,OUTPUT); pinMode(dataOutPin,OUTPUT);//data that is being sent to the shift registers pinMode(dataInPin,INPUT);//data that is read from the CD4021BE shift registers Serial.begin(9600);//enable serial monitor to help see whats going on } void loop() { for(int i =1; i<9;i++){ //turns on then off each pin one by one of the first shift register //Serial.println(dataIn,BIN);//printing data //sending data to 74HC595 digitalWrite(latchPin,LOW);//needs to be set low to send data shiftOut(dataOutPin, clockPin, MSBFIRST,shiftPin[0]);//sending the data to second shift register and it should be off shiftOut(dataOutPin, clockPin, MSBFIRST,shiftPin[i]);//turns one pin high on first shift register digitalWrite(latchPin,HIGH);//sending data to shift register need to shift in while latch is high //delayMicroseconds(50); //reading data from CD4021BE digitalWrite(clockPin,HIGH);//This needs to be set high before latch is pulled low. Otherwise pin 1 on CD4021BE will not be counted digitalWrite(latchPin,LOW);//needs to be set low to send data dataIn = shiftIn(dataInPin,clockPin,MSBFIRST);//reading data from CD4021BE digitalWrite(latchPin,HIGH);//sending data to arduino //finding the the button that was pressed switch (dataIn){ case 128://pin 1 on CD4021BE userIn = btMatrix[0][i-1];// setting userIn to the letter that was pressed. i starts at val 1 but matrix starts at val 0 noInput=false;// a button was pressed break; case 64://pin 15 on CD4021BE userIn = btMatrix[1][i-1]; noInput=false;// a button was pressed break; case 32://pin 14 on CD4021BE userIn = btMatrix[2][i-1]; noInput=false;// a button was pressed break; default: noInput= true; //a button was not pressed break; } if(noInput==false) break;//if a button was pressed on need to continue the loop } for(int i =1; i<3;i++){ //turns on then off each pin one by one of the first shift register. only using pins 1,2 on second //Serial.println(dataIn,BIN);//printing data if(noInput==false) break;//if a button was pressed on need to continue the loop //sending data to 74HC595 digitalWrite(latchPin,LOW);//needs to be set low to send data shiftOut(dataOutPin, clockPin, MSBFIRST,shiftPin[i]);//sending the data to second shift register and it should be off shiftOut(dataOutPin, clockPin, MSBFIRST,shiftPin[0]);//turns one pin high on first shift register digitalWrite(latchPin,HIGH);//sending data to shift register need to shift in while latch is high //delayMicroseconds(50); //reading data from CD4021BE digitalWrite(clockPin,HIGH);//This needs to be set high before latch is pulled low. Otherwise pin 1 on CD4021BE will not be counted digitalWrite(latchPin,LOW);//needs to be set low to send data dataIn = shiftIn(dataInPin,clockPin,MSBFIRST);//reading data from CD4021BE digitalWrite(latchPin,HIGH);//sending data to arduino //finding the the button that was pressed switch (dataIn){ case 128://pin 1 on CD4021BE userIn = btMatrix[0][i+7];//i starts at val 1 but need to be moved up 7 to continue matrix noInput=false;// a button was pressed break; case 64://pin 15 on CD4021BE userIn = btMatrix[1][i+7]; noInput=false;// a button was pressed break; case 32://pin 14 on CD4021BE userIn = btMatrix[2][i+7]; noInput=false;// a button was pressed break; default: //dose not need noInput set to true because if no buttons were pressed noInput= true will carry over from the previous for loop break; } } //checking the input note:can not hold down button if (userIn !='0' && userIn != oldIn)//if a button is pressed and not being held down { Serial.print(userIn);// printing the letter oldIn = userIn;//stops it from printing that same letter over and over } else if(userIn == oldIn){//nobuttons were pressed reset the ability to press the same button if(noInput==true){//no button was pressed userIn='0';//reset user input oldIn='0';//reset old input } } }