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
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
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();
}