The sketch that works without a problem:
//example use of LCD4Bit_mod library
#include <Botty_LCD4Bit_mod.h>
//create object to control an LCD.
//number of lines in display=1
LCD4Bit_mod lcd = LCD4Bit_mod(2);
//Key message
char msgs[5][15] = {"Right Key OK ",
"Up Key OK ",
"Down Key OK ",
"Left Key OK ",
"Select Key OK" };
int adc_key_val[5] = {30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int key = -1;
int oldkey = -1;
int adc_key_in;
void setup()
{
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(19, OUTPUT);
lcd.init();
//optionally, now set up our application-specific display settings, overriding whatever the lcd did in lcd.init()
//lcd.commandWrite(0x0F);//cursor on, display on, blink on. (nasty!)
lcd.clear();
lcd.printIn("KEYPAD testing");
}
void loop()
{
for(int i = 48; i < 58; i += 1)
{
lcd.cursorTo(2, 0);
lcd.print(i);
delay(5);
}
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, LOW); //mux pin 3
pinMode(19, INPUT); //set analog 5 to input
adc_key_in = analogRead(5); // read the value from the sensor
pinMode(19, OUTPUT);
key = get_key(adc_key_in); // convert into key press
// convert into key press
if(key != oldkey)
{
oldkey = key;
if(key >= 0)
{
lcd.cursorTo(2, 2); //line=2, x=0
lcd.printIn(msgs[key]);
}
}
}
// Convert ADC value to key number
int get_key(unsigned int input)
{
int k;
for(k = 0; k < NUM_KEYS; k++)
{
if(input < adc_key_val[k])
{
return k;
}
}
if(k >= NUM_KEYS)
{
k = -1; // No valid key pressed
}
return k;
}
The sketch that will produce garbage on the screen as soon as a button is pressed:
//example use of LCD4Bit_mod library
#include <Botty_LCD4Bit_mod.h>
//create object to control an LCD.
//number of lines in display=1
LCD4Bit_mod lcd = LCD4Bit_mod(2);
//Key message
char msgs[5][15] = {"Right Key OK ",
"Up Key OK ",
"Down Key OK ",
"Left Key OK ",
"Select Key OK" };
int adc_key_val[5] = {30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int key = -1;
int oldkey = -1;
int adc_key_in;
void setup()
{
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(19, OUTPUT);
lcd.init();
//optionally, now set up our application-specific display settings, overriding whatever the lcd did in lcd.init()
//lcd.commandWrite(0x0F);//cursor on, display on, blink on. (nasty!)
lcd.clear();
lcd.printIn("KEYPAD testing");
}
void loop()
{
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, LOW); //mux pin 3
pinMode(19, INPUT);
adc_key_in = analogRead(5); // read the value from the sensor
pinMode(19, OUTPUT);
key = get_key(adc_key_in); // convert into key press
if(key != oldkey) // if keypress is detected
{
delay(50); // wait for debounce time
pinMode(19, INPUT);
adc_key_in = analogRead(5); // read the value from the sensor
pinMode(19, OUTPUT);
key = get_key(adc_key_in); // convert into key press
if(key != oldkey)
{
oldkey = key;
if(key >=0)
{
lcd.cursorTo(2, 0); //line=2, x=0
lcd.printIn(msgs[key]);
}
}
}
}
// Convert ADC value to key number
int get_key(unsigned int input)
{
int k;
for(k = 0; k < NUM_KEYS; k++)
{
if(input < adc_key_val[k])
{
return k;
}
}
if(k >= NUM_KEYS)
{
k = -1; // No valid key pressed
}
return k;
}
Some notes with the second, problem causing, sketch.
The library I use will set the multiplexer to the correct pin to push data into the LCD, the library between the two sketches is the same.
I have also tried the second sketch with a delay after each pinMode change, with a delay of upto 10 milliseconds for each switch (so both after pinMode(19, INPUT) and pinMode(19, OUTPUT) ).