Flickering on true IF cycle, if false input is given flickering stops

So, i have connected a display 16x2 to my board, and i want it to show some different screens.
The default should show time (ipse dixit for now) and the second pick data from a DHT11 sensor to print temperature and humidity.

here’s the code:

#include <DHT.h>
#include <LiquidCrystal.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#define DHTPIN 13                                                // <- pin used for DHT11 signal input
#define DHTTYPE DHT11                                                 // dht11 used as specific sensor
const int dispback = 6;                                        // <- pin used for backlight of the lcd
const int receiver = 7;                                             // <- pin used for IR signal input
IRrecv irrecv(receiver);                                                // create instance of 'irrecv'
decode_results results;
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);     // <- string to edit if you want to change default lcd pins
int reading = 0;
int mode = 0;
int backlight = 1;                             // variable used for switch the backlight of the screen
int crono1 = 0;
int crono2 = 0;

void setup() 
{
  Serial.begin(9600);                                                     // for serial monitor output
  irrecv.enableIRIn();                                                           // Start the receiver
  pinMode (dispback, OUTPUT);
  lcd.begin(16, 2);                                               // display dimensions, columns, rows
  lcd.clear();
}

void loop() 
{
  int t = dht.readTemperature();
  int h = dht.readHumidity();
  if (irrecv.decode(&results))
  {
    reading = results.value;
    Serial.print(reading);
    Serial.print("__");
    Serial.println(results.value, HEX); // display it on serial monitor in hexadecimal
    irrecv.resume();
  }
  if (results.value == 0xFFA25D) //on-off button on remote
  {
    switch (backlight)
    {
      case 0:
      digitalWrite(dispback, HIGH);
      backlight++;
      break;
      
      case 1:
      digitalWrite(dispback, LOW);
      backlight--;
      break;
    }
  }
  if (results.value == 0xFFE21D) //function button remote
  {
    switch (mode)
    {
      case 1:
      lcd.clear();
      mode = 0;
      break;
      default:
      lcd.clear();
      mode++;
      break;
    }
  }
  switch (mode)
    {
      case 0:
      lcd.setCursor(0,0);
      lcd.print("here goes time");
      lcd.setCursor(0,1);
      lcd.print(":");
      break;

      case 1:
      lcd.setCursor(0,0);
      lcd.print("Temperature");
      lcd.setCursor(0,1);
      lcd.print("Humidity");
      lcd.setCursor(14, 0);
      lcd.print(t);
      lcd.setCursor(14, 1);
      lcd.print(h);
      break;
    }
}

my remote sends the first value and if i keep pressing, the FFFFFF value is sent.
Since i’ve not included FFFFFF in my code, if i press a little longer the button I want, everything goes as expected, i can also use any other button in the remote to have the same correcting effect.

The problem is: when only the value i want is received, i get flickering.
If is regarding to the backlight option, it’s the light itself that flickers, if is the funcion, the screen glitches.
I suspect a loop when the value of the remote is matched, can someone help me? maybe i didn’t got the proper way to use an IF or SWITCH.

Many thanks :slight_smile:

Hint: if your identification words on the display are always in the same location, only print them once, never clear the display after the first time in setup. Always check to see if the value you are going to print is the same value as you already printed, don't print it again. If you need to change a word, just print spaces to clear the area, then print the new word.

Paul

thank you for the tip, i’m going to edit it as soon as possible, but i think this won’t fix it, since it affects also the light of the screen.

This kind of "cleverness" dates from the days of the PDP-11 when programmers didn't have an optimizing compiler, and relied on "tricks" to improve efficiency. It makes no rational sense to increment a logical variable. It can only be true or false, it's not an integer. The fact that you can apply functions of one to the other is no longer appropriate in most circumstances, the new guiding principle is "say what you mean" because the compiler can usually figure out what you intended and compile equivalent code.

    switch (backlight)
    {
      case 0:
        digitalWrite(dispback, HIGH);
        backlight++;
        break;

      case 1:
        digitalWrite(dispback, LOW);
        backlight--;
        break;
    }

So this should definitely be

    switch (backlight)
    {
      case 0:
        digitalWrite(dispback, HIGH);
        backlight = 1;
        break;

      case 1:
        digitalWrite(dispback, LOW);
        backlight = 0;
        break;
    }

There are further refinements that you could add, but this basic de-obfuscation is a necessary step.

ok, thanks for the tip, since i'm working on the code right now, i already did, thanks!
since i'm here, i might ask...
i use the var "backlight" to make possible the switch between on and off for the screen.
is there a way to make the argument of the case "HIGH" or "LOW"?

something like that (that i already tried but won't work)

  switch (dispback)  //dispback is a constant referring to the pin that gives power to lcd backlight
    {
      case LOW:
        digitalWrite(dispback, HIGH);
        break;

      case HIGH:
        digitalWrite(dispback, LOW);
        break;
    }

This might work a bit better:

  switch (digitalRead(dispback))  //dispback is a constant referring to the pin that gives power to lcd backlight
    {
      case LOW:
        digitalWrite(dispback, HIGH);
        break;

      case HIGH:
        digitalWrite(dispback, LOW);
        break;
    }

ok, i managed to fix this by myself.
i figured out that indeed, when the ir received the signal i wanted, the whole thing goes in a endless loop
so i added a variable as a counter, and incremented it at the end of the switch/if cycle.
then i reset the counter at every void loop start.

here’s the code if anyone needs a fix to my same problem

#include <DHT.h>
#include <LiquidCrystal.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#define DHTPIN 13                                                // <- pin used for DHT11 signal input
#define DHTTYPE DHT11                                                 // dht11 used as specific sensor
const int dispback = 6;                                        // <- pin used for backlight of the lcd
const int receiver = 7;                                             // <- pin used for IR signal input
IRrecv irrecv(receiver);                                                // create instance of 'irrecv'
decode_results results;
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);     // <- string to edit if you want to change default lcd pins
int mode = 0;                                         //variable used for selecting the screen to show
int buttoncount = 0;                            //variable used for exiting when a ir code is received
int backlight = 1;                             // variable used for switch the backlight of the screen

void setup() 
{
  Serial.begin(9600);                                                     // for serial monitor output
  irrecv.enableIRIn();                                                           // Start the receiver
  pinMode (dispback, OUTPUT);                                // use the pin for powering lcd backlight
  lcd.begin(16, 2);                                               // display dimensions, columns, rows
  lcd.clear();
  digitalWrite(dispback, HIGH);
}

void loop() 
{
  int t = dht.readTemperature();                 // get temperature even if not in the temp-hum screen
  int h = dht.readHumidity();                                                     // same for humidity
  if (irrecv.decode(&results))            // get result from ir remote and store it in "results.value"
  {
    Serial.print("__");
    Serial.println(results.value, HEX);                 // display it on serial monitor in hexadecimal
    irrecv.resume();
    buttoncount = 0;
  }
  if (results.value == 0xFFA25D && buttoncount == 0)                        // on-off button on remote
  {
    switch (backlight)
    {
      case 0:
      digitalWrite(dispback, HIGH);                                               // turn on backlight
      backlight = 1;
      buttoncount = 1;
      break;
      
      case 1:
      digitalWrite(dispback, LOW);                                               // turn off backlight
      backlight = 0;
      buttoncount = 1;
      break;
    }
  }
  if (results.value == 0xFFE21D && buttoncount == 0)                         // function button remote
  {
    switch (mode)
    {
      case 0:
      mode = 1;
      buttoncount = 1;
      break;
      default:
      mode = 0;
      buttoncount = 1;
      break;
    }
  }
  switch (mode)
    {
      case 0:
      lcd.setCursor(0,0);
      lcd.print("here goes time  ");
      lcd.setCursor(0,1);
      lcd.print(":               ");
      break;

      case 1:
      lcd.setCursor(0,0);
      lcd.print("Temperature   ");
      lcd.setCursor(0,1);
      lcd.print("Humidity      ");
      lcd.setCursor(14, 0);
      lcd.print(t);
      lcd.setCursor(14, 1);
      lcd.print(h);
      break;
    }
}

if anyone has other optimization suggestions I’m all ears! thank you

wildbill:
This might work a bit better:

  switch (digitalRead(dispback))  //dispback is a constant referring to the pin that gives power to lcd backlight

{
      case LOW:
        digitalWrite(dispback, HIGH);
        break;

case HIGH:
        digitalWrite(dispback, LOW);
        break;
    }

thank you! that's exactly what i've been searching for. ALready implemented, thanks

Refactor this

  if (results.value == 0xFFA25D && buttoncount == 0)                        // on-off button on remote
  {
    switch (backlight)
    {
      case 0:
        digitalWrite(dispback, HIGH);                                               // turn on backlight
        backlight = 1;
        buttoncount = 1;
        break;

      case 1:
        digitalWrite(dispback, LOW);                                               // turn off backlight
        backlight = 0;
        buttoncount = 1;
        break;
    }
  }

to

  if (results.value == 0xFFA25D && buttoncount == 0)                        // on-off button on remote
  {
    switch (backlight)
    {
      case 0:
        digitalWrite(dispback, HIGH);                                               // turn on backlight
        backlight = 1;
        break;

      case 1:
        digitalWrite(dispback, LOW);                                               // turn off backlight
        backlight = 0;
        break;
    }
    buttoncount = 1;
  }

always be on the lookout for senseless repetition