Hello all,
I stumbled into an issue when connecting my LCD screen to my Arduino Mega 2560. When activated my lcd will only show white boxes unless held at an angle. Is this a wiring error or a coding error? and how can I fix this?
Thanks
#include <LiquidCrystal.h>
#include <Keypad.h> //libraries
static int count = 0; //variable used as first number
unsigned long lastTime = millis();
unsigned long wait = 10000;
LiquidCrystal lcd(24,26,28,30,32,34);
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,75);
pinMode (52,OUTPUT);
pinMode(51, OUTPUT);
Serial.begin(9600);
pinMode(13,INPUT);
lcd.begin(16, 2);
lcd.print("Fab Cell #");
lcd.setCursor(0,1);
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(9,0);
lcd.print(memory);
delay(3000);
lcd.setCursor(7,0);
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);
}
}