Advertisement
Guest User

Untitled

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