detect falling object with infra red (urgent)

I have to finish a project before tomorrow (atleast tomorrowo i will get my grade, even if i fale at that i still have to finish it for monday). It basically work's, i made something like

If i use my hand then it goes fine, however a falling object seem to go to fast to register always (atleast for the second ir setector).

I saw some really advanced code here for how it could be faster but that is way to complicated.
http://www.ladyada.net/learn/sensors/ir.html

Could someone please help!

#define IR1 A0
#define IR2 A1

#define POT A2

#define OPTO 2

int timeout = 1000; // timeout in ms
int targetDist; // distance between IR2 and target to photograph
int lastTargetDist;

float irSensorDist = 48; // distance in mm between the 2 censors

void setup() {
  pinMode(IR1, INPUT);
  pinMode(IR2, INPUT);
  pinMode(POT, INPUT);
  pinMode(OPTO, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  
  targetDist = analogRead(POT);
   if(abs(targetDist - lastTargetDist) >= 3) {
     Serial.print("targetDist: ");
     Serial.println(targetDist);
     lastTargetDist = targetDist;
  }
  
  /*
  Serial.print(analogRead(IR1));
  Serial.print("\t");
  Serial.println(analogRead(IR2));
  */
  // direction should be from A0 to A1
  // so start detecting at A0
  // when something is inbetween the emitter and detector it
  // writes HIGH
  if(digitalRead(IR1) == HIGH) {
    // something was inbetween
    // store the time to calculate speed later
    long detect = millis();
    // check for IR2 to detect something
    while(true) {
      if(millis()-detect > timeout) {
        Serial.println("time out");
        break;
      }
      if(digitalRead(IR2) == HIGH) {
        // calculate the speed
        int time = millis() - detect;
        int mmInMs = irSensorDist / time; // millimeters traveled in milliseconds
        // shoot when target is in front of camera
        int delayT = targetDist * mmInMs;
        delay(delayT);
        // trigger camera
        digitalWrite(OPTO, HIGH);
        delay(25);
        digitalWrite(OPTO, LOW);
        Serial.println("click");
        // delay again else it keeps saying click
        delay(1000);
        break;
      }
    }

    
  }
      
  
}

digitalRead() takes time. Most likely, by the time the first digitalRead() detects the object, millis() executes, and the while condition is evaluated, the object has passed before the second digitalRead() determines what pin on what port to look at.

I'd have the two IRs generate interrupts on pins 2 and 3. That way, you don't have to do anything if there is an eternity after the first interrupt is generated before the second one is, so that test can be disposed of.

In the ISRs, just set a flag and the time.

In loop(), check the two flags. When both are true, compute the time.

The LadyAda link is for a different kind of IR altogether.

I'd have the two IRs generate interrupts on pins 2 and 3. That way, you don't have to do anything if there is an eternity after the first interrupt is generated before the second one is, so that test can be disposed of.

Cause pin 2 and 3 will also be faster then analog? (i used analog for testing sensetivity). And why shoudn't i check for a timeout then?

In the ISRs, just set a flag and the time.

Sorry i don't follow you.

In loop(), check the two flags. When both are true, compute the time.

They will be both true cause it happens so fast?

I know the LadyAda is for a different thing but i thought it meight still could happen.

Also my thing is 8mm high and 4 mm wide, and falls by the speed of gravity, that is nothing compared to a bullet (except the size)...

Also when i let fall the object pass it then often it doesn't even report the timeout. Can it be that i need to change the resistors as well? I tested now to get it working on a descent distance, 7cm, so it works for bigger objects as well.

I'm not sure if using an ISR is necessary if you need to get it done quickly, but try looking up direct port access. It's many times faster than digitalRead.

I'm also questioning how you managed to get a good digitalread value on an analog signal, but that's great if it works.

What is ISR? First i thought it was a typing error for IR.

I'm also questioning how you managed to get a good digitalread value on an analog signal, but that's great if it works.

I think when using digital read on a analog signal that below 512 is 0 and above 512 is 1 (that's my guess).

I will look at direct port acces.

clankill3r:
What is ISR? First i thought it was a typing error for IR.

Interrupt Service Routine :slight_smile:

Take PaulS's approach.

But if you get into a panic, choose your falling object to be something that has significant height (so that it occludes each IR beam for longer) and don't hold it far above the first beam (so that it has minimum speed during the measurement).

Ideally you'll sort the problem and make it more robust, but if you can't achieve that then being able to show that it does in fact work (under ideal conditions) would be better than nothing.

clankill3r:
What is ISR? First i thought it was a typing error for IR.

It's a function that gets run when an "interrupt" happens. An interrupt is usually triggered by a pin change or a timer (in your case, it would be a pin change), and it just pauses whatever the code is currently doing and calls the function, then goes back to whatever it was doing.

I'm also questioning how you managed to get a good digitalread value on an analog signal, but that's great if it works.

I think when using digital read on a analog signal that below 512 is 0 and above 512 is 1 (that's my guess).

Nope. Generally, it will read HIGH if it's above about 2.7V, LOW if it's under about 0.3V, and whatever it wants between the two. For speed, I'd suggest getting something like an analog comparator that can take an analog signal and say "under" or "over" for a certain voltage.

I will look at direct port acces.

That will have the same problems as I just mentioned, but it will be faster. However, if the digitalRead'ing works somehow, direct port access will also.

What are you using for the IR emitters/detectors and how are they connected to the Arduino (in other words - what's the circuit diagram??).

Pete
P.S. Next time do your assignment earlier!

AND ... what are the "objects" that you actually use?

Pete

clankill3r:
If i use my hand then it goes fine, however a falling object seem to go to fast to register always (atleast for the second ir setector).

Explain to your instructor that you have chosen to use foam blocks (or feathers) for the test. :slight_smile:

Also this explains about interrupts:

According to that, in 1/20 of a second a ball will drop around 11 mm.

So, 50 mS to drop that distance. Since an ISR can activate in around 3 uS (16000 times as fast as that) you should easily be able to detect even very heavy objects, like elephants, falling.

It could be that your falling object is small and is not covering all the beam at the same time and light is leaking round the edges.

Try PulseIn();

Not always reliable iv found but works good for finding the time.

http://arduino.cc/en/Reference/PulseIn

yeah what type of sensor have you got set up, proximity or colour, if it detects between light and dark, and by light i mean CD shiney, then there is your problem. Also could you not use a photo light gate and drop the ball through it.

You shouldn't need to use analog reads. That will fundamentally slow it down. I did a post here about using an op-amp as a comparator. In fact that was about measuring light:

Use a simple circuit like that, and then a digital read (and preferably an interrupt). But a loop with a digital read should be OK at those slow speeds.

You should go with interrupts as PaulS suggested. They will give you the fastest response possible. They might seem a bit complicated but will help you out in the long run.

Sorry to sound like a smart aleck but speed of an object falling has no relation with the weight of object. Galileo showed this by throwing 2 balls of different weights from the Leaning Tower of Pisa :stuck_out_tongue:

That is in a vacuum, wind resistance is a factor for one!

Antzy:
Sorry to sound like a smart aleck but speed of an object falling has no relation with the weight of object. Galileo showed this by throwing 2 balls of different weights from the Leaning Tower of Pisa :stuck_out_tongue:

That's OK, I was expecting a comment. :slight_smile:

I was thinking of Monty Python where they were talking about "what also floats on water?".

"Lead".

"Small rocks".

But you have to admit that foam blocks fall more slowly than elephants. :wink:

It's a fair cop.

One of the first things I saw on the internet way back in 1995 was that the most asked question was what does the Witch say in Monty Python, and the second most asked question was what does it mean!