Program to Interface 4*4 Button Pad to Arduino using 4 Digital Pins (Part 2 of 3)

<<<< Read Previous Part (Part 1)

In the previous part, we saw the circuit for interfacing a 4*4 button pad to arduino using only four digital pins (instead of eight digital pins) of arduino. After completing the circuit, upload the following program to your arduino board. Don’t forget to turn on external power supply. Otherwise, you will get an error. If uploading is successful, open your serial monitor. Now, press the switches in your button pad. Number corresponding to pressed switch will be displayed in the serial monitor.

//Pin connected to ST_CP of 74595
int latchPin = 12;
//Pin connected to SH_CP of 74595
int clockPin = 13;
//Pin connected to DS of 74595
int dataPin = 11;

// Arduino Pin to which QH pin of 74165 is connected
int Read_Pin = 10 ;

int O_value;   // Value read from the O terminal of 4*4 button pad
int P_value;   // Value read from the P terminal of 4*4 button pad
int Q_value;   // Value read from the Q terminal of 4*4 button pad
int R_value;   // Value read from the R terminal of 4*4 button pad

int read_value;
int i;

// Function to Shift Out Numbers to 74595

void Shift_Out_to_74595(int Shift_Value)
{
    // take the latchPin low so 
    // the LEDs don't change while you're sending in bits:
    digitalWrite(latchPin, LOW);
    // shift out the bits:
    shiftOut(dataPin, clockPin, MSBFIRST, Shift_Value);  
    //take the latch pin high so the LEDs will light up:
    digitalWrite(latchPin, HIGH); 
    
    return;
}

// Function to Read the state of O, P, Q and R 
// terminals of 4*4 button pad serially from 74165

int Read_From_74165(int Add_value, int i_value)
{     
  //  reading value  
      
  Shift_Out_to_74595(Add_value + 16);       
  Shift_Out_to_74595(Add_value + 80);     
  Shift_Out_to_74595(Add_value + 64); 

  for(i=0;i<8;i++)
  {
    Shift_Out_to_74595(Add_value + 64);     
    if(i==i_value)
    {
      read_value = digitalRead(Read_Pin);
    }
    Shift_Out_to_74595(Add_value + 96);  
  }     
  return(read_value);  
}

void Print_Key(char Key_number)
{
  Serial.print(Key_number);
  delay(500);
  return;
}

void setup() {
  //set pins to output so you can control the shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(Read_Pin, INPUT);  
  Shift_Out_to_74595(255);     
  Serial.begin(9600); 
}

void loop() {
    
  // Q4 of 74595 is connected to the CLK_INH (pin 15) OF 74165
  // Q5 of 74595 is connected to the CLK (pin 2) OF 74165
  // Q6 of 74595 is connected to the SH/LD (pin 1) OF 74165
  
  // K terminal of 4*4 Switch Matrix is connected to Q0 pin of 74595 - 1
  // L terminal of 4*4 Switch Matrix is connected to Q1 pin of 74595 - 2
  // M terminal of 4*4 Switch Matrix is connected to Q2 pin of 74595 - 4
  // N terminal of 4*4 Switch Matrix is connected to Q3 pin of 74595 - 8

  // O terminal of 4*4 Switch Matrix is connected to A pin of 74165
  // P terminal of 4*4 Switch Matrix is connected to B pin of 74165
  // Q terminal of 4*4 Switch Matrix is connected to C pin of 74165
  // R terminal of 4*4 Switch Matrix is connected to D pin of 74165
   
  label:
  
    // Read the state of O, P, Q and R 
    // terminals of 4*4 button pad serially from 74165 

    Shift_Out_to_74595(16);       
    Shift_Out_to_74595(80);     
    Shift_Out_to_74595(64); 

    for(i=0;i<8;i++)
    {
      Shift_Out_to_74595(64);     
      if(i==7)
      {
        O_value = digitalRead(Read_Pin);
      }
      
      if(i==6)
      {
        P_value = digitalRead(Read_Pin);
      }
      
      if(i==5)
      {
        Q_value = digitalRead(Read_Pin);
      }
      
      if(i==4)
      {
        R_value = digitalRead(Read_Pin);
      }
      
      Shift_Out_to_74595(96);  
    }  

    if(O_value==0) //IF ANY SWITCH IN 1, 4, 7 and '*' IS PRESSED, O_value BECOME 0
    {  
      //  SEND LOW TO K AND HIGH TO L, M AND N     
      Shift_Out_to_74595(14); 
      // IF VALUE READ FROM O REMAINS IN 0, SWITCH PRESSED WILL BE 1           
      read_value = Read_From_74165(14, 7);   
      if(read_value==0)
      {
        Print_Key('1');
        goto label;
      }
      
      //  SEND LOW TO L AND HIGH TO K, M AND N     
      Shift_Out_to_74595(13); 
      // IF VALUE READ FROM O REMAINS IN 0, SWITCH PRESSED WILL BE 4
      read_value = Read_From_74165(13, 7);     
      if(read_value==0)
      {
        Print_Key('4');
        goto label;
      }

      //  SEND LOW TO M AND HIGH TO K, L AND N     
      Shift_Out_to_74595(11);
      // IF VALUE READ FROM O REMAINS IN 0, SWITCH PRESSED WILL BE 7     
      //  reading value  
      read_value = Read_From_74165(11, 7);     
      if(read_value==0)
      {
        Print_Key('7');
        goto label;
      }    
    
      //  SEND LOW TO N AND HIGH TO K, L AND M      
      Shift_Out_to_74595(7); 
      // IF VALUE READ FROM O REMAINS IN 0, SWITCH PRESSED WILL BE *
      read_value = Read_From_74165(7, 7);    
      if(read_value==0)
      {
        Print_Key('*');
        goto label;
      }      
    }  
  
    if(P_value==0) //IF ANY SWITCH IN 2, 5, 8 and 0 IS PRESSED, P_value BECOME 0
    {  
      //  SEND LOW TO K AND HIGH TO L, M AND N     
      Shift_Out_to_74595(14); 
      // IF VALUE READ FROM P REMAINS IN 0, SWITCH PRESSED WILL BE 2           
      read_value = Read_From_74165(14, 6);   
      if(read_value==0)
      {
        Print_Key('2');
        goto label;
      }
      
      //  SEND LOW TO L AND HIGH TO K, M AND N     
      Shift_Out_to_74595(13); 
      // IF VALUE READ FROM P REMAINS IN 0, SWITCH PRESSED WILL BE 5
      read_value = Read_From_74165(13, 6);     
      if(read_value==0)
      {
        Print_Key('5');
        goto label;
      }

      //  SEND LOW TO M AND HIGH TO K, L AND N     
      Shift_Out_to_74595(11);
      // IF VALUE READ FROM P REMAINS IN 0, SWITCH PRESSED WILL BE 8     
      //  reading value  
      read_value = Read_From_74165(11, 6);     
      if(read_value==0)
      {
        Print_Key('8');
        goto label;
      }    
    
      //  SEND LOW TO N AND HIGH TO K, L AND M      
      Shift_Out_to_74595(7); 
      // IF VALUE READ FROM P REMAINS IN 0, SWITCH PRESSED WILL BE 0
      read_value = Read_From_74165(7, 6);    
      if(read_value==0)
      {
        Print_Key('0');
        goto label;
      }      
    }

    if(Q_value==0) //IF ANY SWITCH IN 3, 6, 9 and # IS PRESSED, Q_value BECOME 0
    {  
      //  SEND LOW TO K AND HIGH TO L, M AND N     
      Shift_Out_to_74595(14); 
      // IF VALUE READ FROM Q REMAINS IN 0, SWITCH PRESSED WILL BE 3           
      read_value = Read_From_74165(14, 5);   
      if(read_value==0)
      {
        Print_Key('3');
        goto label;
      }
      
      //  SEND LOW TO L AND HIGH TO K, M AND N     
      Shift_Out_to_74595(13); 
      // IF VALUE READ FROM Q REMAINS IN 0, SWITCH PRESSED WILL BE 6
      read_value = Read_From_74165(13, 5);     
      if(read_value==0)
      {
        Print_Key('6');
        goto label;
      }

      //  SEND LOW TO M AND HIGH TO K, L AND N     
      Shift_Out_to_74595(11);
      // IF VALUE READ FROM Q REMAINS IN 0, SWITCH PRESSED WILL BE 9     
      //  reading value  
      read_value = Read_From_74165(11, 5);     
      if(read_value==0)
      {
        Print_Key('9');
        goto label;
      }    
    
      //  SEND LOW TO N AND HIGH TO K, L AND M      
      Shift_Out_to_74595(7); 
      // IF VALUE READ FROM Q REMAINS IN 0, SWITCH PRESSED WILL BE #
      read_value = Read_From_74165(7, 5);    
      if(read_value==0)
      {
        Print_Key('#');
        goto label;
      }      
    }

    if(R_value==0) //IF ANY SWITCH IN A, B, C and D IS PRESSED, R_value BECOME 0
    {  
      //  SEND LOW TO K AND HIGH TO L, M AND N     
      Shift_Out_to_74595(14); 
      // IF VALUE READ FROM R REMAINS IN 0, SWITCH PRESSED WILL BE A           
      read_value = Read_From_74165(14, 4);   
      if(read_value==0)
      {
        Print_Key('A');
        goto label;
      }
      
      //  SEND LOW TO L AND HIGH TO K, M AND N     
      Shift_Out_to_74595(13); 
      // IF VALUE READ FROM R REMAINS IN 0, SWITCH PRESSED WILL BE B
      read_value = Read_From_74165(13, 4);     
      if(read_value==0)
      {
        Print_Key('B');
        goto label;
      }

      //  SEND LOW TO M AND HIGH TO K, L AND N     
      Shift_Out_to_74595(11);
      // IF VALUE READ FROM R REMAINS IN 0, SWITCH PRESSED WILL BE C     
      //  reading value  
      read_value = Read_From_74165(11, 4);     
      if(read_value==0)
      {
        Print_Key('C');
        goto label;
      }    
    
      //  SEND LOW TO N AND HIGH TO K, L AND M      
      Shift_Out_to_74595(7); 
      // IF VALUE READ FROM R REMAINS IN 0, SWITCH PRESSED WILL BE D
      read_value = Read_From_74165(7, 4);    
      if(read_value==0)
      {
        Print_Key('D');
        goto label;
      }      
    }   
}

Continued in Next Part (Part 3) >>>>

(Visited 1,307 times, 1 visits today)
paulpp644
A deep electronic enthusiast who spent most of my time for electronic experiments. I am also interested in publishing my experiments in my blog for helping other electronics enthusiasts.