Blogroll

Powered by Blogger.

Tuesday, 19 August 2014

Program to Interface 4*4 Button Key Pad and Seven Segment Display to Arduino

by realfinetime  |  in Seven Segment Display at  01:14

Previous: Working of Circuit

int K = 4;  // K pin of  4*4 button pad is connected to the 4th digital pin of Arduino mega.
int L = 5;  // L pin of  4*4 button pad is connected to the 5th digital pin of Arduino mega.
int M = 6;  // M pin of  4*4 button pad is connected to the 6th digital pin of Arduino mega.
int N = 7;  // N pin of  4*4 button pad is connected to the 7th digital pin of Arduino mega.

int O = 8;  // O pin of  4*4 button pad is connected to the 8th digital pin of Arduino mega.
int P = 9;  // P pin of  4*4 button pad is connected to the 9th digital pin of Arduino mega.
int Q = 10; // Q pin of  4*4 button pad is connected to the 10th digital pin of Arduino mega.
int R = 11; // R pin of  4*4 button pad is connected to the 11th digital pin of Arduino mega.

int D0 = 0; // D0 pin of 4511 is connected to the 0th digital pin of arduino
int D1 = 1; // D1 pin of 4511 is connected to the 1st digital pin of arduino
int D2 = 2; // D2 pin of 4511 is connected to the 2nd digital pin of arduino
int D3 = 3; // D3 pin of 4511 is connected to the 3rd digital pin of arduino

int LE = 12; // LE pin of 4511 is connected to the 12th digital pin of arduino

int O_value;
int P_value;
int Q_value;
int R_value;

int read_value;


// the setup routine runs once when you press reset:
void setup() {          

  pinMode(K, OUTPUT);    // Declare K pin as OUTPUT
  pinMode(L, OUTPUT);    // Declare L pin as OUTPUT
  pinMode(M, OUTPUT);    // Declare M pin as OUTPUT
  pinMode(N, OUTPUT);    // Declare N pin as OUTPUT

  pinMode(O, INPUT);     // Declare O pin as INPUT
  pinMode(P, INPUT);     // Declare P pin as INPUT
  pinMode(Q, INPUT);     // Declare Q pin as INPUT
  pinMode(R, INPUT);     // Declare R pin as INPUT
  
  
  pinMode(LE, OUTPUT);    // Declare LE pin as OUTPUT  
  pinMode(D0, OUTPUT);    // Declare D0 pin as OUTPUT
  pinMode(D1, OUTPUT);    // Declare D1 pin as OUTPUT
  pinMode(D2, OUTPUT);    // Declare D2 pin as OUTPUT
  pinMode(D3, OUTPUT);    // Declare D3 pin as OUTPUT


// INITIALLY SEND LOW TO OUTPUT PINS (K, L, M and N)

 digitalWrite(K, LOW);
 digitalWrite(L, LOW);
 digitalWrite(M, LOW);
 digitalWrite(N, LOW);

}


// the loop routine runs over and over again forever:
void loop() {
  
  label:
  
  // INITIALLY SEND LOW TO OUTPUT PINS (K, L, M and N) IN THE WHILE LOOP

  digitalWrite(K, LOW);
  digitalWrite(L, LOW);
  digitalWrite(M, LOW);
  digitalWrite(N, LOW);

  O_value=digitalRead(O);  // READ VALUE FROM O
  P_value=digitalRead(P);  // READ VALUE FROM P
  Q_value=digitalRead(Q);  // READ VALUE FROM Q
  R_value=digitalRead(R);  // READ VALUE FROM R

  if(O_value==0) //IF ANY SWITCH IN 1, 4, 7 and '*' IS PRESSED O_value WILL BECOME 0
  {

    //  SEND LOW TO K AND HIGH TO L, M AND N
    digitalWrite(K, LOW);
    digitalWrite(L, HIGH);
    digitalWrite(M, HIGH);
    digitalWrite(N, HIGH);
 
    // IF VALUE READ FROM O IS 0, SWITCH PRESSED WILL BE 1
    read_value=digitalRead(O);  
    if(read_value==0)
    {
      // Display 1 in seven segment display, D3 D2 D1 D0 (0 0 0 1)
      digitalWrite(LE, LOW); // Displayed number will change if LE is LOW
      digitalWrite(D0, HIGH);
      digitalWrite(D1, LOW);
      digitalWrite(D2, LOW);
      digitalWrite(D3, LOW);
      digitalWrite(LE, HIGH); // Displayed number will not change if LE is HIGH
      delay(500);
      goto label;
    }
    
    //  SEND LOW TO L AND HIGH TO K, M AND N
    digitalWrite(K, HIGH);
    digitalWrite(L, LOW);
    digitalWrite(M, HIGH);
    digitalWrite(N, HIGH);

    // IF VALUE READ FROM O IS 0, SWITCH PRESSED WILL BE 4
    read_value=digitalRead(O);
    if(read_value==0)
    {
      // Display 4 in seven segment display, D3 D2 D1 D0 (0 1 0 0)
      digitalWrite(LE, LOW); // Displayed number will change if LE is LOW
      digitalWrite(D0, LOW);
      digitalWrite(D1, LOW);
      digitalWrite(D2, HIGH);
      digitalWrite(D3, LOW);
      digitalWrite(LE, HIGH); // Displayed number will not change if LE is HIGH
      delay(500);
      goto label;
    }

    //  SEND LOW TO M AND HIGH TO K, L AND N
    digitalWrite(K, HIGH);
    digitalWrite(L, HIGH);
    digitalWrite(M, LOW);
    digitalWrite(N, HIGH);

    // IF VALUE READ FROM O IS 0, SWITCH PRESSED WILL BE 7
    read_value=digitalRead(O);
    if(read_value==0)
    {
      // Display 7 in seven segment display, D3 D2 D1 D0 (0 1 1 1)
      digitalWrite(LE, LOW); // Displayed number will change if LE is LOW
      digitalWrite(D0, HIGH);
      digitalWrite(D1, HIGH);
      digitalWrite(D2, HIGH);
      digitalWrite(D3, LOW);
      digitalWrite(LE, HIGH); // Displayed number will not change if LE is HIGH
      delay(500);
      goto label;
    }

    //  SEND LOW TO N AND HIGH TO K, L AND M
    digitalWrite(K, HIGH);
    digitalWrite(L, HIGH);
    digitalWrite(M, HIGH);
    digitalWrite(N, LOW);

    // IF VALUE READ FROM O IS 0, SWITCH PRESSED WILL BE '*'
    read_value=digitalRead(O);
    if(read_value==0)
    {
      goto label;
    }

  }


  else if(P_value==0) //IF ANY SWITCH IN 2, 5, 8 and 0 IS PRESSED P_value WILL BECOME 0
  {
    
    //  SEND LOW TO K AND HIGH TO L, M AND N
    digitalWrite(K, LOW);
    digitalWrite(L, HIGH);
    digitalWrite(M, HIGH);
    digitalWrite(N, HIGH);

    // IF VALUE READ FROM P IS 0, SWITCH PRESSED WILL BE 2 
    read_value=digitalRead(P);
    if(read_value==0)
    {
      // Display 2 in seven segment display, D3 D2 D1 D0 (0 0 1 0)
      digitalWrite(LE, LOW); // Displayed number will change if LE is LOW
      digitalWrite(D0, LOW);
      digitalWrite(D1, HIGH);
      digitalWrite(D2, LOW);
      digitalWrite(D3, LOW);
      digitalWrite(LE, HIGH); // Displayed number will not change if LE is HIGH
      delay(500);
      goto label;
    }


    //  SEND LOW TO L AND HIGH TO K, M AND N
    digitalWrite(K, HIGH);
    digitalWrite(L, LOW);
    digitalWrite(M, HIGH);
    digitalWrite(N, HIGH);


    // IF VALUE READ FROM P IS 0, SWITCH PRESSED WILL BE 5 
    read_value=digitalRead(P);
    if(read_value==0)
    {
      // Display 5 in seven segment display, D3 D2 D1 D0 (0 1 0 1)
      digitalWrite(LE, LOW); // Displayed number will change if LE is LOW
      digitalWrite(D0, HIGH);
      digitalWrite(D1, LOW);
      digitalWrite(D2, HIGH);
      digitalWrite(D3, LOW);
      digitalWrite(LE, HIGH); // Displayed number will not change if LE is HIGH
      delay(500);
      goto label;
    }

    //  SEND LOW TO M AND HIGH TO K, L AND N
    digitalWrite(K, HIGH);
    digitalWrite(L, HIGH);
    digitalWrite(M, LOW);
    digitalWrite(N, HIGH);

    // IF VALUE READ FROM P IS 0, SWITCH PRESSED WILL BE 8 
    read_value=digitalRead(P);
    if(read_value==0)
    {
      // Display 8 in seven segment display, D3 D2 D1 D0 (1 0 0 0)
      digitalWrite(LE, LOW); // Displayed number will change if LE is LOW
      digitalWrite(D0, LOW);
      digitalWrite(D1, LOW);
      digitalWrite(D2, LOW);
      digitalWrite(D3, HIGH);
      digitalWrite(LE, HIGH); // Displayed number will not change if LE is HIGH
      delay(500);
      goto label;
    }

    //  SEND LOW TO N AND HIGH TO K, L AND M
    digitalWrite(K, HIGH);
    digitalWrite(L, HIGH);
    digitalWrite(M, HIGH);
    digitalWrite(N, LOW);


    // IF VALUE READ FROM P IS 0, SWITCH PRESSED WILL BE 0 
    read_value=digitalRead(P);
    if(read_value==0)
    {
      // Display 0 in seven segment display, D3 D2 D1 D0 (0 0 0 0)
      digitalWrite(LE, LOW); // Displayed number will change if LE is LOW
      digitalWrite(D0, LOW);
      digitalWrite(D1, LOW);
      digitalWrite(D2, LOW);
      digitalWrite(D3, LOW);
      digitalWrite(LE, HIGH); // Displayed number will not change if LE is HIGH
      delay(500);
      goto label;
    }

  }


  else if(Q_value==0) //IF ANY SWITCH IN 3, 6, 9 and '#' IS PRESSED Q_value WILL BECOME 0
  {

    //  SEND LOW TO K AND HIGH TO L, M AND N
    digitalWrite(K, LOW);
    digitalWrite(L, HIGH);
    digitalWrite(M, HIGH);
    digitalWrite(N, HIGH);

    // IF VALUE READ FROM Q IS 0, SWITCH PRESSED WILL BE 3  
    read_value=digitalRead(Q);
    if(read_value==0)
    {
      // Display 3 in seven segment display, D3 D2 D1 D0 (0 0 1 1)
      digitalWrite(LE, LOW); // Displayed number will change if LE is LOW
      digitalWrite(D0, HIGH);
      digitalWrite(D1, HIGH);
      digitalWrite(D2, LOW);
      digitalWrite(D3, LOW);
      digitalWrite(LE, HIGH); // Displayed number will not change if LE is HIGH
      delay(500);
      goto label;
    }

    //  SEND LOW TO L AND HIGH TO K, M AND N
    digitalWrite(K, HIGH);
    digitalWrite(L, LOW);
    digitalWrite(M, HIGH);
    digitalWrite(N, HIGH);

    // IF VALUE READ FROM Q IS 0, SWITCH PRESSED WILL BE 6
    read_value=digitalRead(Q);
    if(read_value==0)
    {
      // Display 6 in seven segment display, D3 D2 D1 D0 (0 1 1 0)
      digitalWrite(LE, LOW); // Displayed number will change if LE is LOW
      digitalWrite(D0, LOW);
      digitalWrite(D1, HIGH);
      digitalWrite(D2, HIGH);
      digitalWrite(D3, LOW);
      digitalWrite(LE, HIGH); // Displayed number will not change if LE is HIGH
      delay(500);
      goto label;
    }

    //  SEND LOW TO M AND HIGH TO K, L AND N
    digitalWrite(K, HIGH);
    digitalWrite(L, HIGH);
    digitalWrite(M, LOW);
    digitalWrite(N, HIGH);

    // IF VALUE READ FROM Q IS 0, SWITCH PRESSED WILL BE 9
    read_value=digitalRead(Q);
    if(read_value==0)
    {
      // Display 9 in seven segment display, D3 D2 D1 D0 (1 0 0 1)
      digitalWrite(LE, LOW); // Displayed number will change if LE is LOW
      digitalWrite(D0, HIGH);
      digitalWrite(D1, LOW);
      digitalWrite(D2, LOW);
      digitalWrite(D3, HIGH);
      digitalWrite(LE, HIGH); // Displayed number will not change if LE is HIGH
      delay(500);
      goto label;
    }

    //  SEND LOW TO N AND HIGH TO K, L AND M
    digitalWrite(K, HIGH);
    digitalWrite(L, HIGH);
    digitalWrite(M, HIGH);
    digitalWrite(N, LOW);

    // IF VALUE READ FROM Q IS 0, SWITCH PRESSED WILL BE #
    read_value=digitalRead(Q);
    if(read_value==0)
    {
      goto label;
    }

  }



  else if(R_value==0) //IF ANY SWITCH IN A, B, C and D IS PRESSED R_value WILL BECOME 0
  {
    //  SEND LOW TO K AND HIGH TO L, M AND N
    digitalWrite(K, LOW);
    digitalWrite(L, HIGH);
    digitalWrite(M, HIGH);
    digitalWrite(N, HIGH);

    // IF VALUE READ FROM R IS 0, SWITCH PRESSED WILL BE 'A' 
    read_value=digitalRead(R);
    if(read_value==0)
    {
       goto label;
    }

    //  SEND LOW TO L AND HIGH TO K, M AND N
    digitalWrite(K, HIGH);
    digitalWrite(L, LOW);
    digitalWrite(M, HIGH);
    digitalWrite(N, HIGH);

    // IF VALUE READ FROM R IS 0, SWITCH PRESSED WILL BE 'B' 
    read_value=digitalRead(R);
    if(read_value==0)
    {
       goto label;
    }

    //  SEND LOW TO M AND HIGH TO K, L AND N
    digitalWrite(K, HIGH);
    digitalWrite(L, HIGH);
    digitalWrite(M, LOW);
    digitalWrite(N, HIGH);

    // IF VALUE READ FROM R IS 0, SWITCH PRESSED WILL BE 'C' 
    read_value=digitalRead(R);
    if(read_value==0)
    {
      goto label;
    }

    //  SEND LOW TO N AND HIGH TO K, L AND M
    digitalWrite(K, HIGH);
    digitalWrite(L, HIGH);
    digitalWrite(M, HIGH);
    digitalWrite(N, LOW);

    // IF VALUE READ FROM R IS 0, SWITCH PRESSED WILL BE 'D'
    read_value=digitalRead(R);
    if(read_value==0)
    {
      goto label;
    }
  }
}



 Click here to get the program as a file.

If 0 is pressed, arduino will send 0 to LE. 0 0 0 0 to D3 D2 D1 D0 respectively. Then send 1 to LE.
If 1 is pressed, arduino will send 0 to LE. 0 0 0 1 to D3 D2 D1 D0 respectively. Then send 1 to LE.
If 2 is pressed, arduino will send 0 to LE. 0 0 1 0 to D3 D2 D1 D0 respectively. Then send 1 to LE.
If 3 is pressed, arduino will send 0 to LE. 0 0 1 1 to D3 D2 D1 D0 respectively. Then send 1 to LE.
If 4 is pressed, arduino will send 0 to LE. 0 1 0 0 to D3 D2 D1 D0 respectively. Then send 1 to LE.
If 5 is pressed, arduino will send 0 to LE. 0 1 0 1 to D3 D2 D1 D0 respectively. Then send 1 to LE.
If 6 is pressed, arduino will send 0 to LE. 0 1 1 0 to D3 D2 D1 D0 respectively. Then send 1 to LE.
If 7 is pressed, arduino will send 0 to LE. 0 1 1 1 to D3 D2 D1 D0 respectively. Then send 1 to LE.
If 8 is pressed, arduino will send 0 to LE. 1 0 0 0 to D3 D2 D1 D0 respectively. Then send 1 to LE.
If 9 is pressed, arduino will send 0 to LE. 1 0 0 1 to D3 D2 D1 D0 respectively. Then send 1 to LE.

Output is clear from the animated image given on top of this page.

Previous: Working of Circuit

0 comments:

IMPORTANT NOTICE

All the circuits, published in this blog is only after testing and getting proper results in my private lab. When you try these circuits, you should check the supply voltage, polarity of components, presence of childrens nearby and shorts in the circuits. This website will not be responsible for any harm happened to you or your components caused by your carelessness.

For More Electronic Tips

Blog Archive

Proudly Powered by Blogger.