Buttons on Nokia 5110 LCD doesn't works

Hi everyone, I’m having a trouble with my code. I have “designed” (all by myself) a sliding menu wich shows the title of the section menu where we are and four selectionables items. The problem is, I’m trying to write the code for the pushbuttons. I’m using only Adafruit_GFX.h and Adafruit_PCD8544.h libraries. I know that the code it’s ripetitive but it is the only way I can understand it (I’m not an engineer :slight_smile: ). I will attach the schematic for the pushbuttons and the code. If is there anyone that could help me will be a pleasure.

Thanks in advance and if you have any question I’m here!

P.S. I have loaded the code on arduino and the main page it’s showed, so I think it works.

First part of code:

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

//define buttons
//#define BTN_LEFT 10
//#define BTN_SELECT 11
//#define BTN_RIGHT 12

//define pages
#define pg_home 1
#define pg_sensori 2
#define pg_sensori_temperatura 3
#define pg_sensori_temperatura_ambiente 4
#define pg_sensori_temperatura_acqua 5
#define pg_sensori_sensoreph 6
#define pg_relays 7
#define pg_relays_one 8
#define pg_relays_two 9
#define pg_relays_three 10
#define pg_relays_four 11
#define pg_pompeph 12
#define pg_pompeph_up 13
#define pg_pompeph_down 14
#define pg_display 15
#define pg_display_contrasto 16
#define pg_display_reset 17

//define menu items
#define home_item_sensori 1
#define home_item_relays 2
#define home_item_pompeph 3
#define home_item_display 4

#define sensori_item_temperatura 1
#define sensori_item_temperatura_ambiente 2
#define sensori_item_temperatura_acqua 3
#define sensori_item_sensoreph 4

#define relays_item_r_one 1
#define relays_item_r_two 2
#define relays_item_r_three 3
#define relays_item_r_four 4

#define pompeph_item_ph_up 1
#define pompeph_item_ph_down 2

#define display_item_contrasto 1
#define display_item_backlight 2
#define display_item_reset 3

//define default contrast
int contrast=57;

//backlight ON
boolean backlight = true;

//buttons
volatile boolean up = false;
volatile boolean down = false;
volatile boolean select = false;

int downButtonState = 0;
int upButtonState = 0;  
int selectButtonState = 0;          
int lastDownButtonState = 0;
int lastSelectButtonState = 0;
int lastUpButtonState = 0;

// Variables declared as volatile to ensure they
// wont be optomised out of the code
volatile int homeItem = 1;
volatile int sensoriItem = 1;
volatile int relaysItem = 1;
volatile int pompephItem = 1;
volatile int displayItem = 1;
volatile int page = 1;

Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);

void setup() {

  pinMode(10, INPUT_PULLUP); //button UP
  pinMode(11, INPUT_PULLUP); //button SELECT
  pinMode(12, INPUT_PULLUP); //button DOWN

  digitalWrite(7,LOW); //Turn Backlight ON

  Serial.begin(9600);
  
  display.begin();      
  display.setContrast(contrast); //Set contrast to 57 in this case
  display.clearDisplay(); 
  display.display();   

}

void loop() {

  downButtonState = digitalRead(12);
  selectButtonState = digitalRead(11);
  upButtonState =   digitalRead(10);
 
  checkIfDownButtonIsPressed();
  checkIfUpButtonIsPressed();
  checkIfSelectButtonIsPressed();

  switch(page) {
    case pg_home:
      drawHome();
    break;
    case pg_sensori:
      drawSensori();
    break;
    case pg_sensori_temperatura:
      drawSensoriTemperatura();
    break;
    case pg_sensori_temperatura_ambiente:
      drawSensoriTemperaturaAmbiente();
    break;
    case pg_sensori_temperatura_acqua:
      drawSensoriTemperaturaAcqua();
    break;
    case pg_sensori_sensoreph:
      drawSensoriSensorePh();
    break;
    case pg_relays:
      drawRelays();
    break;
    case pg_relays_one:
      drawRelaysOne();
    break;
    case pg_relays_two:
      drawRelaysTwo();
    break;
    case pg_relays_three:
      drawRelaysThree();
    break;
    case pg_relays_four:
      drawRelaysFour();
    break;
    case pg_pompeph:
      drawPompePh();
    break;
    case pg_pompeph_up:
      drawPompePhUp();
    break;
    case pg_pompeph_down:
      drawPompePhDown();
    break;
    case pg_display:
      drawDisplay();
    break;
    case pg_display_contrasto:
      drawDisplayContrasto();
    break;
    case pg_display_reset:
      resetDefaults();
    break;
  }
}

void drawHome() {
  
  display.setTextSize(1);
  display.clearDisplay();
  display.setTextColor(BLACK, WHITE);
  display.setCursor(15, 0);
  display.print("MAIN MENU");
  display.drawFastHLine(0,8,84,BLACK);
  display.setCursor(0, 10);

  if(homeItem == home_item_sensori) {
    display.setTextColor(WHITE, BLACK);
    } else {
      display.setTextColor(BLACK, WHITE);
    }
  display.print(">SENSORI");
  display.setCursor(0, 20);

  if(homeItem == home_item_relays) {
    display.setTextColor(WHITE, BLACK);
    } else {
      display.setTextColor(BLACK, WHITE);
    }
  display.print(">RELAYS");
  display.setCursor(0, 30);

  if(homeItem == home_item_pompeph) {
    display.setTextColor(WHITE, BLACK);
    } else {
      display.setTextColor(BLACK, WHITE);
    }
  display.print(">POMPE PH");
  display.setCursor(0, 40);

  if(homeItem == home_item_display) {
    display.setTextColor(WHITE, BLACK);
    } else {
      display.setTextColor(BLACK, WHITE);
    }
  display.print(">DISPLAY");
  display.display();
}

void drawSensori() {
  
  display.setTextSize(1);
  display.clearDisplay();
  display.setTextColor(BLACK, WHITE);
  display.setCursor(15, 0);
  display.print("SENSORI");
  display.drawFastHLine(0,8,84,BLACK);
  display.setCursor(0, 18);

  if(sensoriItem == sensori_item_temperatura) {
    display.setTextColor(WHITE, BLACK);
    } else {
      display.setTextColor(BLACK, WHITE);
    }
  display.print(">SENSORI");
  display.setCursor(0, 28);

  if(sensoriItem == sensori_item_sensoreph) {
    display.setTextColor(WHITE, BLACK);
    } else {
      display.setTextColor(BLACK, WHITE);
    }
  display.print(">SENSORE PH");
  display.display();
}