Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. byte h=0,v=0; // variables used in for loops
  2. const int period=50; // A little delay to avoid errors.
  3. int kdelay=0; // Non Blocking Delay
  4. const byte rows=4; // Rows in Keypad
  5. const byte columns=4; // Columns in Keypad
  6. const byte Output[rows]={6,7,8,9}; //Row connceted to Arduino Pins
  7. const byte Input[columns]={2,3,4,5}; //Columns connected to Arduino Pins
  8. const int Led1=10;
  9. const int Led2=11;
  10. const int Led3=12;
  11. const int Led4=13;
  12. const int Led5=14;
  13. const int Led6=15;
  14. const int Led7=16;
  15. const int Led8=17;
  16. const int Led9=18;
  17.  
  18. void setup()
  19. {
  20. for(byte i=0;i<rows;i++) //Loop to declare output pins.
  21. {
  22. pinMode(Output[i],OUTPUT);
  23. }
  24. for(byte s=0;s<columns;s++) // Loop to decalre Input pins, Initial Pulledup.
  25. {
  26. pinMode(Input[s],INPUT_PULLUP);
  27. }
  28. Serial.begin(9600); // Initializing Serail communication.
  29. }
  30. void loop()
  31. {
  32. if(millis()-kdelay>period) //used to make non-blocking delay
  33. {
  34. kdelay=millis(); //capture time from millis function
  35. switch (keypad()) //Switch to get which button is pressed.
  36. {
  37. case 0:
  38. Serial.println(1);
  39. digitalWrite(Led1, HIGH);
  40. delay(100);
  41. digitalWrite(Led1, LOW);
  42. break;
  43. case 1:
  44. Serial.println(2);
  45. digitalWrite(Led2, HIGH);
  46. delay(100);
  47. digitalWrite(Led2, LOW);
  48. break;
  49. case 2:
  50. Serial.println(3);
  51. digitalWrite(Led3, HIGH);
  52. delay(100);
  53. digitalWrite(Led3, LOW);
  54. break;
  55. case 3:
  56. Serial.println("A");
  57. digitalWrite(Led1, HIGH);
  58. digitalWrite(Led2, HIGH);
  59. digitalWrite(Led3, HIGH);
  60. break;
  61. case 4:
  62. Serial.println(4);
  63. digitalWrite(Led4, HIGH);
  64. delay(100);
  65. digitalWrite(Led4, LOW);
  66. break;
  67. case 5:
  68. Serial.println(5);
  69. digitalWrite(Led5, HIGH);
  70. delay(100);
  71. digitalWrite(Led5, LOW);
  72. break;
  73. case 6:
  74. Serial.println(6);
  75. digitalWrite(Led6, HIGH);
  76. delay(100);
  77. digitalWrite(Led6, LOW);
  78. break;
  79. case 7:
  80. Serial.println("B");
  81. digitalWrite(Led4, HIGH);
  82. digitalWrite(Led5, HIGH);
  83. digitalWrite(Led6, HIGH);
  84. break;
  85. case 8:
  86. Serial.println(7);
  87. digitalWrite(Led7, HIGH);
  88. delay(100);
  89. digitalWrite(Led7, LOW);
  90. break;
  91. case 9:
  92. Serial.println(8);
  93. digitalWrite(Led8, HIGH);
  94. delay(100);
  95. digitalWrite(Led8, LOW);
  96. break;
  97. case 10:
  98. Serial.println(9);
  99. digitalWrite(Led9, HIGH);
  100. delay(100);
  101. digitalWrite(Led9, LOW);
  102. break;
  103. case 11:
  104. Serial.println("C");
  105. digitalWrite(Led7, HIGH);
  106. digitalWrite(Led8, HIGH);
  107. digitalWrite(Led9, HIGH);
  108. break;
  109. case 12:
  110. Serial.println("*");
  111. digitalWrite(Led1, HIGH);
  112. digitalWrite(Led4, HIGH);
  113. digitalWrite(Led7, HIGH);
  114.  
  115. break;
  116. case 13:
  117. Serial.println(0);
  118. digitalWrite(Led2, HIGH);
  119. digitalWrite(Led5, HIGH);
  120. digitalWrite(Led8, HIGH);
  121. break;
  122. case 14:
  123. Serial.println("#");
  124. break;
  125. case 15:
  126. Serial.println("D");
  127. digitalWrite(Led1, LOW);
  128. digitalWrite(Led2, LOW);
  129. digitalWrite(Led3, LOW);
  130. digitalWrite(Led4, LOW);
  131. digitalWrite(Led5, LOW);
  132. digitalWrite(Led6, LOW);
  133. digitalWrite(Led7, LOW);
  134. digitalWrite(Led8, LOW);
  135. digitalWrite(Led9, LOW);
  136. break;
  137. default:
  138. ;
  139. }
  140. }
  141. }
  142.  
  143. // Below function is used to detect which button is pressed.
  144.  
  145. byte keypad()
  146. {
  147. static bool no_press_flag=0;
  148. for(byte x=0;x<columns;x++)
  149. {
  150. if (digitalRead(Input[x])==HIGH);
  151. else
  152. break;
  153. if(x==(columns-1))
  154. {
  155. no_press_flag=1;
  156. h=0;
  157. v=0;
  158. }
  159. }
  160. if(no_press_flag==1)
  161. {
  162. for(byte r=0;r<rows;r++)
  163. digitalWrite(Output[r],LOW);
  164. for(h=0;h<columns;h++)
  165. {
  166. if(digitalRead(Input[h])==HIGH)
  167. continue;
  168. else
  169. {
  170. for (v=0;v<rows;v++)
  171. {
  172. digitalWrite(Output[v],HIGH);
  173. if(digitalRead(Input[h])==HIGH)
  174. {
  175. no_press_flag=0;
  176. for(byte w=0;w<rows;w++)
  177. digitalWrite(Output[w],LOW);
  178. return v*4+h;
  179. }
  180. }
  181. }
  182. }
  183. }
  184. return 50;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement