class Sevensegment { //add new class to write a number
private:
byte _LED_A; //attributes for class "Sevensegment",
byte _LED_B;
byte _LED_C;
byte _LED_D;
byte _LED_E;
byte _LED_F;
byte _LED_G;
byte _LED_DP;
public:
Sevensegment(){} //default constructor, no one needs
Sevensegment(byte LED_A, byte LED_B, byte LED_C, byte LED_D, byte LED_E, byte LED_F, byte LED_G, byte LED_DP){ // constructor with all parameters (pins) a new display has
_LED_A = LED_A; // set the attributes of the object (display) to the parameters it was given by the constructor
_LED_B = LED_B;
_LED_C = LED_C;
_LED_D = LED_D;
_LED_E = LED_E;
_LED_F = LED_F;
_LED_G = LED_G;
_LED_DP = LED_DP;
}
void init(){ //initialize the new display by setting all pins to output
pinMode(_LED_A,OUTPUT);
pinMode(_LED_B,OUTPUT);
pinMode(_LED_C,OUTPUT);
pinMode(_LED_D,OUTPUT);
pinMode(_LED_E,OUTPUT);
pinMode(_LED_F,OUTPUT);
pinMode(_LED_G,OUTPUT);
pinMode(_LED_DP,OUTPUT);
}
void print(int number){ //print some number to the new display (object)
boolean digit[10] [8]{ //define the pins x, that have to be turned on to show the number y
{1,1,1,1,1,1,0,0},
{0,1,1,0,0,0,0,0},
{1,1,0,1,1,0,1,0},
{1,1,1,1,0,0,1,0},
{0,1,1,0,0,1,1,0},
{1,0,1,1,0,1,1,0},
{1,0,1,1,1,1,1,0},
{1,1,1,0,0,0,0,0},
{1,1,1,1,1,1,1,0},
{1,1,1,1,0,1,1,0},
};
int ledPin[] = {_LED_A, _LED_B, _LED_C, _LED_D, _LED_E, _LED_F, _LED_G, _LED_DP }; //define the order of LEDs in the display
for(int i = 0; i <= 7; i++){
digitalWrite(ledPin[i],!digit[number] [i]); //turn on all necessary LEDs, !:because active low
}
}
void clear(){ //clear the display by setting all pins to HIGH (active LOW -> turn off)
int ledPin[] = {_LED_A, _LED_B, _LED_C, _LED_D, _LED_E, _LED_F, _LED_G, _LED_DP }; //define the order of LEDs in the display
for(int i = 0; i <= 7; i++){
digitalWrite(ledPin[i],HIGH);
}
}
};
Sevensegment display1(7/*LED_A*/,6/*LED_B*/,3/*LED_C*/,4/*LED_D*/,5/*LED_E*/,8/*LED_F*/,9/*LED_G*/,2/*LED_DP*/); // call the constructor and create new display object and define pins the LEDs are connected to
void setup() {
display1.init(); //initialize the display
}
void loop() { //print all digits and wait 1s
for(int i = 0; i<=9; i++){
display1.print(i);
delay(1000);
display1.clear();
}
}