I'm making a simple counter using a 4-digit 7-segment display, but I don't know how a could display a number bigger than 9, or bigger than 99, with this display.
Basically, each time the pushbutton is pressed, 1 will be added to to the "buttonvalue" variable (I have not yet made this part of the code, since that's not what I need help for.
The way my "printn" function works is that you tell it the digit on which you want to display a number, the the said number. but obviously, a number like 10, 36 (or higher) can't be displayed.
I guess I could split the variable into units, tens and hundreds then display them separatly, but I don't know if it's possible, or if it's even a good way of doing it.
Oh and here's the unfinished code:
int pb = 2; //general variables
int d1 = 10;
int d2 = 11;
int d3 = 12;
int d4 = 13;
int seg1 = 3;
int seg2 = 4;
int seg3 = 5;
int seg4 = 6;
int seg5 = 7;
int seg6 = 8;
int seg7 = 9;
void setup() {
Serial.begin(9600);
pinMode(pb, INPUT_PULLUP);
pinMode(d1, OUTPUT);
pinMode(d2, OUTPUT);
pinMode(d3, OUTPUT);
pinMode(d4, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(d1, HIGH);
digitalWrite(d2, HIGH);
digitalWrite(d3, HIGH);
digitalWrite(d4, HIGH);
}
void loop() {
int pbstate = digitalRead(pb); //this is just for another part of my project, dont worry about it
printn(d1, 19);
}
void printn(int digit, int number) { //here is the "print" function
int x;
int i;
int j;
int n0[] = {1, 1, 1, 1, 1, 1, 0}; //i used arrays to "save" each number
int n1[] = {0, 1, 1, 0, 0, 0, 0};
int n2[] = {1, 1, 0, 1, 1, 0, 1};
int n3[] = {1, 1, 1, 1, 0, 0, 1};
int n4[] = {0, 1, 1, 0, 0, 1, 1};
int n5[] = {1, 0, 1, 1, 0, 1, 1};
int n6[] = {1, 0, 1, 1, 1, 1, 1};
int n7[] = {1, 1, 1, 0, 0, 0, 0};
int n8[] = {1, 1, 1, 1, 1, 1, 1};
int n9[] = {1, 1, 1, 1, 0, 1, 1};
if (number > 9) {
number = number - 10;
printn(d2, 1);
}
Serial.println(number);
digitalWrite(digit, HIGH);
for (i = 3; i < 10; i++) { //i used for loops to make the code more compact instead of having like 47 "digitalWrite"s
digitalWrite(i, 0);
}
for (i = 3, j = 0; i < 10, j < 7; i++, j++) {
if (number == 0) {
x = n0[j];
} else if (number == 1) {
x = n1[j];
} else if (number == 2) {
x = n2[j];
} else if (number == 3) {
x = n3[j];
} else if (number == 4) {
x = n4[j];
} else if (number == 5) {
x = n5[j];
} else if (number == 6) {
x = n6[j];
} else if (number == 7) {
x = n7[j];
} else if (number == 8) {
x = n8[j];
} else if (number == 9) {
x = n9[j];
}
digitalWrite(i, x);
digitalWrite(digit, LOW);
delayMicroseconds(800UL);
digitalWrite(digit, HIGH);
}
}