Program for a Seven Segment Display Controlled by a 4*4 Button Pad (Part 2 of 3)

<<<< Read Previous Part (Part 2)

In the previous part, we saw the circuit to control a common cathode seven segment display by a 4*4 button pad which is controlled using only 4 digital pins of arduino, instead of 8. 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, press the switches in your 4*4 button pad. Arduino should be powered through USB from your computer. Number corresponding to pressed switch will be displayed in the common cathode seven segment display.

//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 IC_4511_D0 = 5; // Arduino Pin 5 is connected to D0 (pin 7) of 4511
int IC_4511_D1 = 2; // Arduino Pin 2 is connected to D1 (pin 1) of 4511
int IC_4511_D2 = 3; // Arduino Pin 3 is connected to D2 (pin 2) of 4511
int IC_4511_D3 = 4; // Arduino Pin 4 is connected to D3 (pin 6) of 4511

int IC_4511_LE = 6; // Arduino Pin 6 is connected to LE (pin 5) of 4511

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 seven_segment_display(int number)
{
  if(number==0)
  {
    digitalWrite(IC_4511_LE, LOW);
        
    digitalWrite(IC_4511_D3, LOW);
    digitalWrite(IC_4511_D2, LOW);
    digitalWrite(IC_4511_D1, LOW);
    digitalWrite(IC_4511_D0, LOW);
    
    digitalWrite(IC_4511_LE, HIGH);
    
  }
  
  else if(number==1)
  {
    digitalWrite(IC_4511_LE, LOW);
        
    digitalWrite(IC_4511_D3, LOW);
    digitalWrite(IC_4511_D2, LOW);
    digitalWrite(IC_4511_D1, LOW);
    digitalWrite(IC_4511_D0, HIGH);
    
    digitalWrite(IC_4511_LE, HIGH);
    
  }

  else if(number==2)
  {
    digitalWrite(IC_4511_LE, LOW);
        
    digitalWrite(IC_4511_D3, LOW);
    digitalWrite(IC_4511_D2, LOW);
    digitalWrite(IC_4511_D1, HIGH);
    digitalWrite(IC_4511_D0, LOW);
    
    digitalWrite(IC_4511_LE, HIGH);
    
  }
  
  else if(number==3)
  {
    digitalWrite(IC_4511_LE, LOW);
        
    digitalWrite(IC_4511_D3, LOW);
    digitalWrite(IC_4511_D2, LOW);
    digitalWrite(IC_4511_D1, HIGH);
    digitalWrite(IC_4511_D0, HIGH);
    
    digitalWrite(IC_4511_LE, HIGH);
    
  }
  
  else if(number==4)
  {
    digitalWrite(IC_4511_LE, LOW);
        
    digitalWrite(IC_4511_D3, LOW);
    digitalWrite(IC_4511_D2, HIGH);
    digitalWrite(IC_4511_D1, LOW);
    digitalWrite(IC_4511_D0, LOW);
    
    digitalWrite(IC_4511_LE, HIGH);
    
  }
  
  else if(number==5)
  {
    digitalWrite(IC_4511_LE, LOW);
        
    digitalWrite(IC_4511_D3, LOW);
    digitalWrite(IC_4511_D2, HIGH);
    digitalWrite(IC_4511_D1, LOW);
    digitalWrite(IC_4511_D0, HIGH);
    
    digitalWrite(IC_4511_LE, HIGH);
    
  }
  
  else if(number==6)
  {
    digitalWrite(IC_4511_LE, LOW);
        
    digitalWrite(IC_4511_D3, LOW);
    digitalWrite(IC_4511_D2, HIGH);
    digitalWrite(IC_4511_D1, HIGH);
    digitalWrite(IC_4511_D0, LOW);
    
    digitalWrite(IC_4511_LE, HIGH);
    
  }
  
  else if(number==7)
  {
    digitalWrite(IC_4511_LE, LOW);
        
    digitalWrite(IC_4511_D3, LOW);
    digitalWrite(IC_4511_D2, HIGH);
    digitalWrite(IC_4511_D1, HIGH);
    digitalWrite(IC_4511_D0, HIGH);
    
    digitalWrite(IC_4511_LE, HIGH);
    
  }
  
  else if(number==8)
  {
    digitalWrite(IC_4511_LE, LOW);
        
    digitalWrite(IC_4511_D3, HIGH);
    digitalWrite(IC_4511_D2, LOW);
    digitalWrite(IC_4511_D1, LOW);
    digitalWrite(IC_4511_D0, LOW);
    
    digitalWrite(IC_4511_LE, HIGH);
    
  }
  
  else if(number==9)
  {
    digitalWrite(IC_4511_LE, LOW);
        
    digitalWrite(IC_4511_D3, HIGH);
    digitalWrite(IC_4511_D2, LOW);
    digitalWrite(IC_4511_D1, LOW);
    digitalWrite(IC_4511_D0, HIGH);
    
    digitalWrite(IC_4511_LE, HIGH);
    
  }
  
  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); 

  pinMode(IC_4511_D0, OUTPUT);
  pinMode(IC_4511_D1, OUTPUT);
  pinMode(IC_4511_D2, OUTPUT);
  pinMode(IC_4511_D3, OUTPUT);
  pinMode(IC_4511_LE, OUTPUT);
  
  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');
        seven_segment_display(1);  // Print 1 in Seven Segment Display
        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');
        seven_segment_display(4);  // Print 4 in Seven Segment Display
        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');
        seven_segment_display(7);  // Print 7 in Seven Segment Display
        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');
        seven_segment_display(2);  // Print 2 in Seven Segment Display
        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');
        seven_segment_display(5);  // Print 5 in Seven Segment Display
        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');
        seven_segment_display(8);  // Print 8 in Seven Segment Display
        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');
        seven_segment_display(0);  // Print 0 in Seven Segment Display
        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');
        seven_segment_display(3);  // Print 3 in Seven Segment Display
        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');
        seven_segment_display(6);  // Print 6 in Seven Segment Display
        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');
        seven_segment_display(9);  // Print 9 in Seven Segment Display
        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 477 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.