//I Kadek Yogi Kurniawan
//19102125

//Simulasi Program Running 3 LED dengan Pushbotton

int ledpin;
int ledred = 14;
int ledyellow = 12;
int ledgreen = 13;
int button = 15;
int val = 0; //menghitung status pin variable
int currentstate = 0;
int previousstate = 0;
int counter = 0;

void setup() {
  pinMode(ledpin, OUTPUT);
  pinMode(ledred, OUTPUT);
  pinMode(ledyellow, OUTPUT);
  pinMode(ledgreen, OUTPUT);
  pinMode(button, INPUT);
  Serial.begin(9600);
}

void loop() {
  val = digitalRead(button);
//perhitungan variable
  if (val==HIGH){
    digitalWrite(ledpin, HIGH);
    currentstate = 1;
  }else{
    digitalWrite(ledpin, LOW);
    currentstate = 0;
  }

  if(currentstate != previousstate){
    if(currentstate == 1){
      counter = counter + 1;
      Serial.println (counter);
    }
  }

//posisi saat simulasi pertama dijalankan dan pushbotton belum ditekan sama sekali, semua led mati
  if (counter == 0){
    digitalWrite(ledred, LOW);
    digitalWrite(ledyellow, LOW);
    digitalWrite(ledgreen, LOW);
  }
//saat pushbotton di tekan-lepas satu kali, maka led merah hidup yang lain mati
  if (counter == 1){
    digitalWrite(ledred, HIGH);
    digitalWrite(ledyellow, LOW);
    digitalWrite(ledgreen, LOW);
  }
//saat pushbotton di tekan-lepas dua kali, maka led kuning hidup yang lain mati
  if (counter == 2){
    digitalWrite(ledred, LOW);
    digitalWrite(ledyellow, HIGH);
    digitalWrite(ledgreen, LOW);
  }
//saat pushbotton di tekan-lepas tiga kali, maka led hijau hidup yang lain mati
  if (counter == 3){
    digitalWrite(ledred, LOW);
    digitalWrite(ledyellow, LOW);
    digitalWrite(ledgreen, HIGH);
  }
//saat pushbotton di tekan-lepas empat kali, maka kembali ke posisi awal dimana Led merah saja yang menyala
  if (counter == 4){
    counter = 1;
  }
  previousstate = currentstate;
  delay (100);
}