/* Control two light barriers, connected to pin A0 and A2 (arduino UNO) Indicator leds are connected to pin 12 and 13. Measure time in ms between to detections of the upper/lower light barrier. 27.12.2020 */ boolean dectUp = false; boolean dectLow = false; boolean timer = false; //timer running? int trigUp = 700; int trigLow = 950; unsigned long deltaT = 0; unsigned long t = 0; unsigned long deltaTU = 0; unsigned long tU = 0; void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); pinMode(12, OUTPUT); //indicator low pinMode(13, OUTPUT); //indicator high } void loop() { // read the input on analog pin 0: int valueLow = analogRead(A0); int valueUp = analogRead(A2); // print out the value you read: /*Serial.print(valueLow); Serial.print(" | oben: ") Serial.println(valueUp); */ //delay(100); // delay in between reads for stability if (valueUp < trigUp) { // upper sensor //Serial.println("Ready"); if (dectUp && timer == false) { //first light after dark and timer not running Serial.println("Rest"); Serial.println(millis() - t); t = millis(); //start timer timer = true; //timer on Serial.println("Start"); } digitalWrite(13, HIGH); dectUp = false; } else { // Dark UP //Serial.println("Detected"); digitalWrite(13, LOW); delay(5); // detection break dectUp = true; timer = false; //timer off } //Serial.print("time-complexity: "); //Serial.println(millis()-tU); //tU = millis(); if (valueLow < trigLow) { //lower sensor digitalWrite(12, HIGH); //Serial.println("Ready"); dectLow = false; } else { // Dark LOW //Serial.println("Detected"); if (!dectLow && timer) { //first dark after light deltaT = millis() - t; timer = false; //timer off Serial.print("Detected: "); Serial.println(deltaT); } digitalWrite(12, LOW); //delay(1); // detection break dectLow = true; } }