clean serial output
[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] ={1, 520, 690, 770, 820 }; // max  
16 int NUM_KEYS = 5;
17 int adc_key_in;
18 int old_adc = 0;
19 int key=-1;
20 int oldkey=-1;
21
22 void setup() { 
23   pinMode(13, OUTPUT);  //we'll use the debug LED to output a heartbeat
24
25   lcd.init();
26   //optionally, now set up our application-specific display settings, overriding whatever the lcd did in lcd.init()
27   //lcd.commandWrite(0x0F);//cursor on, display on, blink on.  (nasty!)
28    lcd.clear();
29   lcd.printIn("KEYPAD testing... pressing");
30
31   Serial.begin(115200);
32   Serial.println("Keypad testing over serial");
33 }
34
35 void loop() {
36
37         adc_key_in = analogRead(0);    // read the value from the sensor  
38
39   if (old_adc != adc_key_in)
40     Serial.println(old_adc = adc_key_in);
41
42   digitalWrite(13, HIGH);  
43   key = get_key(adc_key_in);                    // convert into key press
44         
45         if (key != oldkey)                                  // if keypress is detected
46         {
47     delay(50);          // wait for debounce time
48                 adc_key_in = analogRead(0);    // read the value from the sensor  
49     key = get_key(adc_key_in);                  // convert into key press
50     if (key != oldkey)                          
51     {                   
52       oldkey = key;
53       if (key >=0){
54       lcd.cursorTo(2, 0);  //line=2, x=0
55                         lcd.printIn(msgs[key]);
56         Serial.print("adc=");
57         Serial.print(adc_key_in);
58         Serial.print(" key=");
59         Serial.println(key);
60       }
61     }
62   }
63   
64   //delay(1000);
65   digitalWrite(13, LOW);
66   
67
68  
69   
70   
71 }
72
73 // Convert ADC value to key number
74 int get_key(unsigned int input)
75 {
76         int k;
77     
78         for (k = 0; k < NUM_KEYS; k++)
79         {
80                 if (input < adc_key_val[k])
81                 {
82            
83     return k;
84         }
85         }
86     
87     if (k >= NUM_KEYS)
88         k = -1;     // No valid key pressed
89     
90     return k;
91 }