//Pengaturan hardware Seven Segment pins

int A = 2;

int B = 3;

int C = 4;

int D = 6;

int DP = 5;

int E = 7;

int F = 8;

int G = 9;

 

//pemberian status untuk push buttons pins, push buttons states, dan counter

int switchDownPin = 13;

int counter = 0;

int buttonDownState = 0;

int lastButtonDownState = 0;

 

void setup()

{

Serial.begin(9600);

 

pinMode(A, OUTPUT);

pinMode(B, OUTPUT);

pinMode(C, OUTPUT);

pinMode(D, OUTPUT);

pinMode(E, OUTPUT);

pinMode(F, OUTPUT);

pinMode(G, OUTPUT);

pinMode(DP, OUTPUT);

 

digitalWrite(DP ,HIGH);

}

 

void loop()

{

//mulai dengan menggunakan push button

buttonDownState = digitalRead(switchDownPin);

 

//melakukan proses pengurangan pada Seven Segment ketika button yang di tekan

if (buttonDownState != lastButtonDownState)

{

if (buttonDownState == HIGH)

{

if(counter == 0)

{

counter = 10;

}

counter--;

Serial.println(counter);

changeNumber(counter);

delay(300);

}

else

{

Serial.println("OFF");

}

delay(50);

}

changeNumber(counter);

}

 

//tampilan led seven segment ketika aktif.

void changeNumber(int buttonPress)

{

switch (buttonPress)

{

//number 0

case 0:

digitalWrite(A, LOW);

digitalWrite(B, LOW);

digitalWrite(C, LOW);

digitalWrite(D, LOW);

digitalWrite(E, LOW);

digitalWrite(F, LOW);

digitalWrite(G, HIGH);

break;

//number 1

case 1:

digitalWrite(A, HIGH);

digitalWrite(B, LOW);

digitalWrite(C, LOW);

digitalWrite(D, HIGH);

digitalWrite(E, HIGH);

digitalWrite(F, HIGH);

digitalWrite(G, HIGH);

break;

//number 2

case 2:

digitalWrite(A, LOW);

digitalWrite(B, LOW);

digitalWrite(C, HIGH);

digitalWrite(D, LOW);

digitalWrite(E, LOW);

digitalWrite(F, HIGH);

digitalWrite(G, LOW);

break;

//number 3

case 3:

digitalWrite(A, LOW);

digitalWrite(B, LOW);

digitalWrite(C, LOW);

digitalWrite(D, LOW);

digitalWrite(E, HIGH);

digitalWrite(F, HIGH);

digitalWrite(G, LOW);

break;

//number 4

case 4:

digitalWrite(A, HIGH);

digitalWrite(B, LOW);

digitalWrite(C, LOW);

digitalWrite(D, HIGH);

digitalWrite(E, HIGH);

digitalWrite(F, LOW);

digitalWrite(G, LOW);

break;

//number 5

case 5:

digitalWrite(A, LOW);

digitalWrite(B, HIGH);

digitalWrite(C, LOW);

digitalWrite(D, LOW);

digitalWrite(E, HIGH);

digitalWrite(F, LOW);

digitalWrite(G, LOW);

break;

//number 6

case 6:

digitalWrite(A, LOW);

digitalWrite(B, HIGH);

digitalWrite(C, LOW);

digitalWrite(D, LOW);

digitalWrite(E, LOW);

digitalWrite(F, LOW);

digitalWrite(G, LOW);

break;

//number 7

case 7:

digitalWrite(A, LOW);

digitalWrite(B, LOW);

digitalWrite(C, LOW);

digitalWrite(D, HIGH);

digitalWrite(E, HIGH);

digitalWrite(F, HIGH);

digitalWrite(G, HIGH);

break;

//number 8

case 8:

digitalWrite(A, LOW);

digitalWrite(B, LOW);

digitalWrite(C, LOW);

digitalWrite(D, LOW);

digitalWrite(E, LOW);

digitalWrite(F, LOW);

digitalWrite(G, LOW);

break;

//number 9

case 9:

digitalWrite(A, LOW);

digitalWrite(B, LOW);

digitalWrite(C, LOW);

digitalWrite(D, LOW);

digitalWrite(E, HIGH);

digitalWrite(F, LOW);

digitalWrite(G, LOW);

break;

}

}