Advertisement
noam76

read from keypad

May 5th, 2015
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. // read keypad from 1 analog pin ands do something when specific key is pressed
  2. // I tried to re-read inside case, but it's not working
  3.  
  4. //add this for working
  5. // I add new readkeypad1() function with timeout for re-read from keypad
  6. // after reading the key I compare if not equal to KEY_NOT_PRESSED
  7. // and store the key
  8.  
  9.  
  10. #include <AnalogMatrixKeypad.h>
  11. #define analogPin 0
  12. AnalogMatrixKeypad AnMatrixKeypad(analogPin);
  13.  
  14. #include <Wire.h>
  15.  
  16. char Key,Key1,value;
  17. const int timeout = 1500;
  18. long timer;
  19. int x;
  20.  
  21. void readkeypad()
  22. {
  23. Key = AnMatrixKeypad.readKey();
  24. if(Key != KEY_NOT_PRESSED)
  25. Serial.println(Key); // send pressed key to serial monitor
  26. }
  27.  
  28. // ------ ------ ------ ------ code I add ------ ------ ------ ------
  29. void readkeypad1() //
  30. { //
  31. if(millis() - timer <= timeout) //
  32. { //
  33. Key1 = AnMatrixKeypad.readKey(); //
  34. if(Key1 != KEY_NOT_PRESSED){} //
  35. //Serial.println(Key1); //
  36. //if(Key1 == KEY_NOT_PRESSED) //
  37. //Serial.println(Key1); // send pressed key to serial monitor //
  38. else readkeypad1(); //
  39. } //
  40. } //
  41. // ------ ------ ------ ------ ------ ------ ------ ------ ------ ------
  42.  
  43. void checkeypad()
  44. {
  45. switch (Key)
  46. {
  47. case 'A':
  48. Serial.println(Key);
  49. //Serial.println("Enter new value from keypad :");
  50. // readkeypad(); // read new value from keypad, not working
  51. //value=Key;
  52. //Serial.println("target :");
  53. //Serial.println(value); // print on the serial monitor this char '-'
  54. break;
  55. case 'B':
  56. Serial.println("Elapsed Time is: ");
  57. Serial.println(millis());
  58. break;
  59.  
  60. // ------ ------ ------ ------ code I add ------ ------ ------ ------
  61. case '1': //
  62. if(millis() - timer1 <= timeout) //
  63. { //
  64. Serial.println("Enter new temp target :"); //
  65. timer2 = millis(); //
  66. readkeypad1(); //
  67. if(Key1 != KEY_NOT_PRESSED) //
  68. {x=Key1-'0';} // convert char to int, if you need //
  69. Serial.println("target :"); //
  70. Serial.println(desirabletemp); //
  71. } //
  72. break; //
  73. } //
  74. // ------ ------ ------ ------ ------ ------ ------ ------ ------ ------
  75. }
  76.  
  77. void setup()
  78. {
  79. Wire.begin(); // join i2c bus (address optional for master)
  80. Serial.begin(9600);
  81. }
  82.  
  83. void loop()
  84. {
  85. readkeypad(); // read from keypad
  86. checkeypad(); // check for key pressed
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement