I have built a count up code for a 7 segment LED display like this.
However, it has terrible ghosting but I couldn't fix it.
I want to build a code without delay() but millis() for the combination of some function.
Would you give me some advice?
//sample source
/*https://books.google.co.jp/books?id=xJOBDwAAQBAJ&pg=PA130&lpg=PA130&dq=arduino+7+segment+LED+millis++millis++ghosting&source=bl&ots=cY-duNyZQj&sig=ACfU3U3Z7iC69TV9xWq5jmc-mdw-ushFYA&hl=ja&sa=X&ved=2ahUKEwibn_eo6ovhAhWJHHAKHQspCRcQ6AEwAXoECAkQAQ#v=onepage&q=ghotsting&f=false
*/
#define DATA 11
#define LATCH 10
#define CLOCK 9
// 0b11111110 DP
// 0b11111101 A
// 0b11111011 B
// 0b11110111 C
// 0b11101111 D
// 0b11011111 E
// 0b10111111 F
// 0b01111111 G
static const int num[] {
0b10000001, //ZERO
0b11110011, //ONE
0b01001001, //TWO
0b01100001, //THREE
0b00110011, //FOUR
0b00100101, //FIVE
0b00000101, //SIX
0b11110001, //SEVEN
0b00000001, //EIGHT
0b00100001, //NINE
0b10000001, //ZERO
0b11110010, //ONE DOT
0b01001000, //TWO DOT
0b01100000, //THREE DOT
0b00110010, //FOUR DOT
0b00100100, //FIVE DOT
0b00000100, //SIX DOT
0b11110000, //SEVEN DOT
0b00000000, //EIGHT DOT
0b00100000, //NINE DOT
};
int digits[] = {2,3,4};
int start;
int timer;
int interval = 5000;
int buzzer_time = 20;
int blinkingtime = 10;
int del = 10;
void setup()
{
pinMode(DATA, OUTPUT);
pinMode(LATCH, OUTPUT);
pinMode(CLOCK, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(12,OUTPUT);
analogRead(A0);
start = millis();
}
void loop()
{
start = millis();
while (millis() - start < interval)
{
timer = millis(); //time in elapsed seconds
digit(0,((timer)%100000)/10000,0); //digit D1 for thousands
digit(1,((timer)%1000000)/100000,0); //digit D2 for hundreds
digit(2,((timer)%10000)/1000,0);
}
}
void digit(int d, int n, int DP)
{ //turn all digits off, digit states are HIGH
for (int i = 0; i<3; i++) digitalWrite(digits[i],HIGH);
digitalWrite(LATCH, LOW); //add 128 for decimal point
shiftOut(DATA, CLOCK, MSBFIRST, num[n]);
digitalWrite(LATCH,HIGH); //change display pattern
digitalWrite(digits[d],LOW); //turn digit on, digit state LOW
delay(del);
}