LCD does not show information

In this code:

#include <OneWire.h>
#include <DallasTemperature.h>
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

// Inicializa o pino do sensor de temperatura
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// Ligar e desligar - Refrigeração e Dejelo
#define PIN_REFRIGERACAO 5
#define PIN_DEJELO 6

// Definição de Max e Min temperatura - Por segurança
int tempMin = -5;
int tempMax = 15;

// Definição de temperatura desejada Inicial
int tempDesejada = 5;

// Definição dos botões de regular a temperatura desejada
#define BOTAO_AUMENTA 8
#define BOTAO_DIMINUI 9

// Variáveis para controle do Dejelo
unsigned long intervaloDejelo = 43200000; // 12 horas em milissegundos
unsigned long lastDejeloTime = 0;
bool dejeloActive = false;

void setup() {
  // inicializa o LCD
  lcd.init();
  lcd.backlight();
  
  // inicializa os pinos dos relés de refrigeração e Dejelo
  pinMode(PIN_REFRIGERACAO, OUTPUT);
  pinMode(PIN_DEJELO, OUTPUT);

  // inicializa os pinos dos botões
  pinMode(BOTAO_AUMENTA, INPUT_PULLUP);
  pinMode(BOTAO_DIMINUI, INPUT_PULLUP);

  // inicializa o sensor de temperatura
  sensors.begin();
}

void loop() {
  // atualiza a temperatura sem maiores adições
  sensors.requestTemperatures(); 
  float temperature = sensors.getTempCByIndex(0);
  
  //Nome escolhido
  lcd.setCursor(0, 0);
  lcd.print("STRAPPER");

  // escreve a temperatura com base no sensor
  lcd.setCursor(0, 1);
  lcd.print("Temp: ");
  lcd.print(temperature);
  lcd.print("C");
  
  // Regula o valor da temperatura desejada - alterar: delay(**) - para aumentar ou diminuir o tempo de resposta
  if (digitalRead(BOTAO_AUMENTA) == LOW) {
    tempDesejada++;
    delay(200);
  }
  
  if (digitalRead(BOTAO_DIMINUI) == LOW) {
    tempDesejada--;
    delay(200);
  }
  
  // escreve a temperatura desejada
  lcd.setCursor(9, 0);
  lcd.print("TD: ");
  lcd.print(tempDesejada);
  lcd.print("C");

  // Verifica se o tempo de espera para o dejelo ja concluiu

if (millis() - lastDejeloTime > intervaloDejelo) {
  digitalWrite(PIN_DEJELO, HIGH);
  delay(1200000);   //Tempo que fica ligado o dejelo (20 min e milissegundos)
  digitalWrite(PIN_DEJELO, LOW);
  lastDejeloTime = millis();
}
 
  // liga ou desliga o relé de refrigeração de acordo com a temperatura desejada
  if (temperature < tempDesejada) {
    digitalWrite(PIN_REFRIGERACAO, HIGH);
  } else {
    digitalWrite(PIN_REFRIGERACAO, LOW);
  }

  // Intervalo de atualização do LCD (atualiza os valores de temperatura com base no sensor)
  delay(2000);
}

I'm using these devices:
DS18B20 temperature sensor
4.7K ohm resistor
16x2 LCD display with I2C interface
2 buttons to adjust the temperature
Two relays - one to control the refrigeration system and one to control the defrost cycle.

After doing all the installation and carrying out the test, the system is working, however, after about 20 minutes, the LCD stops showing the information, it stays on and the system continues working, but the LCD just stops showing the information, what can be?

I can't imagine what it could be because you didn't post any hardware information at all. Please read and post what is suggested here:

1 Like

This call of the delay() function will block all activities for 20 minutes.

I can tell you one thing about your code. You can't use delay() together with a millis() timing system:

if (millis() - lastDejeloTime > intervaloDejelo) {
  digitalWrite(PIN_DEJELO, HIGH);
  delay(1200000);   //Tempo que fica ligado o dejelo (20 min e milissegundos)

While the delay() runs, any millis() checks will be ignored.

I understand, but it's strange, because it works correctly for a while, and only the LCD stops working, are these make sense?
And how can i improve this code to work correctly?

Do not use the delay() function.

Likely, that "while" is while

(millis() - lastDejeloTime > intervaloDejelo)

is false.

what can i use instead?

millis()

I make this:

if (millis() - lastDejeloTime > intervaloDejelo) {
  digitalWrite(PIN_DEJELO, HIGH);
  dejeloActive = true;
  lastDejeloTime = millis();
}

if (dejeloActive && millis() - lastDejeloTime > 1200000) {
  digitalWrite(PIN_DEJELO, LOW);
  dejeloActive = false;
}

but the initial problem remains

Always post ALL the code.

Combining a millis() check with any conditional statement, or nesting it in a conditional block is always risky. It means, any time the conditional is false, the timing system that you set up will fail.

In this case, if 'intervaloDejelo' is less than 1200000, 'lastDejeloTime' keeps getting reset, so the second check can never pass.

Please explain your timing requirements. A timing diagram would be ideal.

I'm setting up a refrigerator, so it turns on the compressor to cool;
And every 12 hours it turns on the resistance, for 20 minutes, which is used for thaw;
They work according to the temperature I set;
is there another function i can use to tell time or manage this?

You should probably use an RTC for that. If you lose power, all your timing will reset.

However, the general problem you face, is not due to a choice of time functions. It's your choice of programming logic.

Please describe the desired behaviour while

  • running normally
  • thawing

For example, you may want to disable the compressor during thaw time. So please break it down into explicit parts for us.

You should never rush into a programming task without a clear mental image (if not documentation) of the problem or task at hand. The code itself is not adequate for that, it will not help you define the problem (well, only very rarely).

Coding is not a design activity. It is a translation activity, that converts ideas into actions.

1 Like

ok, i'll try to improve what i did and research better about it, thanks

Hello @rodrigo_dl,

Timing with millis() gets easier as you use it more.

Timing with an RTC might be a lot easier, and better for refrigeration.

I made this "Pet Feeder" for a Arduino Forum user... You can see the RTC setup at the top, and how two alarms are set (hours/minutes), and when the alarms make the servo "feed" his cat.

You could build your own sketch from parts of this to "turn on/turn off" your refrigeration.

1 Like

Programming has been called an art as much as it has been called a science, being hidden kinetic art. Ask any fractal.

1 Like

Lots of people struggle with it, and then suddenly go "aha, it's so obvious". It can be somewhat an individual discovery.

1 Like

So, I solved the problem, but redid the code, it was like this:

#include <OneWire.h>
#include <DallasTemperature.h>
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

// Inicializa o pino do sensor de temperatura
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// Ligar e desligar - Refrigeração e Dejelo.
#define PIN_REFRIGERACAO 5
#define PIN_DEJELO 6

// Definição de temperatura desejada Inicial
int tempDesejada = 5;

// Definição dos botões de regular a temperatura desejada
#define BOTAO_AUMENTA 8
#define BOTAO_DIMINUI 9

// Variáveis para controle do Dejelo
unsigned long intervaloDejelo = 43200000; // 12 horas em milissegundos
unsigned long lastDejeloTime = 0;
bool dejeloActive = false;

// Variáveis para controle da atualização do LCD
unsigned long lastLcdUpdateTime = 0;
const unsigned long lcdUpdateInterval = 2000;

unsigned long lastRefrigeracaoOnTime = 0;
unsigned long tempoMinimoAtivacao = 300000; // Tempo mínimo em milissegundos que o pino de refrigeração deve ficar ativo antes de ser ligado
unsigned long tempoMinimoAtivacaoDesligamento = 300000; // E para ser desligado

void setup() {
  // inicializa o LCD
  lcd.init();
  lcd.backlight();
  
  // inicializa os pinos dos relés de refrigeração e Dejelo
  pinMode(PIN_REFRIGERACAO, OUTPUT);
  pinMode(PIN_DEJELO, OUTPUT);

  // inicializa os pinos dos botões
  pinMode(BOTAO_AUMENTA, INPUT_PULLUP);
  pinMode(BOTAO_DIMINUI, INPUT_PULLUP);

  // inicializa o sensor de temperatura
  sensors.begin();

}

void loop() {
  // atualiza a temperatura sem maiores adições
  sensors.requestTemperatures(); 
  int temperature = sensors.getTempCByIndex(0);
  
  //Nome escolhido
  lcd.setCursor(0, 0);
  lcd.print("STRAPPER");

  // atualiza o LCD se o intervalo de tempo definido já passou
  if (millis() - lastLcdUpdateTime >= lcdUpdateInterval) {
    // escreve a temperatura com base no sensor
    lcd.setCursor(0, 1);
    lcd.print("Temp: ");
    lcd.print(int(temperature));
    lcd.print("C");

    // atualiza a temperatura desejada
    unsigned long lastButtonTime = 0;
    const unsigned long buttonInterval = 200;

    if (digitalRead(BOTAO_AUMENTA) == LOW) {
      if (millis() - lastButtonTime > buttonInterval) {
        tempDesejada++;
        lastButtonTime = millis();
      }
    }

    if (digitalRead(BOTAO_DIMINUI) == LOW) {
      if (millis() - lastButtonTime > buttonInterval) {
        tempDesejada--;
        lastButtonTime = millis();
      }
    }
  
  // escreve a temperatura desejada
  lcd.setCursor(9, 0);
  lcd.print("TD: ");
  lcd.print(int(tempDesejada));
  lcd.print("C");

  // Verifica se o tempo de espera para o dejelo ja concluiu

if (millis() - lastDejeloTime > intervaloDejelo) {
  digitalWrite(PIN_DEJELO, HIGH);
  dejeloActive = true;
  lastDejeloTime = millis();
}

  // Verifica o tempo em que o degelo vai ficar ativo, Para alterar o tempo de degelo, alterar o valor atual
if (dejeloActive && millis() - lastDejeloTime > 1200000) {
  digitalWrite(PIN_DEJELO, LOW);
  dejeloActive = false;
}
 
  // liga ou desliga o relé de refrigeração de acordo com a temperatura desejada
if (temperature < tempDesejada) {
  digitalWrite(PIN_REFRIGERACAO, HIGH);
  lastRefrigeracaoOnTime = millis(); // armazena o tempo em que o relé foi ativado
} else {
  if (millis() - lastRefrigeracaoOnTime >= tempoMinimoAtivacao) { // verifica se o tempo mínimo de ativação foi alcançado
    digitalWrite(PIN_REFRIGERACAO, LOW);
    lastRefrigeracaoOnTime = millis(); // armazena o tempo em que o relé foi desativado
  }
}

// verifica se o tempo mínimo de desativação foi alcançado antes de ligar o relé novamente
if (millis() - lastRefrigeracaoOnTime >= tempoMinimoAtivacaoDesligamento && temperature < tempDesejada) {
  digitalWrite(PIN_REFRIGERACAO, HIGH);
  lastRefrigeracaoOnTime = millis(); // armazena o tempo em que o relé foi ativado novamente
}
  }
}

Thanks everyone who helped!

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