 const int setPin = 8;
 /* End Pin Definitions*/
 
+/* Constants */
+const int mainMenuIndex = 0;
+const int settingsViewIndex = 1;
+const int chronoViewIndex = 2;
+const int resultsViewIndex = 3;
+/* End Constants */
+
 /* Variable Definitions */
 Chrono chrono(Chrono::MICROS);
 LightChrono measurementTimeout;

void setup() {
  Serial.begin(9600);   // Test
  Serial.println("Stopwatch");
  
  // Pin Modes
  pinMode(buzzerPin, OUTPUT);
  pinMode(laserPin, INPUT);
  pinMode(onPin, INPUT);
  
  pinMode(returnPin, INPUT);
  pinMode(prevPin, INPUT);
  pinMode(nextPin, INPUT);
  pinMode(setPin, INPUT);
  
  // Chrono initializations
  measurementTimeout.restart();
  chrono.stop();
  
  // Menu initializations
  views[0] = mainMenu;
  views[1] = settingsMenu;
  views[2] = chronoView;
  views[3] = resultsView;
}

void loop() {
  
  if (measurementTimeout.hasPassed(600)){
    digitalWrite(buzzerPin, LOW);
  }

  if (/*digitalRead(onPin) == HIGH &&*/ enabled) { // soll ueberhaupt gemessen werden?
    
    oldLaserState = laserState;
    laserState = digitalRead(laserPin);
        
    if (laserState == LOW && chrono.isRunning()) {    // zeit laeuft
      
      Serial.println(chrono.elapsed());
      
    } else if (laserState == HIGH && measurementTimeout.hasPassed(1500) && chrono.isRunning()) {    // zeit stoppen
      
      Serial.println("Lap");
      digitalWrite(buzzerPin, HIGH);
      
      
      logAndRestart();
      
    }
    else if (laserState == HIGH && !chrono.isRunning()) {   // first start
      chrono.restart();
      measurementTimeout.restart();
    }
    
  }

  if (measurementIndex >= maxMeasurements) {
    enabled = false;
    
    Serial.println("Gemessene Zeiten:");
    for (int i = 0; i < maxMeasurements; i++) {
      printTime(measuredTimes[i]);
    }
    measurementIndex++;
    delay(10000);
  }
  
}

void mainMenu() {
	Serial.println("RaceTimer");
	
	switch(menuIndex){
		case 0:
			Serial.println("Start");
			break;

    if (digitalRead(prevPin)) {
      maxMeasurements--;
    }

    if (digitalRead(returnPin)) {
      setView(mainMenuIndex);
    }
}

void setView(int index) {
  viewIndex = index
}

void chronoView() {

  if (measurementTimeout.hasPassed(600)){
    digitalWrite(buzzerPin, LOW);
  }

  if (/*digitalRead(onPin) == HIGH &&*/ enabled) { // soll ueberhaupt gemessen werden?

    oldLaserState = laserState;
    laserState = digitalRead(laserPin);

    if (laserState == LOW && chrono.isRunning()) {    // zeit laeuft

       printTime(chrono.elapsed());

    } else if (laserState == HIGH && measurementTimeout.hasPassed(1500) && chrono.isRunning()) {    // zeit stoppen

      Serial.println("Lap");
      digitalWrite(buzzerPin, HIGH);

      logAndRestart();

    }
    else if (laserState == HIGH && !chrono.isRunning()) {   // first start
      chrono.restart();
      measurementTimeout.restart();
    }

  }

  if (measurementIndex >= maxMeasurements) {
      enabled = false;
  }

}

void resultsView() {
	Serial.println("Results");

  for (int i = 0; i < maxMeasurements; i++) {
    printTime(measuredTimes[i]);
  }

  measurementIndex++;

}

void checkviewIndex() {
	if (digitalRead(nextPin)) {
		viewIndex++;
	}
	if (digitalRead(prevPin)) {
		viewIndex--;
	}
}

