       
     }
     state = statePlay;
-    displayWrite("ripeti");
+    displayString("ripeti");
     currentPlayingSeqIndex = 0;
     
   }
   
-  if (state == statePlay) {
-    
-    
-    
+   if (state == statePlay) {
+   //if (state == 100) {
+     
+      if (buttonStates[0] == buttonPressed) {
+        checkPressing(redButton);
+        buttonStates[0] = buttonUnknow;
+      } else if (buttonStates[1] == buttonPressed) {
+        checkPressing(greenButton);
+        buttonStates[1] = buttonUnknow;
+      } else if (buttonStates[2] == buttonPressed) {
+        checkPressing(yellowButton);
+        buttonStates[2] = buttonUnknow;
+      } else if (buttonStates[3] == buttonPressed) {
+        checkPressing(blueButton);
+        buttonStates[3] = buttonUnknow;
+      } 
+  }
+  
+  if (state == stateGameOver) {
+    displayString("sbagliato");
+    delay(1000);
+    displayStringInt("punti ", level);
+    state = stateEnd;
+  }
+  
+       //manage pressing
+      computeButtonState(0, redButton, redLed, NOTE_C4);  
+      computeButtonState(1, greenButton, greenLed, NOTE_D4);  
+      computeButtonState(2, yellowButton, yellowLed, NOTE_E4);  
+      computeButtonState(3, blueButton, blueLed, NOTE_F4);
+    
+      pressingFeedback(false);
       
-  //    stateRedButton = analogRead(A0);
-//      stateRedButton = analogRead(A0);
-  //    stateRedButton = analogRead(A0);
-    //  stateRedButton = analogRead(A0);
-  }
-  
-//  Serial.println(analogRead(A0));
-  /*
-  if (pressed(redButton) && (state == statePlay)) {
-    checkPressing(redLed, NOTE_C4, redButton); 
-  } else if (pressed(greenButton) && (state == statePlay)) {
-    checkPressing(greenLed, NOTE_D4, greenButton);
-  } else if (pressed(yellowButton) && (state == statePlay)) {
-    checkPressing(yellowLed, NOTE_E4, yellowButton);
-  } else if (pressed(blueButton) && (state == statePlay)) {
-    checkPressing(blueLed, NOTE_F4, blueButton);
-  }
-  */
-
-}
-
-void computeButtonStates(int index, int button, int led) {
-      int tmpVal = analogRead(button);
-      delay(20);
-      if (tmpVal > 900) {
-        buttonStates[index] = buttonDown;
-        digitalWrite(led, HIGH);
-      } else {
-        if (buttonStates[index] == buttonDown) {
-          //Serial.print(index);
-          //Serial.println("pressed");
-          buttonStates[index] = buttonPressed;
-          digitalWrite(led, LOW);
-        }
+}
+
+void pressingFeedback(boolean waitForButtonEnd) {
+    if (waitForButtonEnd && buttonFeedBackStartMillis > 0 && millis() - buttonFeedBackStartMillis < 1000) {
+      delay(1000 - (millis() - buttonFeedBackStartMillis));
+      noTone(tonePin);
+        buttonFeedBackStartMillis = 0;
+        //allLedOff();
+    } else {
+      if (buttonFeedBackStartMillis > 0 && millis() > 1000 + buttonFeedBackStartMillis) {
+        noTone(tonePin);
+        buttonFeedBackStartMillis = 0;
+        //allLedOff();
       }
-
-  
-}
-
-void checkPressing(int led, unsigned int note, int button) {
-        digitalWrite(led, HIGH);
-         tone(tonePin, note);
-       
-       if (sequence[currentPlayingSeqIndex] == button) {
-        
-         displayWrite("OK !");
-         currentPlayingSeqIndex ++;
-         if (currentPlayingSeqIndex >= level) {
-           state = stateNewLevel;
-         } 
-       } else {
-         displayWrite("sbagliato");
-         state = stateEnd;
-       }
-          delay(1000);
-          noTone(tonePin);
-          allLedOff();
-
-}
-
-boolean pressed(int button) {
-  return analogRead(button) > 900;
-}
-
+    }
+}
+
+void displayStringInt(char * str, int i) {
+    display.setTextSize(2);
+    display.setTextColor(WHITE);
+    display.setCursor(0,0);
+    display.clearDisplay();
+    display.print(str);
+    display.print(i);
+    display.display();
+}
+
+void displayString(char * str) {
+  display.setTextSize(2);
+  display.setTextColor(WHITE);
+  display.setCursor(0,0);
+  display.clearDisplay();
+  display.println(str);
+  display.display();
+}
 
 void allLedOff() {
   digitalWrite(redLed, LOW);

}


void displayWrite(String text) {
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print(text);
  display.display();
}


int state; 
int sequence[maxLength];
int currentPlayingSeqIndex;
int buttonStates[4]; 
unsigned long buttonFeedBackStartMillis;

void setup() {
  Serial.begin(9600);

  display.begin(SSD1306_SWITCHCAPVCC);
  
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(yellowLed, OUTPUT);
