Stopwatch with more than one Runner

Good Evening,
I would need some help regarding my Code, where i wanna Stop a time of runner with a photocell (=Lichtschranke). I have already made that work, but now I wan to stop the time of multible runners, but I just dont get how :expressionless:

I thought it would be easy, but .....

I Start the Time by pressing a Button, an stopping it by a photocell (Lichtschranke)

I will be very thankfull if one of you could help me.

Thanks you in advance

Jonah Osterried

ich bräuchte Hilfe bei meinem Code, indem ich mit meiner Lichtschranke die Zeit von mehreren Läufern stoppen will.

Ich habe es geschafft von einem Läufer sie Zeit zu stoppen allerdings nicht von mehrern :expressionless:

Ich dachte eigentlich es wäre einfach jedoch zerbrech ich mir gerade den Kopf.

Die Zeit Starte ich mit mit einem Knopf, und stoppe die mit einer Laserichtschranke, welche ich selbst gabaut habe. (mit einer Photoresistor....)

Ich wäre seehr dankbar wenn jemand mir helfen will.

Danke ich vorraus.

//set pin numbers
//const won't change
const int ledPin = A1;   
const int redPin = A3;  
const int greenPin = A2;   
const int ldrPin = A0;  //Photoresistor
const int buttonPin = 7; 
const int buzzerPin = A4;
const int sirene = A5; //warning
const int laser = 8;

int value = LOW;                    // previous value of the LED

int buttonState;                    // variable to store button state

int lastButtonState;                // variable to store last button state

int blinking;                       // condition for blinking - timer is timing

long interval = 100;                // blink interval - change to suit

long previousMillis = 0;            // variable to store last time LED was updated

long startTime ;                    // start time for stop watch

long elapsedTime ;                  // elapsed time for stop watch

int fractional;                     // variable used to store fractional part of time










void setup()

{

   Serial.begin(115200);



   pinMode(ledPin, OUTPUT);         // sets the digital pin as output
   
   pinMode(redPin, OUTPUT);         
   
   pinMode(greenPin, OUTPUT);        



   pinMode(buttonPin, INPUT);       

   digitalWrite(buttonPin, HIGH); 

   pinMode(ldrPin, INPUT);       
   
   pinMode(buzzerPin, OUTPUT);
   
   pinMode(sirene, OUTPUT);
   
   pinMode(laser, OUTPUT);


}




void Startvorgang100m(){
  
  int x = random(1500, 5000);

      digitalWrite(ledPin, HIGH);
      delay(50);
      digitalWrite(ledPin, LOW);
      delay(50);
      digitalWrite(ledPin, HIGH);
      delay(50);
      digitalWrite(ledPin, LOW);
      
      digitalWrite(redPin, HIGH);
      delay(3000);
      digitalWrite(redPin, LOW);
      digitalWrite(buzzerPin, HIGH);
      delay(100);
      digitalWrite(buzzerPin, LOW);
      delay(100);
      digitalWrite(buzzerPin, HIGH);
      digitalWrite(redPin, HIGH);
      delay(100);
      digitalWrite(buzzerPin, LOW);
      digitalWrite(redPin, LOW);
      delay(1000);
      digitalWrite(buzzerPin, HIGH);
      digitalWrite(redPin, HIGH);
      delay(75);
      digitalWrite(buzzerPin, LOW);
      digitalWrite(redPin, LOW);
      delay(75);
      digitalWrite(buzzerPin, HIGH);
      digitalWrite(redPin, HIGH);
      delay(75);
      digitalWrite(buzzerPin, LOW);
      digitalWrite(redPin, LOW);
      delay(75);
      digitalWrite(buzzerPin, HIGH);
      digitalWrite(redPin, HIGH);
      delay(75);
      digitalWrite(buzzerPin, LOW);
      digitalWrite(redPin, LOW);
      delay(75);
      digitalWrite(buzzerPin, HIGH);
      digitalWrite(redPin, HIGH);
      delay(75);
      digitalWrite(buzzerPin, LOW);
      digitalWrite(redPin, LOW);
      Serial.println(x);
      delay(x);
      digitalWrite(buzzerPin, HIGH);
      delay(291);
      digitalWrite(greenPin, HIGH);
}

void messen100m(){
  
     buttonState = digitalRead(buttonPin);                   // read the button state and store

   int ldrStatus = analogRead(ldrPin);   //read the status of the LDR value
   
   
  if (buttonState == LOW && lastButtonState == HIGH  &&  blinking == false && ldrStatus >=800){     // check for a high to low transition

      // if true then found a new button press while clock is not running - start the clock

      Startvorgang100m();
      
      Serial.println("START");

      startTime = millis();                                   // store the start time

      blinking = true;                                     // turn on blinking while timing

      delay(5);                                               // short delay to debounce switch

      lastButtonState = buttonState;                          // store buttonState in lastButtonState, to compare next time
      
      delay(700);
      digitalWrite(buzzerPin, LOW);

   }



   else if (ldrStatus <=900 && blinking == true){     // check for a high to low transition

      // if true then found a new button press while clock is running - stop the clock and report
      
      elapsedTime =   millis() - startTime;
      
      if (elapsedTime >=10000){
        

        elapsedTime =   millis() - startTime;              // store elapsed time

        blinking = false;  
        digitalWrite(buzzerPin, HIGH);
        delay(100);
        digitalWrite(buzzerPin, LOW);
        digitalWrite(greenPin, LOW);

        lastButtonState = buttonState;                     // store buttonState in lastButtonState, to compare next time



       // routine to report elapsed time 

        Serial.print( (int)(elapsedTime / 1000L));         // divide by 1000 to convert to seconds - then cast to an int to print



        Serial.print(".");                             // print decimal point



        // use modulo operator to get fractional part of time 

       fractional = (int)(elapsedTime % 1000L);



       // pad in leading zeros - wouldn't it be nice if 

       // Arduino language had a flag for this? :)

       if (fractional == 0)

          Serial.print("000");      // add three zero's

       else if (fractional < 10)    // if fractional < 10 the 0 is ignored giving a wrong time, so add the zeros

          Serial.print("00");       // add two zeros

       else if (fractional < 100)

          Serial.print("0");        // add one zero



       Serial.println(fractional);  // print fractional part of time 

   } else if (elapsedTime <=10000 && ldrStatus <=900 && blinking == true){
     digitalWrite(sirene, HIGH);
     delay(2000);
     digitalWrite(sirene, LOW);
     
   }

   }



   else{

      lastButtonState = buttonState;                         // store buttonState in lastButtonState, to compare next time

   }



   // blink routine - blink the LED while timing

   // check to see if it's time to blink the LED; that is, the difference

   // between the current time and last time we blinked the LED is larger than

   // the interval at which we want to blink the LED.



   if ( (millis() - previousMillis > interval) ) {



      if (blinking == true){

         previousMillis = millis();                         // remember the last time we blinked the LED



         // if the LED is off turn it on and vice-versa.

         if (value == LOW)

            value = HIGH;

         else

            value = LOW;

         digitalWrite(ledPin, value);

      }

      else{

         digitalWrite(ledPin, LOW);                         // turn off LED when not blinking

      }

   }

}

void hallo(){

  int ldrStatus = analogRead(ldrPin);
  Serial.println(ldrStatus);
  

}


void loop()

{

   digitalWrite(laser, HIGH);
   messen100m();
   
   


}

I see there is talk about arrays in your other posts about this. In general, once you get something like this working for one input it can be expanded to more inputs by putting your data in arrays and looping through them. So, for example, instead of reading input ldrPin you would read ldrPin[0], ldrPin[1] ... ldrPin[N] in a loop for N LDR pins.

My recommendation is to try out this technique in a new, separate program before trying to incorporate it into what you have.

I wrote a small demo, don't know if it will work in your hardware setup but maybe it can give you some ideas to more forward. The data for runner lanes, consisting of a LDR pin number and a finish time are stored in an array of structs. Each time through loop() each LDR is examined until all runners have completed the race.

const byte NUM_LANES = 3;   // total # of lanes

unsigned long startTime;    // time of starting gun

// data structure for lane info
typedef struct lane  {
  int ldrPin;  //Photoresistor
  unsigned long finishTime;
};

// lane definitions
lane lanes[NUM_LANES] = {{A0, 0UL}, {A1, 0UL}, {A2, 0UL}};

byte numFinishers = 0;      // count of racers who have finished

void setup() {

  for ( int idx = 0; idx < NUM_LANES; idx++) pinMode(lanes[idx].ldrPin, INPUT);

  startTime = millis();     // starting gun
}

void loop() {

  // if racers still on track
  if ( numFinishers < NUM_LANES )
  {
    // look at each lane
    for (int idx = 0; idx < NUM_LANES; idx++)
    {

      // if runner has not already finished and has crossed the line
      if ( lanes[idx].finishTime ==  0UL && analogRead(lanes[idx].ldrPin) <= 900 )
      {

        // mark finish time
        lanes[idx].finishTime = millis();

        // increment count
        numFinishers++;

      } // if

    } // for

    // if all racers done
    if ( numFinishers == NUM_LANES )
    {
      // print times
      for (int laneIdx = 0; laneIdx < NUM_LANES; laneIdx++)
      {
        Serial.print("Lane ");
        Serial.print(laneIdx + 1);
        Serial.print(" time:  ");
        Serial.println(lanes[laneIdx].finishTime - startTime);

      } // for

    } // if

  } // if


}