// Hands-on 2: GPIO Input with Debouncing //
// Nama: Moh. Thoriq Ghautsillah //
// NIM: 20/464222/SV/18541 //
int digit[10][7] = {{LOW,LOW,LOW,LOW,LOW,LOW,HIGH}, // Angka 0
{HIGH,LOW,LOW,HIGH,HIGH,HIGH,HIGH}, // Angka 1
{LOW,LOW,HIGH,LOW,LOW,HIGH,LOW}, // Angka 2
{LOW,LOW,LOW,LOW,HIGH,HIGH,LOW}, // Angka 3
{HIGH,LOW,LOW,HIGH,HIGH,LOW,LOW}, // Angka 4
{LOW,HIGH,LOW,LOW,HIGH,LOW,LOW}, // Angka 5
{LOW,HIGH,LOW,LOW,LOW,LOW,LOW}, // Angka 6
{LOW,LOW,LOW,HIGH,HIGH,HIGH,HIGH}, // Angka 7
{LOW,LOW,LOW,LOW,LOW,LOW,LOW}, // Angka 8
{LOW,LOW,LOW,LOW,HIGH,LOW,LOW}}; // Angka 9
int ledPin[7] = {6,7,8,9,10,11,12}; // Inisialisasi array pin LED
int button_1 = 2; // Inisialisasi pin pushbutton count up
int button_2 = 3; // Inisialisasi pin pushbutton count down
int i; // Inisialisasi variabel increment for loop
int counter = 0; // Variabel penghitung
void setup() {
// put your setup code here, to run once:
for(i = 0; i < 8; i++)
{
pinMode(ledPin[i], OUTPUT); // Array pin LED sebagai OUTPUT
}
for(i = 0; i < 8; i++)
{
digitalWrite(ledPin[i], digit[0][i]); // Kondisi awal 7 segment menampilkan angka 0
}
pinMode(button_1, INPUT_PULLUP); // Mode button_1 sebagai INPUT_PULLUP
pinMode(button_2, INPUT_PULLUP); // Mode button_2 sebagai INPUT_PULLUP
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(button_1) == 0)
{
delay(10);
if(digitalRead(button_1) == 0)
{
delay(10);
while(digitalRead(button_1) == 0){delay(1);} // Debouncing
counter++; // Increment pada variabel counter
if(counter > 9)
{
counter = 0;
}
}
}
if(digitalRead(button_2) == 0)
{
delay(10);
if(digitalRead(button_2) == 0)
{
delay(10);
while(digitalRead(button_2) == 0){delay(1);} // Debouncing
counter--; // Decrement pada variabel counter
if(counter < 0)
{
counter = 9;
}
}
}
for(i = 0; i < 8; i++)
{
digitalWrite(ledPin[i], digit[counter][i]); // Menampilkan angka pada 7 segment
}
}