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.
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?