Go Down

Topic: detect falling object with infra red (urgent) (Read 2 times) previous topic - next topic

clankill3r

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!

Code: [Select]
#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;
      }
    }

   
  }
     
 
}

PaulS

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.

clankill3r

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

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


Sorry i don't follow you.

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

WizenedEE

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.

clankill3r

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

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

KenshinPT


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


Interrupt Service Routine :)

PeterH

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.
I only provide help via the forum - please do not contact me for private consultancy.

WizenedEE


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

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

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.

el_supremo

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!

el_supremo

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

Pete

Nick Gammon


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. :)

Also this explains about interrupts:

http://gammon.com.au/interrupts

Nick Gammon

http://en.wikipedia.org/wiki/Equations_for_a_falling_body

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.

Grumpy_Mike

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.

technobabologist

Try PulseIn();

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

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

technobabologist

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.

Go Up