Measuring speed using phototransistors in Arduino

  • Thread starter HHOboy
  • Start date
  • #1
32
0

Main Question or Discussion Point

Hey guys, I am working on a project where I am using two photo-transistors to detect the speed of a projectile. I am trying to measure the speed of a paintball but I am getting mixed results. When a slow moving object passes through the sensors the correct speed is displayed but extremely fast objects can sometimes trick it.

Here is the sketch... It works but if you know any ways to make it more efficient please let me know, Thanks for your help!

FYI: The two sensors are 4 inches apart that is where the constant "333333" comes from the distance between the sensors in the FPS calculation

C:
int firstsens = 4;
int secondsens = 5;
unsigned long time, time2;
float fps, elap;
int val;
int val2;

void setup()
{
Serial.begin(9600);
pinMode (firstsens, INPUT);
pinMode (secondsens, INPUT);
}
void loop()
{
Serial.println("Waiting for projectile...");
val = analogRead(firstsens);
val2 = analogRead(secondsens);
while (val > 1)
{
  val = analogRead(firstsens);
}

while (val <= 1)
{
time = micros();
val = analogRead(firstsens);
}

while (val2 > 1)
{
  val2 = analogRead(secondsens);
}

while (val2 <= 1)
{
time2 = micros();
val2 = analogRead(secondsens);
}
elap = time2 - time;

fps = 333333/elap;
Serial.println(fps );
}
 
Last edited by a moderator:

Answers and Replies

Related Threads for: Measuring speed using phototransistors in Arduino

  • Last Post
Replies
8
Views
630
  • Last Post
Replies
4
Views
1K
  • Last Post
Replies
2
Views
936
  • Last Post
Replies
6
Views
1K
  • Last Post
Replies
3
Views
799
  • Last Post
Replies
2
Views
2K
Replies
7
Views
5K
Top