Facebook
From Sweltering Tern, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 173
  1. /*
  2.   LiquidCrystal Library - Hello World
  3.  
  4.  Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
  5.  library works with all LCD displays that are compatible with the
  6.  Hitachi HD44780 driver. There are many of them out there, and you
  7.  can usually tell them by the 16-pin interface.
  8.  
  9.  This sketch prints "Hello World!" to the LCD
  10.  and shows the time.
  11.  
  12.   The circuit:
  13.  * LCD RS pin to digital pin 12
  14.  * LCD Enable pin to digital pin 11
  15.  * LCD D4 pin to digital pin 5
  16.  * LCD D5 pin to digital pin 4
  17.  * LCD D6 pin to digital pin 3
  18.  * LCD D7 pin to digital pin 2
  19.  * LCD R/W pin to ground
  20.  * LCD VSS pin to ground
  21.  * LCD VCC pin to 5V
  22.  * 10K resistor:
  23.  * ends to +5V and ground
  24.  * wiper to LCD VO pin (pin 3)
  25.  
  26.  Library originally added 18 Apr 2008
  27.  by David A. Mellis
  28.  library modified 5 Jul 2009
  29.  by Limor Fried (http://www.ladyada.net)
  30.  example added 9 Jul 2009
  31.  by Tom Igoe
  32.  modified 22 Nov 2010
  33.  by Tom Igoe
  34.  modified 7 Nov 2016
  35.  by Arturo Guadalupi
  36.  
  37.  This example code is in the public domain.
  38.  
  39.  http://www.arduino.cc/en/Tutorial/LiquidCrystalHelloWorld
  40.  
  41. */
  42. int lcd_key=0;
  43. int adc_key_in=0;
  44. #define btnRIGHT  0
  45. #define btnUP     1
  46. #define btnDOWN   2
  47. #define btnLEFT   3
  48. #define btnSELECT 4
  49. #define btnNONE   5
  50.  
  51.  
  52.  
  53. // include the library code:
  54. #include <LiquidCrystal.h>
  55.  
  56. // initialize the library by associating any needed LCD interface pin
  57. // with the arduino pin number it is connected to
  58. const int rs = 8, en = 9, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
  59. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  60. const int numCols=16;
  61. const int numRows=2;
  62.  
  63. void setup() {
  64.   // set up the LCD's number of columns and rows:
  65.   lcd.begin(numCols,numRows);
  66.   // Print a message to the LCD.
  67.   lcd.setCursor(0,0);
  68.  
  69. }
  70.  
  71. int x1=0;
  72. int y1=0;
  73.  
  74. int read_LCD_buttons(){               // read the buttons
  75.     adc_key_in = analogRead(0);       // read the value from the sensor
  76.  
  77.     // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
  78.     // we add approx 50 to those values and check to see if we are close
  79.     // We make this the 1st option for speed reasons since it will be the most likely result
  80.  
  81.     if (adc_key_in > 1000) return btnNONE;
  82.  
  83.     // For V1.1 us this threshold
  84.     if (adc_key_in < 50)   return btnRIGHT;  
  85.     if (adc_key_in < 250)  return btnUP;
  86.     if (adc_key_in < 450)  return btnDOWN;
  87.     if (adc_key_in < 650)  return btnLEFT;
  88.     if (adc_key_in < 850)  return btnSELECT;
  89.  
  90.  
  91.       return btnNONE;
  92.   }
  93.  
  94. void loop() {
  95.   // set the cursor to column 0, line 1
  96.   // (note: line 1 is the second row, since counting begins with 0):
  97.  
  98.  
  99. lcd_key=read_LCD_buttons();
  100.  
  101.   switch (lcd_key)               // depending on which button was pushed, we perform an action
  102. {
  103.    case btnRIGHT:
  104.      {
  105.      x1+=1;
  106.      lcd.clear();
  107.      lcd.setCursor(x1,y1);
  108.      lcd.print("*");
  109.      delay(500);
  110.      break;
  111.      }
  112.      
  113.    case btnLEFT:
  114.      {
  115.      x1-=1;
  116.      lcd.clear();
  117.      lcd.setCursor(x1,y1);
  118.      lcd.print("*");
  119.      delay(500);
  120.      break;
  121.      }
  122.      
  123.    case btnUP:
  124.      {
  125.       y1+=1;
  126.      lcd.clear();
  127.      lcd.setCursor(x1,y1);
  128.      lcd.print("*");
  129.      delay(500);
  130.      break;
  131.      }
  132.      
  133.    case btnDOWN:
  134.      {
  135.     y1-=1;
  136.      lcd.clear();
  137.      lcd.setCursor(x1,y1);
  138.      lcd.print("*");
  139.      delay(500);
  140.      break;
  141.      }
  142.      
  143.    case btnSELECT:
  144.      {
  145.      
  146.      break;
  147.      }
  148.      
  149.      case btnNONE:
  150.      {
  151.    
  152.     // lcd.print(DropCount);
  153.      break;
  154.      }
  155. }
  156.  
  157.  
  158.     }
  159.