int i;         // Zeit die auf das Delay addiert wird
int start;     // Start
int deadend;   // Ende
int rb1;
int rb2;
int rb3;
int rb4;
int rb5;

void setup() {
  // RGB-Pins werden als OUTPUT definiert
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);

  // Übertragungsrate an Serial Monitor
  Serial.begin(9600);
}

void loop() {
  // Instruktionen und Delay werden ausgegeben
  Serial.println("Press once to start the test");
  Serial.print("Delay between 4000ms and ");
  Serial.print(9000 + i);
  Serial.println("ms");

  // LED wird blau
  analogWrite(3, 0);
  analogWrite(5, 0);
  analogWrite(6, 255);

  // Sobald der User drückt, wird der Test gestartet
  while (digitalRead(A0) == 0) {}
  Serial.println("Test has been started");

  // LED wird rot -> test startet
  analogWrite(3, 255);
  analogWrite(5, 0);
  analogWrite(6, 0);

// Zufälliges Delay zwischen 4s und 9s + i
  delay(random(4000, 9000 + i));

// LED wird grün
  analogWrite(3, 0);
  analogWrite(5, 255);
  analogWrite(6, 0);

// Zeit vom Programmstart bis jetzt wird gemessen und als "start" gesaved
  start = millis();

// Programm wartet darauf, bis gedrückt wird
  while (digitalRead(A0) == 0) {}

// Zeit vom Programmstart bis jetzt wird gemessen und als "deadend" gesaved
  deadend = millis();

// LED wird wieder rot
  analogWrite(3, 255);
  analogWrite(5, 0);
  analogWrite(6, 0);

// Ausgabe der Reaktionszeit des Users
  if (deadend - start >= 20 && deadend - start < 100 )
  {
    Serial.print("Reaction time: ");
    Serial.print ( deadend - start);
    Serial.print("ms. ");
    Serial.println("i dont believe u wtf");
    Serial.println();

  }

  else if(deadend - start >= 100 && deadend - start < 200 )
  {
    Serial.print("Reaction time: ");
    Serial.print ( deadend - start);
    Serial.print("ms. ");
    Serial.println("That's an impressive reaction time! Congrats.");
    Serial.println();
         for(int rainbow=1; rainbow<=3;rainbow++) 
      {for(int rb1 = 0; rb1 <= 255; rb1++)
     {
      analogWrite(3, 255);
      analogWrite(5, rb1);
      analogWrite(6, 0);
      delay(1);
     }
     for(int rb2 = 255; rb2 >=0 ; rb2--)
    {
      analogWrite(3, rb2);
      analogWrite(5, 255);
      analogWrite(6, 0);
      delay(1);
    }
   for(int rb3 = 0; rb3 <= 255; rb3++)
    {
      analogWrite(3, 0);
      analogWrite(5, 255);
      analogWrite(6, rb3);
      delay(1);
    }
  for(int rb4 = 255; rb4 >=0 ; rb4--)
   {
      analogWrite(3, 0);
      analogWrite(5, rb4);
      analogWrite(6, 255);
      delay(1);
   }
  for(int rb5 = 0; rb5 <= 255; rb5++)
   {
      analogWrite(3, rb5);
      analogWrite(5, 0);
      analogWrite(6, 255);
      delay(1);
    }
  for(int rb6 = 255; rb6 >=0 ; rb6--)
    {
      analogWrite(3, 255);
      analogWrite(5, 0);
      analogWrite(6, rb6);
      delay(1);
    }   
  }
  }

  else if(deadend - start >= 200 && deadend - start < 300 )
  {
    Serial.print("Reaction time: ");
    Serial.print ( deadend - start);
    Serial.print("ms. ");
    Serial.println("That's a decent reaction time.");
    Serial.println();
  }

  else if(deadend - start >= 300 && deadend - start < 400 )
  {
    Serial.print("Reaction time: ");
    Serial.print ( deadend - start);
    Serial.print("ms. ");
    Serial.println("That's a solid reaction time but I'm sure you can do better. ");
    Serial.println();
  }


  else if(deadend - start >= 400 && deadend - start < 500)
  {
    Serial.print("Reaction time: ");
    Serial.print ( deadend - start);
    Serial.print("ms. ");
    Serial.println("That's a prety bad reaction time. Try again");
    Serial.println();
  }

  else if(deadend - start >= 500)
  {
    Serial.print("Reaction time: ");
    Serial.print ( deadend - start);
    Serial.print("ms. ");
    Serial.println("You're too bad for this test, just stop trying.");
    Serial.println();
      

  }
// Wenn der User eine Zeit unter 30ms hat, ist er ein Cheater
  else if (deadend-start < 20)
  {
    Serial.print("You're a cheater because a reaction time of ");
    Serial.print(deadend-start);
    Serial.println("ms isn't human");
    Serial.println();
    for(int cheatlight=0; cheatlight<=5; cheatlight++)
    {
    analogWrite(3, 0);
    analogWrite(5, 0);
    analogWrite(6, 255);
    delay(400);
    analogWrite(3, 255);
    analogWrite(5, 0);
    analogWrite(6, 0);
    delay(400);
    }
} 

// Erweiterung des Delays um 1s pro Test
  i = i + 1000;

// Wenn das Delay zwischen 4s und 20s (9s +11s) ist, wird es auf 4s und 9s resetted
  if (i > 11000)
  {
    i = 0;
  }
delay(500);

}