//Deklarasikan pin untuk saklar pushbutton1 dan pushbutton2 serta LED
const int switchPin1 = 2; //Pin untuk saklar pushbutton1
const int switchPin2 = 3; //Pin untuk saklar pushbutton2
const int ledCount = 4; //Jumlah LED
int ledPins[] = {9, 10, 11, 12}; //Pin untuk setiap LED
int currentLed = 0; //Variabel untuk menyimpan indeks LED saat ini yang menyala
//Variabel untuk menyimpan status saklar sebelumnya
int switchState1 = 0; //status saklar pushbutton 1
int switchState2 = 0; //status saklar pushbutton 2
void setup() {
// put your setup code here, to run once:
pinMode(switchPin1, INPUT_PULLUP); //Internal pull-up resistor aktif
pinMode(switchPin2, INPUT_PULLUP); //Internal pull-up resistor aktif
//atur pin LED sbg output
for (int i = 0; i<ledCount; i++){
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
switchState1 = digitalRead(switchPin1);
switchState2 = digitalRead(switchPin2);
//Jika saklar pushbutton1 ditekan, LED menyala dari 1 menuju 4
if(switchState1 == LOW && switchState2 == HIGH){
for(int i = 0; i<=currentLed; i++){
digitalWrite(ledPins[i], HIGH);
delay(200);
}
currentLed++; //Pindah next LED
if(currentLed >= ledCount){
currentLed = 0; //Kembali ke LED pertama setelah mencapai LED terakhir
}
}
//Jika saklar pushbutton2 ditekan, maka reset (LED padam)
else if(switchState2 == LOW && switchState1 == HIGH){
for(int i = 0; i<ledCount; i++){
digitalWrite(ledPins[i], LOW);
}
currentLed = 0; //reset indeks LED saat ini
}
}