Advertisement
sunu

DFROBOT lcd sketch

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