Hello I am using 4-digit 7 segment Tm1637 for my small project. the program is working as intended but the display is flickering and the outcome is unpleasant.
Can you check my code and explain me why is this happening.
Bare in mind that I very new to programing and Arduino.
my code:
#include <Arduino.h>
#include <TM1637Display.h>
// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3
// The amount of time (in milliseconds) between tests
#define TEST_DELAY 2000
TM1637Display display(CLK, DIO);
int button=A1;
int cuted;
int caunt=0;
int buttonR=A0;
int reset;
int buttonM=A2;
int minusone;
int delays=100;
void setup()
{
pinMode(button,INPUT);
pinMode(buttonR,INPUT);
pinMode(buttonM,INPUT);
}
void loop() {
cuted=digitalRead(button);
reset=digitalRead(buttonR);
minusone=digitalRead(buttonM);
if(cuted==HIGH){
caunt=caunt+2;
delay(delays);
}
if(reset==HIGH){
caunt=0;
delay(delays);
}
if(minusone==HIGH){
caunt=caunt-1;
delay(delays);
}
display.setBrightness(0x4);
uint8_t data[] = { 0x0, 0x0, 0x0, 0x0 };
display.setSegments(data);
display.showNumberDec(caunt, false, 4,4);
}
By using your code the flickering stopped, but I don't understand how this is working .
Is it possible for you to explain it for dumies or tell me where to find that info.
The display flickers each time you update it. Your code is updating it each time loop() runs, which can be hundreds of times each second. Probably 99% of these updates are not truly updates because the value shown has not changed, so can be avoided.
The changes suggested by @6v6gt only update the display if the value has changed.
But even this suggestion could be improved. Your code changes the brightness every time, even though the brightness stays the same. That could be done in setup(). Your code uses display.setSegments() to clear the digits. This is not required, because the display.showNumberDec() used immediately after will set every segment as needed to display the number.
Try this
#include <Arduino.h>
#include <TM1637Display.h>
// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3
// The amount of time (in milliseconds) between tests
#define TEST_DELAY 2000
TM1637Display display(CLK, DIO);
int button=A1;
int cuted;
int caunt=0;
int buttonR=A0;
int reset;
int buttonM=A2;
int minusone;
int delays=100;
void setup()
{
pinMode(button,INPUT);
pinMode(buttonR,INPUT);
pinMode(buttonM,INPUT);
display.setBrightness(0x4);
}
void loop() {
cuted=digitalRead(button);
reset=digitalRead(buttonR);
minusone=digitalRead(buttonM);
if(cuted==HIGH){
caunt=caunt+2;
display.showNumberDec(caunt, false, 4,4);
delay(delays);
}
if(reset==HIGH){
caunt=0;
display.showNumberDec(caunt, false, 4,4);
delay(delays);
}
if(minusone==HIGH){
caunt=caunt-1;
display.showNumberDec(caunt, false, 4,4);
delay(delays);
}
}
As @PaulRB has pointed out (with improvements), the flicker was caused by continuous updatimg of the screen.
Since you are displaying the value of caunt it is necessary to change the display only when that value changes, not every loop() iteration as you had it.
Just a note here because it may not be obvious: static int oldCaunt = -9999 ; a static variable retains its value between successive loop() iterations. The initialisation of a static variable (to -9999 in this case) is done only once at program start time.
You are acting if the buttons are high in your code which implies that the pins have pull down resistors. Is that correct.
Yes, indeed I'm using pulldown resistors. I still cant understand why I should set static int oldCaunt = -9999. But I will check it out.
Again, I am very thankful for the fix an the explain
I chose -9999 for oldCaunt to ensure that something unique appeared on the screen immediately at system start. If I had used say 0 for oldCaunt then at system start nothing would have appeared on the screen until either the cuted or minusone button had been pressed.
Many systems use a startup display of 8.8.8.8., as this demonstrates that all segments of each LED display are functional. Usually, this times out after one or two seconds, then the actual display of data values begins. Just a suggestion.