Screen shows nothing after a time on Nokia 5110 screen

hello,

I started a new project to make a clock and to show temprature and humidity also. I completed my code which is below and working withouth problem. but my problem is after a while screen shows nothing and not responding my button comments also.

I added my code and prictures of my project. I somebody could assist me to solve this problem and if you will have nay suggestion to make my code better, I'll appriciate.



#include "DHT.h"
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include "RTClib.h"

// Define the connections pins:
#define CLK 2
#define DIO 3

// Create rtc and display object:
RTC_DS3231 rtc;

// Declare LCD object for software SPI
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);

#define DHTPIN 2
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

int tresholdselect = 300;// to find first button
int tresholdNavUp = 400;//to find second button
int tresholdNavDown = 550;// to find third buton
 
int selection1 = 5000; // selection of time or date
int selection2 = 5000; // selection of hour or minute
int selection3 = 0; // selection of day or month or year
int selectionValue1 = 0; // first selection. 0 for time, 1 for date
int selectionValue2 = 0; // second selection. for time 0 for hour, 1 for minute. for date 0 for day, 1 for month, 2 for year
int controlbutton = 0; // to prevent to enter first selection case without pressing nvigate buttons
int controlbutton2 = 0; //  to prevent when press first selection to make second selectip
int controlbutton3 = 0;// to prevent when press second selection to make third selectip


// Animation variables
int scrollPosition = 0;
bool scrollRight = true;


void setup() {
  Serial.begin(9600);
  Serial.println("DHRxx test!");
  delay(3000);

  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, let's set the time!");
    rtc.adjust(DateTime(2024, 1, 10, 15, 28, 05));
  }

  dht.begin();
  display.begin();
  display.setContrast(57);
}


void amendHour(int valuebutton)
{
    //read the values from 
  DateTime now = rtc.now();
  int rtcHour = now.hour();
  int rtcMin = now.minute();
  int rtcYear=now.year();
  int rtcMonth=now.month();
  int rtcDay=now.day();
  switch(ButtonValue(valuebutton))
        {
          case 1:
        //selection
        rtc.adjust(DateTime(rtcYear, rtcMonth, rtcDay, selection3, rtcMin, 0 ));
        display.print(String(selection3)+ " Ayarlandı");
        delay(1000);
        selectionValue1 = 0;
        selectionValue2 = 0;
        selection3 = 0;
        controlbutton = 0;
        controlbutton2 = 0;
        controlbutton3 = 0;
        break;
          case 2:
        //navigate+
        selection3 = (selection3 % 23) + 1;
        display.print(selection3);
        break;
          case 3:
        //navigate-
        selection3 = (selection3>0 ) ? (selection3 % 23) - 1 : 23;
        display.print(selection3);
        break;
          default:
        display.print(selection3);
          break;
        }
}

void amendMinute(int valuebutton)
{
    //read the values from 
  DateTime now = rtc.now();
  int rtcHour = now.hour();
  int rtcMin = now.minute();
  int rtcYear=now.year();
  int rtcMonth=now.month();
  int rtcDay=now.day();
  switch(ButtonValue(valuebutton))
        {
          case 1:
        //selection
        rtc.adjust(DateTime(rtcYear, rtcMonth, rtcDay, rtcHour, selection3, 0 ));
        display.print(String(selection3) + " Ayarlandı"); 
        delay(1000);
        selectionValue1 = 0;
        selectionValue2 = 0;
        selection3 = 0;
        controlbutton = 0;
        controlbutton2 = 0;
        controlbutton3 = 0;
        break;
          case 2:
        //navigate+
        selection3 = (selection3 % 59) + 1;
        display.print(selection3);
        break;
          case 3:
        //navigate-
        selection3 = (selection3>0 ) ? (selection3 % 59) - 1 : 59;
        display.print(selection3);
        break;
          default:
        display.print(selection3);
          break;
        }
}

void amendDay(int valuebutton)
{
    //read the values from 
  DateTime now = rtc.now();
  int rtcHour = now.hour();
  int rtcMin = now.minute();
  int rtcYear=now.year();
  int rtcMonth=now.month();
  int rtcDay=now.day();
  switch(ButtonValue(valuebutton))
        {
          case 1:
        //selection
        rtc.adjust(DateTime(rtcYear, rtcMonth, selection3, rtcHour, rtcMin, 0 ));
        display.print(String(selection3) + " Ayarlandı");
        delay(1000);
        selectionValue1 = 0;
        selectionValue2 = 0;
        selection3 = 0;
        controlbutton = 0;
        controlbutton2 = 0;
        controlbutton3 = 0;
        break;
          case 2:
        //navigate+
        if(rtcMonth == 1 || rtcMonth == 3 || rtcMonth == 5 || rtcMonth == 7 || rtcMonth == 8 || rtcMonth == 10 || rtcMonth == 12)//31 day months
        {
          selection3 = (selection3 % 31) + 1;
          display.print(selection3);
        }
        else if(rtcMonth == 4 || rtcMonth == 6 || rtcMonth == 9 || rtcMonth == 11)//30 day months
        {
          selection3 = (selection3 % 30) + 1;
          display.print(selection3);
        }
        else if(rtcMonth == 2 && ((rtcYear % 4 == 0 && rtcYear % 100 != 0) || (rtcYear % 400 == 0)))//Leap year february
        {
          selection3 = (selection3 % 29) + 1;
          display.print(selection3);
        }
        else if(rtcMonth == 2 && (rtcYear % 4 != 0 || (rtcYear % 100 == 0 && rtcYear % 400 != 0)))//not leap year february
        {
          selection3 = (selection3 % 28) + 1;
          display.print(selection3);
        }
        break;
          case 3:
        //navigate-
        if(rtcMonth == 1 || rtcMonth == 3 || rtcMonth == 5 || rtcMonth == 7 || rtcMonth == 8 || rtcMonth == 10 || rtcMonth == 12)//31 day months
        {
          selection3 = (selection3>0 ) ? (selection3 % 31) - 1 : 31;
          display.print(selection3);
        }
        else if(rtcMonth == 4 || rtcMonth == 6 || rtcMonth == 9 || rtcMonth == 11)//30 day months
        {
          selection3 = (selection3>0 ) ? (selection3 % 30) - 1 : 30;
          display.print(selection3);
        }
        else if(rtcMonth == 2 && ((rtcYear % 4 == 0 && rtcYear % 100 != 0) || (rtcYear % 400 == 0)))//Leap year february
        {
          selection3 = (selection3>0 ) ? (selection3 % 29) - 1 : 29;
          display.print(selection3);
        }
        else if(rtcMonth == 2 && (rtcYear % 4 != 0 || (rtcYear % 100 == 0 && rtcYear % 400 != 0)))//not leap year february
        {
          selection3 = (selection3>0 ) ? (selection3 % 28) - 1 : 28;
          display.print(selection3);
        }
        break;
          default:
        display.print(selection3);
          break;
        }
}

void amendMonth(int valuebutton)
{
    //read the values from 
  DateTime now = rtc.now();
  int rtcHour = now.hour();
  int rtcMin = now.minute();
  int rtcYear=now.year();
  int rtcMonth=now.month();
  int rtcDay=now.day();
  switch(ButtonValue(valuebutton))
        {
          case 1:
        //selection
        rtc.adjust(DateTime(rtcYear, selection3, rtcDay, rtcHour, rtcMin, 0 ));
        display.print(String(selection3)+ " Ayarlandı");
        delay(1000);
        selectionValue1 = 0;
        selectionValue2 = 0;
        selection3 = 0;
        controlbutton = 0;
        controlbutton2 = 0;
        controlbutton3 = 0;
        break;
          case 2:
        //navigate+
        selection3 = (selection3 % 12) + 1;
        display.print(selection3);
        break;
          case 3:
        //navigate-
        selection3 = (selection3>0 ) ? (selection3 % 12) - 1 : 12;
        display.print(selection3);
        break;
          default:
        display.print(selection3);
          break;
        }
}

void amendYear(int valuebutton)
{
    //read the values from 
  DateTime now = rtc.now();
  int rtcHour = now.hour();
  int rtcMin = now.minute();
  int rtcYear=now.year();
  int rtcMonth=now.month();
  int rtcDay=now.day();
  switch(ButtonValue(valuebutton))
        {
          case 1:
        //selection
        rtc.adjust(DateTime(selection3, rtcMonth, rtcDay, rtcHour, rtcMin, 0 ));
        display.print(String(selection3)+ " Ayarlandı");
        delay(1000);
        selectionValue1 = 0;
        selectionValue2 = 0;
        selection3 = 0;
        controlbutton = 0;
        controlbutton2 = 0;
        controlbutton3 = 0;
        break;
          case 2:
        //navigate+
        selection3 = (selection3 % 99) + 1;
        display.print(selection3);
        break;
          case 3:
        //navigate-
        selection3 = (selection3>0 ) ? (selection3 % 99) - 1 : 99;
        display.print(selection3);
        break;
          default:
        display.print(selection3);
          break;
        }
}

void showselection(int value1, int value2)
{
  display.setCursor(0, 30);
  if (value2==0)// to find if the second selection is made or not
  {
    if (value1 % 2 == 0)//selection of time
    {
      display.print("Zaman Secildi");
      delay(1000);
    } else {//selection of date
      display.print("Tarih Secildi");
      delay(1000);
    }
  } 
  else if(value2>0 && value1 % 2 == 0)//if the second selection is made, which means first selection already done for Time
  {
    if(value2% 2 ==0)
    {
      display.print("Saat Secildi");
      delay(1000);
    } else{
      display.print("Dakika Secildi");
      delay(1000);
    }
  }
  else if(value2>0 && value1 % 2 == 1)//if the second selection is made, which means first selection already done for date
  if (value2% 3 ==0)
  {
    display.print("Gun Secildi");
    delay(1000);
  } else if (value2% 3 == 1)
  {
    display.print("Ay Secildi");
    delay(1000);
  } else if (value2% 3 == 2)
  {
    display.print("Yil Secildi");
    delay(1000);
  }
}


void displayselection1(int valueselect1, int valueselect2)//screen display for selection
{
  display.setCursor(0, 30);
  if(valueselect2==0)// if first selection has not been made
  {
    if (valueselect1 % 2 == 0)
    {
      display.print("Zaman Ayarla");
    } else if(valueselect1 % 2 == 1)
    {
      display.print("Tarih Ayarla");
    } else {display.print("Secim Hata");}
  } 
  else if(valueselect2 % 2 == 0) // if first selectionis made for time
  {
    if (valueselect1 % 2 == 0)
    {
      display.print("Saat Ayarla");
    } else if(valueselect1 % 2 == 1)
    {
      display.print("Dakika Ayarla");
    } else {display.print("Secim Hata");}
  }
  else if(valueselect2 % 2 == 1) // if first selectionis made for date
  {
    if (valueselect1 % 3 == 0)
    {
      display.print("Gun Ayarla");
    } else if(valueselect1 % 3 == 1)
    {
      display.print("Ay Ayarla");
    } else if(valueselect1 % 3 == 2)
    {
      display.print("Yil Ayarla");
    }else {display.print("Secim Hata");}
  }
}

int ButtonValue(int button)
{
  if (button<tresholdselect && button>100) // press first button for select the object
  {
    return 1;
  } else if (button<tresholdNavUp && button>tresholdselect) //press second button to navigate +
  {
    return 2;
  } else if (button<tresholdNavDown && button>tresholdNavUp) //press second button to navigate -
  {
    return 3;
  } else
  {
    return 0;
  }
}

void getHeatHum()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);

  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("DHT sensöründen veriler okunamadı");
    return;
  }

  float hic = dht.computeHeatIndex(t, h, false);

  display.clearDisplay();
  display.setTextColor(BLACK);
  display.drawLine(0, 0, 82, 0, BLACK);
  display.setCursor(0, 2);
  display.setTextSize(0.5);
  display.print(String(t) + "*C " + String(h) + "%");
  display.setCursor(0, 10);
  display.print("HeatIn:" + String(hic) + "*C");
  display.drawLine(0, 18, 82, 18, BLACK);

}

void getTimeDate()
{
    DateTime now = rtc.now();
  display.setCursor(0, 20);

  if (now.hour() < 10) {
    if (now.minute() < 10) {
      display.print("0" + String(now.hour()) + ((now.second() % 2 == 0) ? " " : ":") + "0" + String(now.minute()) + " " + String(now.day(), DEC) + "." + String(now.month(), DEC) + "." + String(now.year() % 100, DEC));
    } else {
      display.print("0" + String(now.hour()) + ((now.second() % 2 == 0) ? " " : ":") + String(now.minute()) + " " + String(now.day(), DEC) + "." + String(now.month(), DEC) + "." + String(now.year() % 100, DEC));
    }
  } else {
    if (now.minute() < 10) {
      display.print(String(now.hour()) + ((now.second() % 2 == 0) ? " " : ":") + "0" + String(now.minute()) + " " + String(now.day(), DEC) + "." + String(now.month(), DEC) + "." + String(now.year() % 100, DEC));
    } else {
      display.print(String(now.hour()) + ((now.second() % 2 == 0) ? " " : ":") + String(now.minute()) + " " + String(now.day(), DEC) + "." + String(now.month(), DEC) + "." + String(now.year() % 100, DEC));
    }
  }
  display.drawLine(0, 28, 82, 28, BLACK);

}

void loop() {

  //sıcaklık ve nem değerleri oku ve ekrana yaz
  getHeatHum();

  //saat ve tarih değerleri oku ve ekrana yaz
  getTimeDate();
  
    // Read the analog value from A0
  int sensorValue = analogRead(A0);
  //Serial.print(sensorValue);

  if (sensorValue > 300)
  {
    controlbutton = 1;
  }

  if (sensorValue > 300 && controlbutton > 0 && selectionValue1 > 0 )
  {
    controlbutton2 = 1;
  }

  if (sensorValue >300 && selectionValue1 > 0 && selectionValue2 > 0 )
  {
    controlbutton3 = 1;
  }

  if (selectionValue1 == 0 && controlbutton == 1) // first selection
  {
   
     switch(ButtonValue(sensorValue))
    {
      case 1:
    //selection
    display.setCursor(0, 30);
    selectionValue1 = selection1;
    selection1 = 5000;
    showselection(selectionValue1,selectionValue2);
    break;
      case 2:
    //navigate+
    selection1++;
    displayselection1(selection1,selectionValue2);
    break;
      case 3:
    //navigate-
    selection1--;
    displayselection1(selection1,selectionValue2);
    break;
      default:
    displayselection1(selection1,selectionValue2);
      break;
    }
  }

  if (selectionValue1 > 0 && selectionValue2 == 0 && controlbutton2 == 1)//if first selection is done
  {
    switch(ButtonValue(sensorValue))
    {
      case 1:
    //selection
    display.setCursor(0, 30);
    selectionValue2 = selection2;
    selection2 = 5000;
    showselection(selectionValue1,selectionValue2);
    break;
      case 2:
    //navigate+
    selection2++;
    displayselection1(selection2,selectionValue1);
    break;
      case 3:
    //navigate-
    selection2--;
    displayselection1(selection2,selectionValue1);
    break;
      default:
    displayselection1(selection2,selectionValue1);
      break;
    }
  }
  else if (selectionValue1 > 0 && selectionValue2 > 0 && controlbutton3 ==1)//if both selection is done
  {
    display.setCursor(0, 30);
    if (selectionValue1 % 2 == 0)// if time is selected
    {
      if (selectionValue2 % 2 == 0) // if hour is selected
      {
        amendHour(sensorValue);
      }
      else if (selectionValue2 % 2 == 1) // if minute is selected
      {
        amendMinute(sensorValue);
      }
    }
    else if(selectionValue1 % 2 == 1)// if date is selected
    {
      if (selectionValue2 % 3 == 0) // if day is selected
      {
        amendDay(sensorValue);
      }
      else if (selectionValue2 % 3 == 1)//if month is selected
      {
        amendMonth(sensorValue);
      }
      else if (selectionValue2 % 3 == 2)//if year is selected
      {
        amendYear(sensorValue);
      }
    }
  }
  
  // Draw a box as a reference
  display.drawRect(0, 40, 84, 5, BLACK);

  // Draw the scrolling animation
  drawScrollingAnimation();

  display.display();
  delay(100);
}

void drawScrollingAnimation() {
  // Draw a simple rectangle moving from left to right and then right to left
  int rectWidth = 10;
  int rectHeight = 5;

  if (scrollRight) {
    scrollPosition += 2;
    if (scrollPosition > (84 - rectWidth)) {
      scrollRight = false;
    }
  } else {
    scrollPosition -= 2;
    if (scrollPosition < 0) {
      scrollRight = true;
    }
  }

  display.fillRect(scrollPosition, 40, rectWidth, rectHeight, BLACK);
}

When you compile (or upload), would you copy/paste the memory report from the serial monitor window of the IDE?

my code is using 70% of storage 66% of dynamic memory.

Sketch uses 22598 bytes (70%) of program storage space. Maximum is 32256 bytes.
Global variables use 1363 bytes (66%) of dynamic memory, leaving 685 bytes for local variables. Maximum is 2048 bytes.

You are using pins 2 and 3 twice...

I am certain you have copy/pasted two sketches together... which is okay to do, but you must verify these conflicts are resolved. Maybe start with just one sketch to verify your screen...

Try this tutorial... made by Rui Santos, who is a known author...

So how long is a "while"? Are there whiles of different length? How do you know that Arduino is not responding to the buttons? Your code might be kosher, and your problems are all down to mechanical issues with the 5110. You might be able to verify this by copying display commands to the serial monitor.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.