#define BTN_TOLERANCE 2
#define BTN_1_VALUE 662
#define BTN_2_VALUE 747
#define BTN_3_VALUE 796
#define BTN_4_VALUE 721
 
int btns[] = {0, 0, 0 ,0};
 
int spellCombo[] = {0, 0, 0, 0};
int spellComboIdx = 0;
 
void setup() {
  pinMode(2, INPUT);
  Serial.begin(9600);
}
 
void processSpell() {
  if (btns[0]==1) {
    spellCombo[spellComboIdx++] = 1;
  } else if (btns[1]==1) {
    spellCombo[spellComboIdx++] = 2;
  } else if (btns[2]==1) {
    spellCombo[spellComboIdx++] = 3;
  } else if (btns[3]==1) {
    spellCombo[spellComboIdx++] = 4;
  }
 
  Serial.print(spellCombo[0]);
  Serial.print(" | ");
  Serial.print(spellCombo[1]);
  Serial.print(" | ");
  Serial.print(spellCombo[2]);
  Serial.print(" | ");
  Serial.print(spellCombo[3]);
  Serial.println();
 
  if (spellComboIdx==4) {
     spellComboIdx = 0;
     spellCombo[0] = 0;
     spellCombo[1] = 0;
     spellCombo[2] = 0;
     spellCombo[3] = 0;
     Serial.println("Sort lancé !!");
  }
}
 
void checkButtons() {
  int value = analogRead(2);
  if (value <= (BTN_1_VALUE+BTN_TOLERANCE) && value >= (BTN_1_VALUE-BTN_TOLERANCE)) {
    if (btns[0] != 1) {
      btns[0] = 1;
      btns[1] = 0;
      btns[2] = 0;
      btns[3] = 0;
      processSpell();
    }
  } else if (value <= (BTN_2_VALUE+BTN_TOLERANCE) && value >= (BTN_2_VALUE-BTN_TOLERANCE)) {
    if (btns[1] != 1) {
      btns[0] = 0;
      btns[1] = 1;
      btns[2] = 0;
      btns[3] = 0;
      processSpell();
    }
  } else if (value <= (BTN_3_VALUE+BTN_TOLERANCE) && value >= (BTN_3_VALUE-BTN_TOLERANCE)) {
    if (btns[2] != 1) {
      btns[0] = 0;
      btns[1] = 0;
      btns[2] = 1;
      btns[3] = 0;
      processSpell();
    }
  } else if (value <= (BTN_4_VALUE+BTN_TOLERANCE) && value >= (BTN_4_VALUE-BTN_TOLERANCE) {
    if (btns[3] != 1) {
      btns[0] = 0;
      btns[1] = 0;
      btns[2] = 0;
      btns[3] = 1;
      processSpell();
    }
  } else {
     btns[0] = 0;
     btns[1] = 0;
     btns[2] = 0;
     btns[3] = 0;
  }
}
 
void loop() {
  checkButtons();
}