Hi, I wrote a piece of code using the serial monitor and keyboard input, now trying to modify it for keypad and lcd display. I'm using wire.h, LiquidCrystal.h, and keypad.h libraries all downloaded from here and all working with the provided examples.
/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // i2c address is 0x27 20 characters 4 line display
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','4','7','*'},
{'2','5','8','0'},
{'3','6','9','#'},
{'A','B','C','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {13, 12, 11, 10}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup(){
Serial.begin(9600);
lcd.init();
lcd.init();
lcd.backlight();
}
void loop(){
char customKey = customKeypad.getKey();
if (customKey){
lcd.print(customKey);
// Serial.println(customKey);
}
}
I've made slight modifications to the example provided, letting me output to the lcd display. This code works perfectly. Input keypad values are read and then printed properly to the display. I think this rules out hardware issues.
The new code I am trying to implement is not working, I've tried several approaches with no success. I'm trying to get user input for several values to drive a CNC. This code is only slightly different than the previous working example.
/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // i2c address is 0x27 20 characters 4 line display
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','4','7','*'},
{'2','5','8','0'},
{'3','6','9','#'},
{'A','B','C','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {13, 12, 11, 10}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup(){
Serial.begin(9600);
lcd.init();
lcd.init();
lcd.backlight();
}
void loop(){
lcd.home();
lcd.print("enter a key");
customKeypad.waitForKey();
char customKey = customKeypad.getKey();
lcd.print(customKey);
lcd.setCursor(0,1);
lcd.print("enter an axis");
char axis = customKeypad.getKey();
if (customKey){
lcd.print(customKey);
customKeypad.waitForKey();
}
}
This last code produces garbled output to the lcd. At first I thought faulty hardware (keypad or lcd display) then I go back to the first code (above) and everything works as it should.
A few obvious questions, things like where in the code should the customKeypad.waitForKey(); be, I've tried it before and after the char customKey = customKeypad.getKey(); line. Both seem the same.
I need to allow the user to input several values to adjust a CNC router spindle along x,y and z axis.