Advertisement
Guest User

Untitled

a guest
Dec 26th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. /*************************************************************************************
  2.  
  3. Mark Bramwell, July 2010
  4.  
  5. This program will test the LCD panel and the buttons.When you push the button on the shield,
  6. the screen will show the corresponding one.
  7.  
  8. Connection: Plug the LCD Keypad to the UNO(or other controllers)
  9.  
  10. **************************************************************************************/
  11.  
  12. #include <LiquidCrystal.h>
  13.  
  14. LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // select the pins used on the LCD panel
  15.  
  16. // define some values used by the panel and buttons
  17. int lcd_key = 0;
  18. int adc_key_in = 0;
  19.  
  20. #define btnRIGHT 0
  21. #define btnUP 1
  22. #define btnDOWN 2
  23. #define btnLEFT 3
  24. #define btnSELECT 4
  25. #define btnNONE 5
  26.  
  27. int read_LCD_buttons(){ // read the buttons
  28. adc_key_in = analogRead(0); // read the value from the sensor
  29.  
  30. // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
  31. // we add approx 50 to those values and check to see if we are close
  32. // We make this the 1st option for speed reasons since it will be the most likely result
  33.  
  34. if (adc_key_in > 1000) return btnNONE;
  35.  
  36. // For V1.1 us this threshold
  37. if (adc_key_in < 50) return btnRIGHT;
  38. if (adc_key_in < 250) return btnUP;
  39. if (adc_key_in < 450) return btnDOWN;
  40. if (adc_key_in < 650) return btnLEFT;
  41. if (adc_key_in < 850) return btnSELECT;
  42.  
  43. // For V1.0 comment the other threshold and use the one below:
  44. /*
  45. if (adc_key_in < 50) return btnRIGHT;
  46. if (adc_key_in < 195) return btnUP;
  47. if (adc_key_in < 380) return btnDOWN;
  48. if (adc_key_in < 555) return btnLEFT;
  49. if (adc_key_in < 790) return btnSELECT;
  50. */
  51.  
  52. return btnNONE; // when all others fail, return this.
  53. }
  54.  
  55. void setup(){
  56. lcd.begin(16, 2); // start the library
  57. lcd.setCursor(0,0); // set the LCD cursor position
  58. lcd.print("Push the buttons"); // print a simple message on the LCD
  59. }
  60.  
  61. void loop(){
  62. lcd.setCursor(9,1); // move cursor to second line "1" and 9 spaces over
  63. lcd.print(millis()/1000); // display seconds elapsed since power-up
  64.  
  65. lcd.setCursor(0,1); // move to the begining of the second line
  66. lcd_key = read_LCD_buttons(); // read the buttons
  67.  
  68. switch (lcd_key){ // depending on which button was pushed, we perform an action
  69.  
  70. case btnRIGHT:{ // push button "RIGHT" and show the word on the screen
  71. lcd.print("RIGHT ");
  72. break;
  73. }
  74. case btnLEFT:{
  75. lcd.print("LEFT "); // push button "LEFT" and show the word on the screen
  76. break;
  77. }
  78. case btnUP:{
  79. lcd.print("UP "); // push button "UP" and show the word on the screen
  80. break;
  81. }
  82. case btnDOWN:{
  83. lcd.print("DOWN "); // push button "DOWN" and show the word on the screen
  84. break;
  85. }
  86. case btnSELECT:{
  87. lcd.print("SELECT"); // push button "SELECT" and show the word on the screen
  88. break;
  89. }
  90. case btnNONE:{
  91. lcd.print("NONE "); // No action will show "None" on the screen
  92. break;
  93. }
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement