Sketch 1
void setup(){
for(int pin = 2 ; pin <= 9 ; pin++){ // define digital pin 2-9 as output
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
}
}
void loop() {
// display number 0
int n0[8]={1, 1, 1, 0, 1, 1, 1, 0} ;
//display the array of n0[8] in digital pin 2-9
for(int pin = 2; pin <= 9 ; pin++){
digitalWrite(pin,n0[pin-2]);
}
delay(500);
// display number1
int n1[8]={1, 0, 0, 0, 0, 0, 1, 0} ;
// display the array of n1[8] in digital pin 2-9
for(int pin = 2; pin <= 9 ; pin++){
digitalWrite(pin,n1[pin-2]);
}
delay(500);
// display number 2
int n2[8]={1, 1, 0, 1, 1, 1, 0, 0} ;
// display the array of n2[8] in digital pin 2-9
for(int pin = 2; pin <= 9 ; pin++){
digitalWrite(pin,n2[pin-2]);
}
delay(500);
// display number 3
int n3[8]={1, 1, 0, 1, 0, 1, 1, 0} ;
// display the array of n3[8] in digital pin 2-9
for(int pin = 2; pin <= 9 ; pin++){
digitalWrite(pin,n3[pin-2]);
}
delay(500);
// display number 4
int n4[8]={1, 0, 1, 1, 0, 0, 1, 0} ;
// display the array of n4[8] in digital pin 2-9
for(int pin = 2; pin <= 9 ; pin++){
digitalWrite(pin,n4[pin-2]);
}
delay(500);
// display number 5
int n5[8]={0, 1, 1, 1, 0, 1, 1, 0} ;
// display the array of n5[8] in digital pin 2-9
for(int pin = 2; pin <= 9 ; pin++){
digitalWrite(pin,n5[pin-2]);
}
delay(500);
// display number 6
int n6[8]={0, 1, 1, 1, 1, 1, 1, 0};
//display the array of n6[8] in digital pin 2-9
for(int pin = 2; pin <= 9 ; pin++){
digitalWrite(pin,n6[pin-2]);
}
delay(500);
// display number 7
int n7[8]={1, 1, 0, 0, 0, 0, 1, 0} ;
// display the array of n7[8] in digital pin 2-9
for(int pin = 2; pin <= 9 ; pin++){
digitalWrite(pin,n7[pin-2]);
}
delay(500);
// display number 8
int n8[8]={1, 1, 1, 1, 1, 1, 1, 0} ;
// display the array of n8[8] in digital pin 2-9
for(int pin = 2; pin <= 9 ; pin++){
digitalWrite(pin,n8[pin-2]);
}
delay(500);
// display number 9
int n9[8]={1, 1, 1, 1, 0, 1, 1, 0} ;
// display the array of n9[8] in digital pin 2-9
for(int pin = 2; pin <= 9 ; pin++){
digitalWrite(pin,n9[pin-2]);
}
delay(500);
}
Sketch 2
int number[10][8] =
{
{1, 1, 1, 0, 1, 1, 1, 0}, //display 0
{1, 0, 0, 0, 0, 0, 1, 0}, //display 1
{1, 1, 0, 1, 1, 1, 0, 0}, //display 2
{1, 1, 0, 1, 0, 1, 1, 0}, //display 3
{1, 0, 1, 1, 0, 0, 1, 0}, //display 4
{0, 1, 1, 1, 0, 1, 1, 0}, //display 5
{0, 1, 1, 1, 1, 1, 1, 0}, //display 6
{1, 1, 0, 0, 0, 0, 1, 0}, //display 7
{1, 1, 1, 1, 1, 1, 1, 0}, //display 8
{1, 1, 1, 1, 0, 1, 1, 0} //display 9
};
void numberShow(int i) { //call this function to display numbers
for (int pin = 2; pin <= 9 ; pin++) {
digitalWrite(pin, number[i][pin - 2]);
}
}
void setup() {
for (int pin = 2 ; pin <= 9 ; pin++) { // define digital pins 2 to 9 as output
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
}
}
void loop() {
for(int j = 0; j <= 9 ; j++){
numberShow(j); // Call numberShow() function to display 0-9.
delay(500);
}
}