Hello guys,
I recently bought a 7-segment 4-digit display and i got it to work after a LOT of effort. My display works just fine except the second digit. My code is the following. The problem is that if I change the loop() function writing the 2nd digit[] to HIGH and the segment[] I want to LOW the thing works but if I try calling the writeDigit() function to write a number nothing happens. Even if I call the writeNum() function with a 4-digit number (ex. 1234) every segment turns on correctly except the second one. What am I doing wrong?
int digit[4] = {2,3,4,5};
int segment[7] = {6,7,8,9,10,11,12};
int dot = 13;
int x;
int del = 5;
int num[10][7]= {
{1,1,1,1,1,1,0},
{0,0,0,1,1,0,0},
{1,0,1,1,0,1,1},
{0,0,1,1,1,1,1},
{0,1,0,1,1,0,1},
{0,1,1,0,1,1,1},
{1,1,1,0,1,1,1},
{0,0,1,1,1,0,0},
{1,1,1,1,1,1,1},
{0,1,1,1,1,1,1}
};
void setup() {
for (int i = 2; i <= 13; i++){
pinMode(i, OUTPUT);
}
Serial.begin(9600);
reset();
}
void loop() {
if (Serial.available() > 0){
reset();
x = Serial.parseInt();
}
writeNum(x);
}
void writeNum(int x){
if(x < 10){
writeDigit(4, x);
delayMicroseconds(del);
} else if(x < 100){
writeDigit(3, (x / 10));
delayMicroseconds(del);
writeDigit(4, (x % 10));
delayMicroseconds(del);
} else if(x < 1000){
writeDigit(2, (x / 100));
delayMicroseconds(del);
writeDigit(3, ((x%100)/10));
delayMicroseconds(del);
writeDigit(4, ((x%100)%10));
delayMicroseconds(del);
} else {
int one = x / 1000;
x = x % 1000;
int two = x / 100;
x = x % 100;
int three = x / 10;
int four = x % 10;
writeDigit(1, one);
delayMicroseconds(del);
writeDigit(2, two);
delayMicroseconds(del);
writeDigit(3, three);
delayMicroseconds(del);
writeDigit(4, four);
delayMicroseconds(del);
}
}
void writeDigit(int row, int x){
reset();
digitalWrite(digit[row-1], HIGH);
for (int i = 0; i < 7; i++){
if (num[x][i] == 1){
digitalWrite(segment[i], LOW);
}else{
digitalWrite(segment[i], HIGH);
}
}
}
void reset(){
for (int i = 0; i < 4; i++){
digitalWrite(digit[i], LOW);
}
for (int i = 0; i < 7; i++){
digitalWrite(segment[i], HIGH);
}
digitalWrite(dot, HIGH);
}