I am having trouble getting my lcd screen to work. Right now I want to have both a 4x4 keypad and an lcd screen plugged into my arduino mega 2560 but only my keypad works. Here is my wiring and code for this project. On the display I only see white boxes on my lcd screen. Many tutorials I have seen mentioned using the first thirteen pins only for the lcd, but I cannot do that with the keypad as well. Is there a way to wire the lcd just using the side pins? Or is this just a problem with my code?
for lcd
VSS = GND
VDD = 5v
VO = pin 22
RS = pin 24
RW = pin 26
E = pin 36
D4 = pin 28
D5 = pin 30
D6 = pin 32
D7 = pin 34
A =5v
K = GND
for keypad (going from left to right)
pin 2
pin 3
pin 4
pin 5
pin 6
pin 7
pin 8
pin 9
here is the code
#include <LiquidCrystal.h>
#include <Keypad.h> //libraries
static int count = 0; //variable used as first number
unsigned long lastTime = millis();
unsigned long wait = 10000;
int rs=24, en=36, d4=28, d5=30, d6=32, d7=34;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
const byte ROWS = 4;
const byte COLS = 4;
bool blinking = false;
int buttonState=0;
int lastButtonState=0;
int buttonPushCounter =0;
int memory;
void partLoc(){
lcd.setCursor(7,1);
}
void fabLoc(){
lcd.setCursor(11,0);
}
void clearScreen(){
lcd.print(" ");
}
void countUp(){
count++;
partLoc();
clearScreen();
partLoc();
lcd.print(count);
for (int i = 0; i <= 16; i++) {
blinking=false;
lcd.setCursor(i,1);
}
tone(52,2200);
delay(100);
noTone(52);
}
void countDown(){
if(count!=0){
count--;
partLoc();
clearScreen();
partLoc();
lcd.print(count);
partLoc();
lcd.print(count);
for (int i = 0; i <= 16; i++) {
blinking=false;
lcd.setCursor(i,1);
}
tone(52,2100);
delay(100);
noTone(52);
}
}
char hexaKeys [ROWS][COLS] ={
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup()
{
analogWrite(22,60);
pinMode (52,OUTPUT);
pinMode(51, OUTPUT);
Serial.begin(9600);
pinMode(13,INPUT);
lcd.begin(16, 2);
delay(500);
lcd.print("Fab Cell #");
lcd.setCursor(0,1);
delay(500);
lcd.print("Part #");
lcd.setCursor(11,0);
lcd.blink();
}
void loop(){
int sensor = digitalRead(53);
unsigned long timeNow = millis();
buttonState = digitalRead(53);
if (buttonState != lastButtonState) {
if (buttonState == 1) {
buttonPushCounter++;
} else {
//Serial.println("off");
if (timeNow - lastTime > wait){ // adds delay after the count up but allows for input during the delay. delay will not stop the rest of the program from running
countUp();
Serial.println(count);
lastTime=timeNow;
}
}
lastButtonState = buttonState;
}
char customKey = customKeypad.getKey();
if (blinking){
lcd.blink();
}
//Switchcase Statements
switch (customKey){
case 'C':
countUp();
Serial.println(count);
break;
case 'D':
countDown();
Serial.println(count);
break;
case 'B':
fabLoc();
clearScreen();
fabLoc();
blinking = true;
tone(52,2200);
delay(100);
noTone(52);
break;
}
// Allows button input from buttons 0 - 9
if ((customKey>='0') && (customKey <= '9')){
lcd.print(customKey);
for (int i = 0; i <= 16; i++) {
blinking=false;
lcd.setCursor(i,1);
}
tone(52,2000);
delay(100);
noTone(52);
}
//When A is pressed displays the most recently cleared number
if (customKey=='A'){
for (int i = 0; i <= 16; i++) {
blinking=false;
lcd.setCursor(i,0);
}
for (int i = 0; i <= 16; i++) {
blinking=false;
lcd.setCursor(i,1);
}
Serial.println(memory);
lcd.setCursor(14,1);
lcd.print(memory);
delay(3000);
lcd.setCursor(8,1);
lcd.print(" ");
}
//When # is pressed the count resets
if (customKey=='#'){
memory = count; //stores the cleared value
count=0;
Serial.println("reset");
Serial.println(count);
lcd.setCursor(6,1);
lcd.print(" ");
partLoc();
lcd.print(count);
for (int i = 0; i <= 16; i++) {
blinking=false;
lcd.setCursor(i,1);
}
tone(52,1200);
delay(100);
noTone(52);
}
}