I am using a nokia 5110 display with adafruit gfx and PCD8544.h libraries i made a code for the display and the display remained blank. when I tried one of my older codes it worked perfectly fine I can’t find any mistakes in my new code please help. I will put both working and not working codes
code that does not work:
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
// Software SPI (slower updates, more flexible pin options):
// pin 7 - Serial clock out (SCLK)
// pin 6 - Serial data out (DIN)
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
int state1;
int state2;
int state3;
int lastState1;
int lastState2;
int lastState3;
int menuVariable = 1;
int selectedOption = 0;
void setup(){
pinMode(12, INPUT);
pinMode(11, INPUT);
pinMode(10, INPUT);
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
display.begin();
display.setContrast(50);
display.display();
display.clearDisplay();
}
void loop(){
state1 = digitalRead(12);
state2 = digitalRead(11);
state3 = digitalRead(10);
checkIfselectButtonIsPressed();
if(selectedOption > 1){selectedOption = 0;}
if(selectedOption == 0){
checkIfupButtonIsPressed();
checkIfdownButtonIsPressed();
if(menuVariable > 3){menuVariable = 1;}
if(menuVariable < 1){menuVariable = 3;}
switch(menuVariable){
case 1:
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(15, 0);
display.print("MAIN MENU");
display.drawFastHLine(0, 10, 83, BLACK);
display.setCursor(0, 14);
display.print("[option 1]");
display.setCursor(0, 24);
display.print("option 2");
display.setCursor(0, 34);
display.print("option 3");
display.display();
break;
case 2:
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(15, 0);
display.print("MAIN MENU");
display.drawFastHLine(0, 10, 83, BLACK);
display.setCursor(0, 14);
display.print("option 1");
display.setCursor(0, 24);
display.print("[option 2]");
display.setCursor(0, 34);
display.print("option 3");
display.display();
break;
case 3:
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(15, 0);
display.print("MAIN MENU");
display.drawFastHLine(0, 10, 83, BLACK);
display.setCursor(0, 14);
display.print("option 1");
display.setCursor(0, 24);
display.print("option 2");
display.setCursor(0, 34);
display.print("[option 3]");
display.display();
break;
} //close switch case
} //close if
}//close loop
void checkIfupButtonIsPressed(){
if(lastState1 != state1){
if(state1 == HIGH){
menuVariable++;
}
}
lastState1 = state1;
}
void checkIfselectButtonIsPressed(){
if(lastState2 != state2){
if(state1 == HIGH){
selectedOption++;
}
}
lastState2 = state2;
}
void checkIfdownButtonIsPressed(){
if(lastState3 != state3){
if(state1 == HIGH){
menuVariable--;
}
}
lastState3 = state3;
}
code that works:
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
const int backLight = 9;
const int buttonUp = 12;
const int buttonSelect = 11;
const int buttonDown = 10;
int upState;
int selectState;
int downState;
int lastUpState;
int lastSelectState;
int lastDownState;
int item;
int contrast = 50;
int selection = 0;
int light = 1;
void setup(){
display.begin();
display.setContrast(contrast);
display.display();
display.clearDisplay();
pinMode(backLight, OUTPUT);
pinMode(buttonUp, INPUT);
pinMode(buttonSelect, INPUT);
pinMode(buttonDown, INPUT);
digitalWrite(backLight, light);
}
void loop(){
upState = digitalRead(buttonUp);
selectState = digitalRead(buttonSelect);
downState = digitalRead(buttonDown);
checkIfbuttonSelectIsPressed();
if(item > 3){item = 1;}
if(item < 1){item = 3;}
if(selection > 1){selection = 0;}
if(0 == selection){
checkIfbuttonUpIsPressed();
checkIfbuttonDownIsPressed();
switch(item){
case 1:
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(15, 0);
display.print("MAIN MENU");
display.drawFastHLine(0, 10, 83, BLACK);
display.setCursor(0, 12);
display.print("[contrast]");
display.setCursor(0, 22);
display.print("light");
display.setCursor(0, 33);
display.print("reset");
display.display();
break;
case 2:
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(15, 0);
display.print("MAIN MENU");
display.drawFastHLine(0, 10, 83, BLACK);
display.setCursor(0, 12);
display.print("contrast");
display.setCursor(0, 22);
display.print("[light]");
display.setCursor(0, 33);
display.print("reset");
display.display();
break;
case 3:
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(15, 0);
display.print("MAIN MENU");
display.drawFastHLine(0, 10, 83, BLACK);
display.setCursor(0, 12);
display.print("contrast");
display.setCursor(0, 22);
display.print("light");
display.setCursor(0, 33);
display.print("[reset]");
display.display();
break;
}
} else{
if(item == 1){
contrastPage();
}
if(item == 2){
lightPage();
}
if(item == 3){
digitalWrite(backLight, LOW);
contrast = 50;
}
}
}
void checkIfbuttonUpIsPressed(){
if(upState != lastUpState){
if(upState == HIGH){
item++;
}
}
lastUpState = upState;
}
void checkIfbuttonSelectIsPressed(){
if(selectState != lastSelectState){
if(selectState == HIGH){
selection++;
}
}
lastSelectState = selectState;
}
void checkIfbuttonDownIsPressed(){
if(downState != lastDownState){
if(downState == HIGH){
item--;
}
}
lastDownState = downState;
}
void buttonContrastUp(){
if(upState != lastUpState){
if(upState == HIGH){
contrast++;
}
}
lastUpState = upState;
}
void buttonContrastDown(){
if(downState != lastDownState){
if(downState == HIGH){
contrast--;
}
}
lastDownState = downState;
}
void lightOff_OnButton(){
if(upState != lastUpState){
if(upState == HIGH){
light++;
}
}
lastUpState = upState;
if(light > 1){light = 0;}
}
void contrastPage(){
buttonContrastUp();
buttonContrastDown();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(15, 0);
display.drawFastHLine(0, 10, 83, BLACK);
display.setCursor(20, 0);
display.print("Contrast: ");
display.setCursor(20, 20);
display.print(contrast);
display.setContrast(contrast);
display.display();
}
void lightPage(){
lightOff_OnButton();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(15, 0);
display.print("back light");
display.drawFastHLine(0, 10, 83, BLACK);
display.setCursor(20, 0);
display.print("light ");
if(light == 1){
digitalWrite(backLight, LOW);
display.setCursor(20, 20);
display.print("ON");
}
if(light == 0){
digitalWrite(backLight, HIGH);
display.setCursor(20, 20);
display.print("OFF");
}
display.display();
}