blank display

I am using a nokia 5110 display I created a new code but when I uploaded it the display did nothing it just stayed blank, when I uploaded an example or a older code for it it worked fine I can't find any errors with my code please help.

#include <DS3231.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3); //in order pins are: CLK, DIN, DC, CE, RST
DS3231 rtc(SDA, SCL);

const int backLight = 9;
const int buttonUp = 12;
const int buttonSelect = 11;
const int buttonDown = 10;

char Mon = MONDAY; //day of week variables 
char tue = TUESDAY;
char wed = WEDNESDAY;
char thur = THURSDAY;
char fri = FRIDAY;
char sat = SATURDAY;
char sun = SUNDAY;

int hr = 1; //time variables
int mn = 0;
int sc = 0;
int day = 1;
int month = 1;
int yr = 2017;

int upState; //menu variables
int selectState;
int downState;
int lastUpState;
int lastSelectState;
int lastDownState;
int item = 1;
int mover = 1;
int contrast = 50;
int selection = 0;
int light = 1;

void setup(){
  pinMode(backLight, OUTPUT);
  pinMode(buttonUp, INPUT);
  pinMode(buttonSelect, INPUT);
  pinMode(buttonDown, INPUT);
  digitalWrite(backLight, light);

  rtc.setDOW(Mon);
  rtc.setTime(hr, mn, sc);
  rtc.setDate(month, day, yr);

  display.begin();
  display.setContrast(contrast);
  display.display();
  display.clearDisplay();
}

void loop(){
  upState = digitalRead(buttonUp);
  selectState = digitalRead(buttonSelect);
  downState = digitalRead(buttonDown);

  checkIfselectButtonIsPressed();

  if(item > 4){item = 1;}
  if(item < 1){item = 4;}
  if(selection > 1){selection = 0;}

  if(0 == selection){
    checkIfupButtonIsPressed();
    checkIfdownButtonIsPressed();
    switch(item){
      case 1:
        display.clearDisplay();
        display.setTextSize(1);
        display.setTextColor(BLACK);
        display.setCursor(15, 0);
        display.print("MENU");
        display.drawFastHLine(0, 10, 83, BLACK);
        display.setCursor(0, 12);
        display.print("[Time]");
        display.setCursor(0, 22);
        display.print("change time");
        display.setCursor(0, 33);
        display.print("back light");
        display.display();
        break;

       case 2:
        display.clearDisplay();
        display.setTextSize(1);
        display.setTextColor(BLACK);
        display.setCursor(15, 0);
        display.print("MENU");
        display.drawFastHLine(0, 10, 83, BLACK);
        display.setCursor(0, 12);
        display.print("Time");
        display.setCursor(0, 22);
        display.print("[change time]");
        display.setCursor(0, 33);
        display.print("back light");
        display.display();
        break;

       case 3:
        display.clearDisplay();
        display.setTextSize(1);
        display.setTextColor(BLACK);
        display.setCursor(15, 0);
        display.print("MENU");
        display.drawFastHLine(0, 10, 83, BLACK);
        display.setCursor(0, 12);
        display.print("Time");
        display.setCursor(0, 22);
        display.print("change time");
        display.setCursor(0, 33);
        display.print("[back light]");
        display.display();
        break;

       case 4:
        display.clearDisplay();
        display.setTextSize(1);
        display.setTextColor(BLACK);
        display.setCursor(15, 0);
        display.print("MENU");
        display.drawFastHLine(0, 10, 83, BLACK);
        display.setCursor(0, 12);
        display.print("[contrast]");
        display.display();
        break;
    }
  }
    else{
      if(item == 1){
        printTime();
      }
      if(item == 2){
        display.clearDisplay();
        display.setTextSize(1);
        display.setTextColor(BLACK);
        display.setCursor(0, 0);
        display.print("time change page");
        display.display();
      }
      if(item == 3){
        lightPage();
      }
      if(item == 4){
        contrastPage();
      }
    }
  
}



void checkIfupButtonIsPressed(){
  if(upState != lastUpState){
    if(upState == HIGH){
      item++;
    }
  }
  lastUpState = upState;
}

void checkIfselectButtonIsPressed(){
  if(selectState != lastSelectState){
    if(selectState == HIGH){
      selection++;
    }
  }
}

void checkIfdownButtonIsPressed(){
  if(downState != lastDownState){
    if(downState == HIGH){
      item--;
    }
  }
}

void printTime(){
  display.clearDisplay();
  display.setTextColor(BLACK);
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.print(rtc.getTimeStr());
  display.setCursor(0, 20);
  display.print(rtc.getDateStr());
  display.setCursor(0, 30);
  display.print(rtc.getDOWStr());
  display.display();
}

void changeLight(){
  if(downState != lastDownState){
    if(downState == HIGH){
      light++;
    }
  }
  lastDownState = downState;
  if(light > 1){light = 0;}
}

void lightPage(){
  changeLight();
  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, 20);
  if(light == 1){
      digitalWrite(backLight, LOW);
      display.print("ON");
  } else{
      digitalWrite(backLight, HIGH);
      display.print("OFF");
  }
  display.display();
}

void contrastUp(){
  if(upState != lastUpState){
    if(upState == HIGH){
        contrast++;
    }
  }
  lastUpState = upState;
}

void contrastDown(){
  if(downState != lastDownState){
    if(downState == HIGH){
        contrast--;
    }
  }
  lastDownState = downState;
}

void contrastPage(){
  contrastUp();
  contrastDown();
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor(15, 0);
  display.print("contrast:");
  display.drawFastHLine(0, 10, 83, BLACK);
  display.setCursor(20, 20);
  display.setContrast(contrast);
  display.print(contrast);
  display.display();
}

Please post an example of code that worked

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();
}

Have you been able to solve this issue?

It would have been helpful to have posted more information as detailed in the forum guidelines, point 6.

The only thing that jumps out at me as a possibility, based on the minimal information supplied, is a pin conflict between your two libraries - Adafruit_PCD8544 and DS3231. I you were using a Leonardo or Micro then you would probably clash over the display reset pin 3 which is also the SCL pin.

I would suggest commenting out all the RTC stuff and see if that helps. If it does, then try changing pin 3 (or even the other pins if necessary) on the display to something else. If it doesn't, then try commenting out different parts of your code until you narrow the problem down.

As an aside, a few fairly obvious errors in your code:

  if (item > 4) {
    item = 1;
  }
  if (item < 1) {
    item = 4;
  }

  if (0 == selection) {
    checkIfupButtonIsPressed();
    checkIfdownButtonIsPressed();

checkIfupButtonIsPressed() and checkIfdownButtonIsPressed() modify item after your range check, so the value of item can go out of range for the following switch statement.

void checkIfselectButtonIsPressed() {
  if (selectState != lastSelectState) {
    if (selectState == HIGH) {
      selection++;
    }
  }
  // MISSING
}

void checkIfdownButtonIsPressed() {
  if (downState != lastDownState) {
    if (downState == HIGH) {
      item--;
    }
  }
  // MISSING
}

In both these cases you are missing the assignment of current state to last state.

A common way to restrict a value between boundaries is to use the modulo operator. For example, if you want to increase a value "test" to be in [0..3] you would do:test = test + 1 % 4If you want to decrease the value, you would do:test = test + 3 % 4To have a base value other than 0, simply add it. If you want the value to be between [1..4] then it would be:

test = (test + 1 % 4) + 1
test = (test + 3 % 4) + 1

Jacques

jbellavance:

test = test + 1 % 4

This is equivalent to

test = test + 1

because % has a higher precedence than +.

You want

test = (test + 1) % 4

etc.