/*LOW ACTIVE LED */
/* CPE345 - 65006866 อัศวิน อินทกูล - SEC002*/
#define btn01 22
long ranNum = 0;
int first = 1; //กำหนดรอบแรกเพื่อให้ไฟครบตอนเริ่ม
void setup() {
  Serial.begin(115200);
  Serial.println("Random Dice with (LOW ACTIVE LED) !");
  pinMode(0, OUTPUT); //1
  pinMode(2, OUTPUT); //2
  pinMode(4, OUTPUT); //3
  pinMode(5, OUTPUT); //4
  pinMode(16, OUTPUT); //5
  pinMode(17, OUTPUT); //6
  pinMode(18, OUTPUT); //7
  pinMode(btn01, INPUT); // btn
  reset();
  delay(200);
  digitalWrite(0, LOW);
  delay(200);
  digitalWrite(2, LOW);
  delay(200);
  digitalWrite(4, LOW);
  delay(200);
  digitalWrite(16, LOW);
  delay(200);
  digitalWrite(17, LOW);
  delay(200);
  digitalWrite(18, LOW);
  delay(200);
  digitalWrite(5, LOW);
  delay(200);
  reset();
  delay(200);
}
void loop() {
  Serial.println("------");
  Serial.println(ranNum);
  int btnRead = digitalRead(btn01);
  delay(200);
  if(btnRead == HIGH){
      first++;
      reset();
      Serial.println("Dice . . .");
      ranNum = random(1,7); // random 1-6
      switch(ranNum){
      case 6:
        digitalWrite(0, LOW);
        digitalWrite(2, LOW);
        digitalWrite(4, LOW);
        digitalWrite(16, LOW);
        digitalWrite(17, LOW);
        digitalWrite(18, LOW);
        delay(200);
        reset();
      break;
      case 5:
        digitalWrite(0, LOW);
        digitalWrite(2, LOW);
        digitalWrite(5, LOW);
        digitalWrite(17, LOW);
        digitalWrite(18, LOW); 
        delay(200);
        reset();
      break;
      case 4:
        digitalWrite(0, LOW);
        digitalWrite(2, LOW);
        digitalWrite(17, LOW);
        digitalWrite(18, LOW);
        delay(200);
        reset();
      break;
      case 3:
        digitalWrite(0, LOW);
        digitalWrite(5, LOW);
        digitalWrite(18, LOW);
        delay(200);
        reset();
      break;
      case 2:
        digitalWrite(0, LOW);
        digitalWrite(18, LOW);
        delay(200);
        reset();
      break;
      case 1:
        digitalWrite(5, LOW);
        delay(200);
        reset();
      break;
      }
      
  }else //สำหรับไฟค้างตอนทอยเสร็จ
  {
    if(first!=1){ //เช็ครอบแรก?
      if(ranNum==6){
          digitalWrite(0, LOW);
          digitalWrite(2, LOW);
          digitalWrite(4, LOW);
          digitalWrite(16, LOW);
          digitalWrite(17, LOW);
          digitalWrite(18, LOW);
      }
      else if(ranNum==5){
          digitalWrite(0, LOW);
          digitalWrite(2, LOW);
          digitalWrite(5, LOW);
          digitalWrite(17, LOW);
          digitalWrite(18, LOW); 
      }
      else if(ranNum==4){
          digitalWrite(0, LOW);
          digitalWrite(2, LOW);
          digitalWrite(17, LOW);
          digitalWrite(18, LOW);
      }
      else if(ranNum==3){
          digitalWrite(0, LOW);
          digitalWrite(5, LOW);
          digitalWrite(18, LOW);
      }
        else if(ranNum==2){
          digitalWrite(0, LOW);
          digitalWrite(18, LOW);
      }
        else if(ranNum==1){
          digitalWrite(5, LOW);
      }   
    }
  }
}
void reset(){
      digitalWrite(0, HIGH);
      digitalWrite(2, HIGH);
      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(16, HIGH);
      digitalWrite(17, HIGH);
      digitalWrite(18, HIGH);
}