Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- byte h=0,v=0; // variables used in for loops
- const int period=50; // A little delay to avoid errors.
- int kdelay=0; // Non Blocking Delay
- const byte rows=4; // Rows in Keypad
- const byte columns=4; // Columns in Keypad
- const byte Output[rows]={6,7,8,9}; //Row connceted to Arduino Pins
- const byte Input[columns]={2,3,4,5}; //Columns connected to Arduino Pins
- const int Led1=10;
- const int Led2=11;
- const int Led3=12;
- const int Led4=13;
- const int Led5=14;
- const int Led6=15;
- const int Led7=16;
- const int Led8=17;
- const int Led9=18;
- void setup()
- {
- for(byte i=0;i<rows;i++) //Loop to declare output pins.
- {
- pinMode(Output[i],OUTPUT);
- }
- for(byte s=0;s<columns;s++) // Loop to decalre Input pins, Initial Pulledup.
- {
- pinMode(Input[s],INPUT_PULLUP);
- }
- Serial.begin(9600); // Initializing Serail communication.
- }
- void loop()
- {
- if(millis()-kdelay>period) //used to make non-blocking delay
- {
- kdelay=millis(); //capture time from millis function
- switch (keypad()) //Switch to get which button is pressed.
- {
- case 0:
- Serial.println(1);
- digitalWrite(Led1, HIGH);
- delay(100);
- digitalWrite(Led1, LOW);
- break;
- case 1:
- Serial.println(2);
- digitalWrite(Led2, HIGH);
- delay(100);
- digitalWrite(Led2, LOW);
- break;
- case 2:
- Serial.println(3);
- digitalWrite(Led3, HIGH);
- delay(100);
- digitalWrite(Led3, LOW);
- break;
- case 3:
- Serial.println("A");
- digitalWrite(Led1, HIGH);
- digitalWrite(Led2, HIGH);
- digitalWrite(Led3, HIGH);
- break;
- case 4:
- Serial.println(4);
- digitalWrite(Led4, HIGH);
- delay(100);
- digitalWrite(Led4, LOW);
- break;
- case 5:
- Serial.println(5);
- digitalWrite(Led5, HIGH);
- delay(100);
- digitalWrite(Led5, LOW);
- break;
- case 6:
- Serial.println(6);
- digitalWrite(Led6, HIGH);
- delay(100);
- digitalWrite(Led6, LOW);
- break;
- case 7:
- Serial.println("B");
- digitalWrite(Led4, HIGH);
- digitalWrite(Led5, HIGH);
- digitalWrite(Led6, HIGH);
- break;
- case 8:
- Serial.println(7);
- digitalWrite(Led7, HIGH);
- delay(100);
- digitalWrite(Led7, LOW);
- break;
- case 9:
- Serial.println(8);
- digitalWrite(Led8, HIGH);
- delay(100);
- digitalWrite(Led8, LOW);
- break;
- case 10:
- Serial.println(9);
- digitalWrite(Led9, HIGH);
- delay(100);
- digitalWrite(Led9, LOW);
- break;
- case 11:
- Serial.println("C");
- digitalWrite(Led7, HIGH);
- digitalWrite(Led8, HIGH);
- digitalWrite(Led9, HIGH);
- break;
- case 12:
- Serial.println("*");
- digitalWrite(Led1, HIGH);
- digitalWrite(Led4, HIGH);
- digitalWrite(Led7, HIGH);
- break;
- case 13:
- Serial.println(0);
- digitalWrite(Led2, HIGH);
- digitalWrite(Led5, HIGH);
- digitalWrite(Led8, HIGH);
- break;
- case 14:
- Serial.println("#");
- break;
- case 15:
- Serial.println("D");
- digitalWrite(Led1, LOW);
- digitalWrite(Led2, LOW);
- digitalWrite(Led3, LOW);
- digitalWrite(Led4, LOW);
- digitalWrite(Led5, LOW);
- digitalWrite(Led6, LOW);
- digitalWrite(Led7, LOW);
- digitalWrite(Led8, LOW);
- digitalWrite(Led9, LOW);
- break;
- default:
- ;
- }
- }
- }
- // Below function is used to detect which button is pressed.
- byte keypad()
- {
- static bool no_press_flag=0;
- for(byte x=0;x<columns;x++)
- {
- if (digitalRead(Input[x])==HIGH);
- else
- break;
- if(x==(columns-1))
- {
- no_press_flag=1;
- h=0;
- v=0;
- }
- }
- if(no_press_flag==1)
- {
- for(byte r=0;r<rows;r++)
- digitalWrite(Output[r],LOW);
- for(h=0;h<columns;h++)
- {
- if(digitalRead(Input[h])==HIGH)
- continue;
- else
- {
- for (v=0;v<rows;v++)
- {
- digitalWrite(Output[v],HIGH);
- if(digitalRead(Input[h])==HIGH)
- {
- no_press_flag=0;
- for(byte w=0;w<rows;w++)
- digitalWrite(Output[w],LOW);
- return v*4+h;
- }
- }
- }
- }
- }
- return 50;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement