Laser chronograph project

Hello everyone! Iam working on a school project, a laser chronograph. Iam using two basic lasers and sensors with photodiodes that send digital output to Arduino. I've worked out a very basic code that you can see below, but it is not working. The principle of the device is simple, once the first laser is interrupted, the signal from the sensor records start time and once the second one is interrupted, the signal from the second sensor records endtime. The program then calculates the speed and sends it to serial monitor. But as you can see in this video https://youtu.be/Ek-97awx7VI, the program works.... well oddly. It sends the data as soon as the first sensor is interrupted, despite the fact it should wait for the interruption of the second sensor to calculate the time properly. Also, the formula for counting the speed is wrong, it should be (0.155 / (endTime - startTime) / 1000) and not (0.155 * (endTime - startTime) / 1000), 0,155 is the distance between sensors in meters. But with the multiplication, the serial monitor starts continuously writing out "inf". Do you have any idea of what am I doing wrong??? Further explanation of the device is below the code. Iam a big noob in electronics and even bigger in programming so any help or advice will be really velcome. :slight_smile:

Video of the project "working" with real time serial monitor output :

int sensor1 = 2; // first sensor connected to pin 2*
int sensor2 = 3; // second sensor connected to pin 3*
int laser1 = 4; // first laser connected to pin 4*
int laser2 = 5; // second laser connected to pin 5*

unsigned long startTime;
unsigned long endTime;
float speed;



void setup() {
  pinMode(sensor1, INPUT); // set sensor1 as input
  pinMode(sensor2, INPUT); // set sensor2 as input
  pinMode(laser1, OUTPUT); // set laser1 as output
  pinMode(laser2, OUTPUT); // set laser2 as output
 Serial.begin(9600);  
}

void loop() {
  digitalWrite(laser1, HIGH); // turn on first laser*
  digitalWrite(laser2, HIGH); // turn on second laser*
  
 while(digitalRead(sensor1) == HIGH) {
  } //while the sensor is active (nothing is happening) do nothing*

 while(digitalRead(sensor1) == LOW) { // wait for first sensor to be interrupted*
  startTime = millis();// record start time*
 }

while(digitalRead(sensor2) == HIGH) {
}//while the sensor is active (nothing is happening) do nothing*

while(digitalRead(sensor2) == LOW) { // wait for second sensor to be interrupted*
endTime = millis(); 
}// record end time*


speed = (0.155 * (endTime - startTime)) / 1000; //This counts the actual speed, the 0.155 value is the distance between the sensors in meters*


//if speed is bigger then 0 (which theoretically happends only when I actually move an object trough the two sensors)

if(speed > 0.01)  
 { Serial.print("Speed:"); //print out the speed to serial monitor*
  Serial.print(speed);
  Serial.println(" m/s"); } 

 }

Iam using a light sensor with 5 photodiodes soldered in series connected to it. The sensor is tuned with the little blue trimmer and when it detects light the diode lights up and it sends DO. Iam using visible light (instead of IR) and this unwieldy setup, because it is simpler do work with. But if I wanted to measure really fast objects like 6mm airsoft bullets flying at 120m/s is it fast enough to work??? What parts would you recommend for this type of job??? What other things would you recommned to use in this project, and what are the limitations of my homemade sensor with diodes?

Watching videos? No. Helpers do this on their free time, without compensation. We have a private life also.
It's Your job to extract useful information and present it here.

Posting schematics would be valuable.
Program documentation is always wanted but looks like being unknown by many members. Heard about flow charts?

Iam sorry if I misunderstood the purpose of this forum... Iam not forcing anyone to spend time with the answer. The video is mine tho, it is not some random dude on youtube, i recorded it so it is easier to understand the problem. I did not draw any schematic or flow chart because the video shows very clearly the whole setup with written labels.

That's a misjudgement.

1 Like

Hi @serth ,
Use counter to track the actions of your code.
Define the counted as byte and with the value 0.

By interrupting the first laser you increment the counter by 1.
When passing through the second laser you again increment the counter by 1.

By interrupting the first laser you put the value of 1 on the counter
When passing through the second laser you put the value of 2 on the counter.

Use an if to only print and calculate when the counter is 2, and after printing
printing, reset the counter to 0.

PS:
Try this code:

int sensor1 = 2; // first sensor connected to pin 2*
int sensor2 = 3; // second sensor connected to pin 3*
int laser1 = 4; // first laser connected to pin 4*
int laser2 = 5; // second laser connected to pin 5*

unsigned long startTime;
unsigned long endTime;
float speed;
byte cnt = 0;


void setup() {
  pinMode(sensor1, INPUT); // set sensor1 as input
  pinMode(sensor2, INPUT); // set sensor2 as input
  pinMode(laser1, OUTPUT); // set laser1 as output
  pinMode(laser2, OUTPUT); // set laser2 as output
  Serial.begin(9600);
}

void loop() {
  digitalWrite(laser1, HIGH); // turn on first laser*
  digitalWrite(laser2, HIGH); // turn on second laser*

  while (digitalRead(sensor1) == HIGH) {
  } //while the sensor is active (nothing is happening) do nothing*

  while (digitalRead(sensor1) == LOW) { // wait for first sensor to be interrupted*
    startTime = millis();// record start time*
    cnt = 1;
  }

  while (digitalRead(sensor2) == HIGH) {
  }//while the sensor is active (nothing is happening) do nothing*

  while (digitalRead(sensor2) == LOW) { // wait for second sensor to be interrupted*
    endTime = millis();
    cnt = 2;
  }// record end time*

  if (cnt == 2)
  {
    speed = (0.155 * (endTime - startTime)) / 1000; //This counts the actual speed, the 0.155 value is the distance between the sensors in meters*


    //if speed is bigger then 0 (which theoretically happends only when I actually move an object trough the two sensors)

    if (speed > 0.01)
    { Serial.print("Speed:"); //print out the speed to serial monitor*
      Serial.print(speed);
      Serial.println(" m/s");
    }
    cnt = 0;
  }
}

Thank you very much!! I'll definitelly try this

Not related to the problem, but the while loops for the ==LOW tests seem superfluous.

Hi,
it's true, but I didn't want to change the OP's code too much so as not to confuse him.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.