How to stop the display from flickering ?

Hi Class,

This code compiles:

#include <Arduino.h>
#include <HX711_ADC.h>
#include <TM1637TinyDisplay6.h>
#include <Keypad.h>

// HX711 circuit wiring
const int HX711_dout = 4;
const int HX711_sck = 5;

// TM1637 - 6 pinos
const int CLK = 3;
const int DIO = 2;

HX711_ADC balanca(HX711_dout, HX711_sck);
TM1637TinyDisplay6 display(CLK, DIO);

#define ROW_NUM     4 //4 linhas
#define COLUMN_NUM  4 //4 colunas

char keys[ROW_NUM][COLUMN_NUM] = {
  {'7', '8', '9', 'A'},
  {'4', '5', '6', 'B'},
  {'1', '2', '3', 'X'},
  {'0', '.', 'C', 'D'}
};

byte pin_rows[ROW_NUM] = {8, 9, 10, 11};
byte pin_column[COLUMN_NUM] = {12, 13, 14, 15};

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
char key = keypad.getKey();

float calibrationValue;
unsigned long t = 0;

void setup() {

  Serial.begin(115200);
  delay(10);
  Serial.println();

  display.begin();
  display.setBrightness(7);
  balanca.begin();

  calibrationValue = 158.20;
  unsigned long stabilizingtime = 2000;
  boolean _tare = true;
  balanca.start(stabilizingtime, _tare);
  if (balanca.getTareTimeoutFlag()) {
    Serial.println("HX711 nao responde, confira ligações");
    while (1);
  }
  else {
    balanca.setCalFactor(calibrationValue);
    Serial.println("Balanca pronta");
  } 

  balanca.tareNoDelay();
}
//------------------------------------------------------------------------------------------
void loop() {

  keyboard();  

  static boolean newDataReady = 0;
  const int serialPrintInterval = 100; //increase value to slow down serial print activity
    // check for new data/start next conversion:
  if (balanca.update()) newDataReady = true;
    // get smoothed value from the dataset:
  if (newDataReady)
  {
    if (millis() > t + serialPrintInterval) {
      float i = balanca.getData();
      //Serial.print("Load_cell output val: ");
      //Serial.println(i);
      newDataReady = 0;
      t = millis();
      display.showNumberDec(i);
    }
  }
  // check if last tare operation is complete
  if (balanca.getTareStatus() == true) {
    Serial.println("Tare complete");
  }
  float i = balanca.getData();
  
}

//-------------------------------------------------------------------------------------------
void keyboard()  {

  char key = keypad.getKey();
  display.showString("A");  

  if (key)  {    
    int x = key - '0'; 
    display.showNumberDec(x);
    Serial.println(x);
  } 
  
}  
//-------------------------------------------------- END

I have a load cell, Arduino UNO, 4x4 keyboard and TM1637 display - 6 digits.

In loop() I have the weight reading and also the void(keyboard).

Therefore, the display shows flashing values. It's quite annoying to see it blinking. I would like to see both the weight and values ​​typed on the keyboard without blinking.

I already added an integer and did it like this:
if(x == 1) {
keyboard();
}
To only activate this void when it was not showing weight. Ultimately, deactivating one void activates the other. But it did not work.

I would like to see both functions (weight reading and key values) without either of them showing flashing characters on the display.

What would the code for this look like ?

Grateful

Hi, @sergioarduino

Can you please post a copy of your circuit a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

What is your power supply?

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

1 Like

Hint: Your "void" functions are very hard to follow. They are not "void" functions, void tells you there is nothing returned from the function the next thing is the name of the function. Example: "void keyboard()" void says it returns nothing, keyboard is the name of the function. If the function needs parameters they are placed in between the ().

What is your definition of "showing weight"? I ask because your current code displays the scale value every time it had new data ready. How are you supposed to show keyboard input? A bit more understanding of your logic is needed. Once you press a key, should it only display that value? For how long?

Also, this is not safe:

It needs to be
if (millis() - t >= serialPrintInterval) {

How do you imagine this on one display? - if you constantly display either the weight or the keyboard symbol, the display will definitely blink.
Try displaying data with a delay - for example, display "A" and wait a second, then display the weight and wait again.

I put this here in loop()
It seems to have worked:

As long as I don't press C, it shows the weight. If I press C it shows the keys typed. But only 1 at a time.

What I want is: When turning on the scale, it first shows the value of a weight alarm, at this point I can edit this value or not (if the value is already what I want, I don't need to edit it). Editing or not, if I press any other key (like ), then it exits this initial screen and shows the weight, or shows 0 if there is no weight.

No flickering, of course.

Can anyone put the code to do this ?

#include <Arduino.h>
#include <HX711_ADC.h>
#include <TM1637TinyDisplay6.h>
#include <Keypad.h>

// HX711 circuit wiring
const int HX711_dout = 4;
const int HX711_sck = 5;

// TM1637 - 6 pinos
const int CLK = 3;
const int DIO = 2;

HX711_ADC balanca(HX711_dout, HX711_sck);
TM1637TinyDisplay6 display(CLK, DIO);

#define ROW_NUM     4 //4 linhas
#define COLUMN_NUM  4 //4 colunas

char keys[ROW_NUM][COLUMN_NUM] = {
  {'7', '8', '9', 'A'},
  {'4', '5', '6', 'B'},
  {'1', '2', '3', 'X'},
  {'0', '.', 'C', 'D'}
};

byte pin_rows[ROW_NUM] = {8, 9, 10, 11};
byte pin_column[COLUMN_NUM] = {12, 13, 14, 15};

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
char key = keypad.getKey();

int z,h;
float calibrationValue,i;
unsigned long t = 0;

void setup() {

  Serial.begin(115200);
  delay(10);
  Serial.println();

  display.begin();
  display.setBrightness(7);
  char key = keypad.getKey();
  balanca.begin();

  calibrationValue = 158.20;
  unsigned long stabilizingtime = 2000;
  balanca.tareNoDelay();
  boolean _tare = true;
  balanca.start(stabilizingtime, _tare);
  if (balanca.getTareTimeoutFlag()) {
    Serial.println("HX711 nao responde, confira ligações");
    while (1);
  }
  else {
    balanca.setCalFactor(calibrationValue);
    Serial.println("Balanca pronta");
  } 

  balanca.tareNoDelay();

}
//------------------------------------------------------------------------------------------
void loop() {
  
  char key = keypad.getKey();
  if (key == 'C')  {
    z = 1;  
  }
  if(z == 1) {
     if (key)  {  
       i = key - '0';
       display.showNumberDec(i);
     }
  } else {
     i = balanca.getData();
    }
   
  
  static boolean newDataReady = 0;
  const int serialPrintInterval = 100; //increase value to slow down serial print activity
    // check for new data/start next conversion:
  if (balanca.update()) newDataReady = true;
    // get smoothed value from the dataset:
  if (newDataReady)
  {
    if (millis() > t + serialPrintInterval) {
      //i = balanca.getData();
      //Serial.print("Load_cell output val: ");
      //Serial.println(i);
      newDataReady = 0;
      t = millis();
      display.showNumberDec(i);
    }
  }
  // check if last tare operation is complete
  if (balanca.getTareStatus() == true) {
    Serial.println("Tare complete");
  }
  float i = balanca.getData();
  
}

//-------------------------------------------------------------------------------------------

With just 1 digit on the right, the display does not flickr. As more numbers are shown, the flicker increases. Boring thing.

If you only do this at power on, then this code needs to go in setup(). It should stay there until you press whatever key exits that and then move on to loop().

A different approach is to design your code as a state machine (or finite state machine if you google). You will have several states (STARTUP, DISPLAY_WEIGHT, ...) and then you do what you need to do depending on what state you are in. If conditions are met, you transition to a new state.

I would further suggest you name your variables something meaningful. A variable like z provides zero information about what it may be used for within your code. It helps others understand your code easier/better.

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