#include <Adafruit_NeoPixel.h>

#define MAX_LED_NUM 16 //LEDの個数
#define delay_time  30 //ルーレット回転速度 数値が少ない方が難易度が高い 

Adafruit_NeoPixel strip = Adafruit_NeoPixel(MAX_LED_NUM, 6, NEO_GRB + NEO_KHZ800);
  
/* ルーレット状態 */
#define RLT_OFF     0 /* オフ   */
#define RLT_START   1 /* 回転中 */
#define RLT_STOP    2 /* 停止中 */
 
#define LED_OFFSET  7 /* LED 開始ピン番号 */
#define SOUND     700 /* 音の周波数 */
#define SOUND2   1500 /* 音の周波数 */
 
int button = 0;
int pre_button = 0;
int rlt_state = RLT_OFF;
int count = 0;              /* ルーレット回転中の点灯LED記録用 */
int stop_count = 0;         /* ルーレットSTOP中の回数記録用 */
int s1 = 0;  
int s2 = 100;  

/* 音 */
int keyA = 0;
int keyB = 0;
int keyC = 0;
int keyD = 0;
int keyE = 0;
int keyF = 0;
int keyG = 0;
 
int Do = 261;
int Re = 294;
int Mi = 329;
int Fa = 349;
int So = 392;
int La = 440;
int Si = 493;
int Do2 = 523;
int Re2 = 587;
int Mi2 = 659;
int Fa2 = 698;
int So2 = 784;
int La2 = 880;
int Si2 = 988;
int Do3 = 1047;
int Re3 = 1175;
int Mi3 = 1319;
int Fa3 = 1397;
int So3 = 1568;
 
int oneup[6] = { Mi2, So2, Mi3, Do3, Re3, So3};
int mario[13] = {Mi2, Mi2, 0, Mi2, 0, Do2, Mi2, 0, So2, 0, 0, 0, So};
int dead[9] = {Si, Fa2, 0, Fa2, Fa2, Mi2, 0, Re2, Do2};
//int fire[3] = {So, So2, So3};
//int damage[18] = {La2, Mi, La, 0, 0, 0, La2, Mi, La, 0, 0, 0, La2, Mi, La, 0, 0, 0};
//int star[32] = {Do2, 0, Do2, 0, Do2, 0, Re, Do2, 0, Do2, 0, Re, Do2, 0, Do2, 0,
//                Si , 0, Si, 0, Si, 0, Do, Si, 0, Si, 0, Do, Si, 0, Si, 0
//               };
               

void setup() {

  strip.begin();
  strip.show(); // Initialize all strip to 'off
  
  pinMode( 2, INPUT );        /* ボタン入力 */
  pinMode( 13, OUTPUT );        /* スピーカー */

  Serial.begin(9600); 
   
}
 
void loop() {
  
  button = digitalRead( 2 );
 
  if ( button != pre_button) {  /* ボタン入力 */
    if ( button == HIGH && rlt_state == RLT_OFF) {    /* 開始ボタンが押された */
 
      rlt_state = RLT_START;
      s1=0;
      s2=100;
 
    } else if (button == HIGH && rlt_state == RLT_START) { /* ストップボタンが押された */
      rlt_state = RLT_STOP;
      stop_count = 0;
    }
  }

 /////ルーレット操作///////
  switch ( rlt_state ) {    
    case RLT_OFF:
      break;
 
    //スイッチ1回オン 回転開始//
    case RLT_START:

   if (s1==0){
      for (int i = 0; i < 13; i++) {
      tone(13, mario[i], 50);
      delay(100);
   }    
     for(int j = 0; j <=16; j++) {
     strip.setPixelColor(j, strip.Color(0,0,30));
     strip.show();
     delay(20);}
     s1=1;
     }
     strip.setPixelColor(count, strip.Color(30,0,0));
     strip.show();
      
     tone(13,SOUND,20);                       
     delay(delay_time);

     strip.setPixelColor(count, strip.Color(0,0,30));
     strip.show();
      
     count++;
     if (count >= MAX_LED_NUM) {
        count = 0;
     }
      break;
 
   //スイッチ2回目 ストップ開始//
    case RLT_STOP:  

   if (s2==100){
      s2=count;     
   }
        
      strip.setPixelColor(s2, strip.Color(0,30,0)); //緑
      strip.setPixelColor(count, strip.Color(0,30,0)); 
      strip.show();
      tone(13,SOUND,20);
      delay(100 +  stop_count * 10);           //だんだん切り替わり時間を長く
      strip.setPixelColor(count, strip.Color(0,0,0));
      strip.show();
      count++;
      if (count >= MAX_LED_NUM) {
        count = 0;
      }
      stop_count++;                             
      if (stop_count >= MAX_LED_NUM ) {
        rlt_state = RLT_OFF;                    // 1周分光ったらルーレットを停止 */
        if (count == 8 ) {                    //8番目で止まったとき
            stop8();
         }else {                               //8番目以外で止まったとき
           stop9();
         }
        count = 0;
      }
       break;

     //その他//
    default:
        break;
  }
  pre_button = button;      /* 前回のボタン状態を記憶 */
  
}

////8番目以外で止まったとき/////
void  stop9(){
       
        for ( int i = 0; i < 7; i++) {          // 回転終了、7回点灯 
          strip.setPixelColor(count, strip.Color(0,30,0));
          strip.setPixelColor(8, strip.Color(100,0,0));
          strip.show();
          
          tone(13,SOUND2,20);
          delay(100);
          //strip.setPixelColor(count, strip.Color(0,0,0));
          strip.setPixelColor(8, strip.Color(0,0,0));
          strip.show();
          
          delay(100);     
        }

          strip.setPixelColor(count, strip.Color(0,0,0));
          strip.show();

    for (int i = 0; i < 9; i++) {
      tone(13, dead[i], 100);
      delay(150);
    }    
}
        

////8番目で止まったとき/////
void  stop8(){
    for ( int i = 0; i < 7; i++) {          // 回転終了、7回点灯 
      strip.setPixelColor(count, strip.Color(0,30,0));
      strip.show();          
      tone(13,SOUND2,20);
      delay(100);
      strip.setPixelColor(count, strip.Color(0,0,0));
      strip.show();          
      delay(100);          
    }
            
    for(int j = 0; j <=8; j++) {
      strip.setPixelColor(j+7, strip.Color(0,0,30));
      strip.setPixelColor(8-j, strip.Color(0,0,30));
      strip.show();
      delay(50);
    }

    for(int j = 0; j <=8; j++) {
      strip.setPixelColor(j, strip.Color(30,30,0));
      strip.setPixelColor(16-j, strip.Color(30,30,0));
      strip.show();
      delay(50);
    }

    for(int j = 0; j <=8; j++) {
      strip.setPixelColor(j+7, strip.Color(0,30,0));
      strip.setPixelColor(8-j, strip.Color(0,30,0));
      strip.show();
      delay(50);
    }

    for(int j = 0; j <=8; j++) {
      strip.setPixelColor(j, strip.Color(30,0,30));
      strip.setPixelColor(16-j, strip.Color(30,0,30));
      strip.show();
      delay(50);
    }
       
    for(int j = 0; j <=8; j++) {
      strip.setPixelColor(j+7, strip.Color(30,0,0));
      strip.setPixelColor(8-j, strip.Color(30,0,0));
      strip.show();
      delay(50);
    }

    for(int j = 0; j <=8; j++) {
      strip.setPixelColor(j, strip.Color(0,30,30));
      strip.setPixelColor(16-j, strip.Color(0,30,30));
      strip.show();
      delay(50);
    }

    for (int i = 0; i < 6; i++) {
      tone(13, oneup[i], 100);
      delay(100);
    }    
    for (int i = 0; i < 6; i++) {
      tone(13, oneup[i], 100);
      delay(100);
    }    
    for (int i = 0; i < 6; i++) {
      tone(13, oneup[i], 100);
      delay(100);
    }
    for(int j = 0; j <=8; j++) {
      strip.setPixelColor(j+7, strip.Color(0,0,0));
      strip.setPixelColor(8-j, strip.Color(0,0,0));
      strip.show();
      delay(50);}
    }