Nomothumu 0.9, Time sensor for sport use

part 2

             if ((int)(lastlap /60000) >= 1 && (int)(lastlap /60000) < 10 )     //if time > 1 minute but < 10 minutes show as 1.23.45
                   {
// to do
// time > 1 minute not displayed correctly
// change lastlap from int into long!!! and check result

                         m = (int)(lastlap / 60000);
                         n = (int)((lastlap - (m*60000)) / 10000L);
                         o = (int)(((lastlap - (m*60000)) / 1000L) % 10L);

                      digitalWrite(latchPin, LOW);                                  // enable writing to display
                      shiftOut(dataPin, clockPin, LSBFIRST, cijfertwee[m]);         // 
                      shiftOut(dataPin, clockPin, LSBFIRST, cijfer[n]);             // 
                      shiftOut(dataPin, clockPin, LSBFIRST, cijfertwee[i]);         //
                      shiftOut(dataPin, clockPin, LSBFIRST, cijfer[j]);
                      shiftOut(dataPin, clockPin, LSBFIRST, cijfer[k]);                      
                      digitalWrite(latchPin, HIGH);                             // stop writing to display
          
                   }                  
  // to do
  //      if ((int)(lastlap /60000) >= 10)                                           //if time > 10 minutes show as 12.34.5
  
  
      digitalWrite(ledPin, LOW);                                          // Turn blinking LED off (so it won't stay light when button preseed when LED is on)
      digitalWrite(delayPin, HIGH);                                       // Light delay LED for 2 seconds
      delay(20);                                                        // Close beam for 2 seconds
      digitalWrite(delayPin, LOW);                                        // Turn off delay LED
 
}
  
  else
  
  {
  
  if (blinking == true && delaybuttonState == HIGH)                        // If time is running and delay button is pressed keep in this loop so no breaking of the beam is detected
  {
                      
             if ((int)(elapsedTime /60000) < 1)                              // if time < 1 minute show as 12.345
                   {                          
                      elapsedTime =   millis() - startTime;                  // store elapsed time                 
                      h = (int)((elapsedTime / 10000L) % 10L);               // store first digit of time in seconds      
                      i = (int)((elapsedTime / 1000L) % 10L);                // store second digit of time in seconds      
                          
                      digitalWrite(latchPin, LOW);                          // keep time running on display (only seconds)
                      shiftOut(dataPin, clockPin, LSBFIRST, cijfer[h]);     // first digit of seconds
                      shiftOut(dataPin, clockPin, LSBFIRST, cijfertwee[i]); // second digit of seconds including decimal point
                      shiftOut(dataPin, clockPin, LSBFIRST, B0);            // turn last 3 digits off
                      shiftOut(dataPin, clockPin, LSBFIRST, B0);
                      shiftOut(dataPin, clockPin, LSBFIRST, B0);                      
                      digitalWrite(latchPin, HIGH);
                   }
             if ((int)(elapsedTime /60000) >= 1 && (int)(elapsedTime /60000) < 10 )  //if time > 1 minute but < 10 minutes show as 1.23.45
                   {
           
           m = (int)(elapsedTime / 60000);
           n = (int)((elapsedTime - (m*60000)) / 10000L);
           o = (int)(((elapsedTime - (m*60000)) / 1000L) % 10L);

                      digitalWrite(latchPin, LOW);                                  // enable writing to display
                      shiftOut(dataPin, clockPin, LSBFIRST, cijfertwee[m]);         // 
                      shiftOut(dataPin, clockPin, LSBFIRST, cijfer[n]);             // 
                      shiftOut(dataPin, clockPin, LSBFIRST, cijfertwee[i]);         //
                      shiftOut(dataPin, clockPin, LSBFIRST, cijfer[j]);
                      shiftOut(dataPin, clockPin, LSBFIRST, cijfer[k]);                      
                      digitalWrite(latchPin, HIGH);                             // stop writing to display
          
                   }  
                   
  // to do
  //      if ((int)(lastlap /60000) >= 10)                                           //if time > 10 minutes show as 12.34.5
                   

    digitalWrite(ledPin, LOW);                                              // turn of blinking LED
    digitalWrite(delayPin, HIGH);                                           // Turn on delay LED
    delay (100);
    digitalWrite(delayPin, LOW);                                            // Turn off delay LED (short delay so when in loop LED looks on)
  }
    
   else if (blinking == false && delaybuttonState == HIGH)              // If time is not running and delay button is pressed keep in this loop so no breaking of the beam is detected
    {                                                               // don't update time
      
    digitalWrite(delayPin, HIGH);                                   // Turn on delay LED
    digitalWrite(ledPin, LOW);                                      // turn off LED when not blinking
    delay (50);                        
    digitalWrite(delayPin, LOW);                                    // Turn off delay LED (short delay so when in loop LED looks on)
    }
    
   else
    {

   if (buttonState == LOW && lastButtonState == HIGH  &&  blinking == false)      // if time is not running and beam is broken
   {     
      startTime = millis();                                   // store the start time
      blinking = true;                                        // turn on blinking while timing
      digitalWrite(delayPin, HIGH);                           // close beam for 2 seconds
      digitalWrite(ledPin, LOW);                              // blinking LED is off during 2 seconds
      delay(2000);                                            
      digitalWrite(delayPin, LOW);                            // turn off delay LED
      lastButtonState = buttonState;                          // store buttonState in lastButtonState, to compare next time

   }

   else if (buttonState == LOW && lastButtonState == HIGH && blinking == true)    // if time is running and beam is broken
     {     
     
     if (modeState == HIGH)                                   // modestate = high  -->> single mode so stop timing after braking the beam
      {
      elapsedTime =   millis() - startTime;                   // store elapsed time
      lastlap = elapsedTime;                                  // store elapsed time for last lap function
      lapcount++;                                             // add one lap to the count
      blinking = false;                                       // turn off blinking, all done timing
      lastButtonState = buttonState;                          // store buttonState in lastButtonState, to compare next time
                                                                                  // serial print for debugging use, leave out in final version
      Serial.print("Ronde ");                                 // serial print for lap (ronde) number
      Serial.print(lapcount);
      Serial.print(": ");
      }
      else if (modeState == LOW)                               // modestate = low  -->> continious mode so stop timing after braking the beam    
      {
      elapsedTime =   millis() - startTime;                   // store elapsed time
      startTime = millis();                                   // store the new start time since we start a new lap
      lastlap = elapsedTime;                                  // store elapsed time for last lap function
      lapcount++;                                             // add one lap to the count
      blinking = true;                                        // keep blinking on because we start a new lap  (not needed since it was alreaady on) laptime stays vivible for 2 seconds beacuase of delay further on
      lastButtonState = buttonState;                          // store buttonState in lastButtonState, to compare next time
      Serial.print("Ronde ");                                 // serial print for lap (ronde) number
      Serial.print(lapcount);
      Serial.print(": ");
      }

      Serial.print( (int)(elapsedTime / 1000L) );           // divide by 1000 to convert to seconds - then cast to an int to print
      Serial.print(".");                                    // print decimal point
      fractional = (int)(elapsedTime % 1000L);              // use modulo operator to get fractional part of time
      if (fractional < 100)                                 // if fractional < 100 the 0 is ignored giving a wrong time, so add the zero
      {
      Serial.print("0");                                    // add zero
      if (fractional < 10)                                  // if fractional > 10 both zero's are ignored so add antoher zero
      {
      Serial.print("0");                                    // add another zero  
      }
      } 
      Serial.println(fractional);                           // print fractional part of time