75cd61163aa2886540ec8bbd940761b95c86676c
[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   Serial.begin(115200);
31   Serial.println("Keypad testing over serial");
32 }
33
34 void loop() {
35
36         adc_key_in = analogRead(0);    // read the value from the sensor  
37   digitalWrite(13, HIGH);  
38   key = get_key(adc_key_in);                    // convert into key press
39         
40         if (key != oldkey)                                  // if keypress is detected
41         {
42     delay(50);          // wait for debounce time
43                 adc_key_in = analogRead(0);    // read the value from the sensor  
44     key = get_key(adc_key_in);                  // convert into key press
45     if (key != oldkey)                          
46     {                   
47       oldkey = key;
48       if (key >=0){
49       lcd.cursorTo(2, 0);  //line=2, x=0
50                         lcd.printIn(msgs[key]);
51         Serial.print("adc=");
52         Serial.print(adc_key_in);
53         Serial.print(" key=");
54         Serial.println(key);
55       }
56     }
57   }
58   
59   //delay(1000);
60   digitalWrite(13, LOW);
61   
62
63  
64   
65   
66 }
67
68 // Convert ADC value to key number
69 int get_key(unsigned int input)
70 {
71         int k;
72     
73         for (k = 0; k < NUM_KEYS; k++)
74         {
75                 if (input < adc_key_val[k])
76                 {
77            
78     return k;
79         }
80         }
81     
82     if (k >= NUM_KEYS)
83         k = -1;     // No valid key pressed
84     
85     return k;
86 }