IR Sensor circuit

Hi there, I am new to arduino and I am building a timer circuit from one found online. It starts a timer when it detects an object on the first sensor and stops the timer when it detects an object on the second sensor. Everything completely works but I have no idea what each line of the code is actually doing and how it works. If anyone was able to explain how the code operates it would be greatly appreciated. It's just a short code but complex for a newbie. I will post it below. Thanks

unsigned long t1=0;
unsigned long t2=0;
void sens1() { if ((t1==0) && (t2==0)) t1=micros(); }
void sens2() { if ((t2==0) && (t1!=0)) t2=micros(); }

void setup() {
pinMode(2,INPUT);
pinMode(3,INPUT);
pinMode(13,OUTPUT);
attachInterrupt(0,sens1,RISING);
attachInterrupt(1,sens2,RISING);
//Serial.begin(115200);
Serial.begin(115200);
}

void loop() {
digitalWrite(13,t1?HIGH:LOW);
if (t2>0)
{
Serial.println(t2-t1);
t1=t2=0;
}
}

Hi @arduino235.

unsigned long t1 = 0;       // Creates variable t1 format unsignel length long and assigns the value 0;
unsigned long t2 = 0;       // Creates variable t2 format unsignel length and assigns the value 0;
//-------------------------------------------------------------------
void sens1() {                                         // Routine called by interrupt from port 2
  if ((t1 == 0) && (t2 == 0)) t1 = micros();           // Make t1 equal to the value returned by the micros function if t2 equals 0 "and" t1 equals 0
}
//-------------------------------------------------------------------
void sens2() {                                         // Routine called by interrupt from port 2
  if ((t2 == 0) && (t1 != 0)) t2 = micros();           // Make t2 equal to the value returned by the micros function if t2 equals 0 "and" t1 not 0
}
//-------------------------------------------------------------------
void setup() {                                         // Routine that runs a single time when starting the arduino
  pinMode(2, INPUT);                                   // Set pin 2 as input
  pinMode(3, INPUT);                                   // Set pin 3 as input
  pinMode(13, OUTPUT);                                 // Set pin 13 as output
  attachInterrupt(0, sens1, RISING);                   // Attach interrupt 0 to raise pin2 and call routine sens1()
  attachInterrupt(1, sens2, RISING);                   // Attach interrupt 1 to the rise of pin1 and call routine sens2()
  Serial.begin(115200);                                // Initialize the serial at 115200 bps
}
//-------------------------------------------------------------------
void loop() {                                         // Routine that runs all the time
  digitalWrite(13, t1 ? HIGH : LOW);                  // If t1 is different from 0 make pin 13 HIGH, if it is equal to 0 make pin 13 LOW
  if (t2 > 0)                                         // If t2 is greater than 0
  {
    Serial.println(t2 - t1);                          // Print result of t2 - t1
    t1 = t2 = 0;                                      // And make t1 and t2 equal to 0
  }
}
1 Like

Thank you very much this is very useful