380e710b6e64d60ad772eda39284ae05f5b12c52
[Arduino] / LCD_Keypad_Shield / LCD_Keypad_Shield.ino
1
2 //example use of LCD4Bit_mod library
3
4 #include <LCD4Bit_mod.h> 
5 //create object to control an LCD.  
6 //number of lines in display=1
7 LCD4Bit_mod lcd = LCD4Bit_mod(2); 
8
9 //Key message
10 char msgs[5][15] = {"Right Key OK ", 
11                     "Up Key OK    ", 
12                     "Down Key OK  ", 
13                     "Left Key OK  ", 
14                     "Select Key OK" };
15 int  adc_key_val[5] ={30, 150, 360, 535, 760 };
16 int NUM_KEYS = 5;
17 int adc_key_in;
18 int key=-1;
19 int oldkey=-1;
20
21 void setup() { 
22   pinMode(13, OUTPUT);  //we'll use the debug LED to output a heartbeat
23
24   lcd.init();
25   //optionally, now set up our application-specific display settings, overriding whatever the lcd did in lcd.init()
26   //lcd.commandWrite(0x0F);//cursor on, display on, blink on.  (nasty!)
27    lcd.clear();
28   lcd.printIn("KEYPAD testing... pressing");
29 }
30
31 void loop() {
32
33         adc_key_in = analogRead(0);    // read the value from the sensor  
34   digitalWrite(13, HIGH);  
35   key = get_key(adc_key_in);                    // convert into key press
36         
37         if (key != oldkey)                                  // if keypress is detected
38         {
39     delay(50);          // wait for debounce time
40                 adc_key_in = analogRead(0);    // read the value from the sensor  
41     key = get_key(adc_key_in);                  // convert into key press
42     if (key != oldkey)                          
43     {                   
44       oldkey = key;
45       if (key >=0){
46       lcd.cursorTo(2, 0);  //line=2, x=0
47                         lcd.printIn(msgs[key]);
48       }
49     }
50   }
51   
52   //delay(1000);
53   digitalWrite(13, LOW);
54   
55
56  
57   
58   
59 }
60
61 // Convert ADC value to key number
62 int get_key(unsigned int input)
63 {
64         int k;
65     
66         for (k = 0; k < NUM_KEYS; k++)
67         {
68                 if (input < adc_key_val[k])
69                 {
70            
71     return k;
72         }
73         }
74     
75     if (k >= NUM_KEYS)
76         k = -1;     // No valid key pressed
77     
78     return k;
79 }\r