2 //example use of LCD4Bit_mod library
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);
10 char msgs[5][15] = {"Right Key OK ",
15 int adc_key_val[5] ={1, 520, 690, 770, 820 }; // max
23 pinMode(13, OUTPUT); //we'll use the debug LED to output a heartbeat
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!)
29 lcd.printIn("KEYPAD testing... pressing");
32 Serial.println("Keypad testing over serial");
37 adc_key_in = analogRead(0); // read the value from the sensor
39 if (old_adc != adc_key_in)
40 Serial.println(old_adc = adc_key_in);
42 digitalWrite(13, HIGH);
43 key = get_key(adc_key_in); // convert into key press
45 if (key != oldkey) // if keypress is detected
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
54 lcd.cursorTo(2, 0); //line=2, x=0
55 lcd.printIn(msgs[key]);
57 Serial.print(adc_key_in);
58 Serial.print(" key=");
65 digitalWrite(13, LOW);
73 // Convert ADC value to key number
74 int get_key(unsigned int input)
78 for (k = 0; k < NUM_KEYS; k++)
80 if (input < adc_key_val[k])
88 k = -1; // No valid key pressed