Go Down

Topic: Support for IR Proximity Sensor without Delay() (Read 1 time) previous topic - next topic

ClncyFshSlayer

Hopefully what I am looking to do it possible to code.

I have a code I have been plugging away at for a couple of weeks. I finally got it dialed in to do exactly what I wanted and scaled it up, not thinking about a portion of the code using a delay function. Below is the particular snippet that is causing problems.

Code: [Select]

//IR Sensor for Pod 1
int readIR1(int times1){
  for(int x=0;x<times1;x++){     
    digitalWrite(IRemitter1,LOW);           // turning the IR LEDs off to read the IR coming from the ambient
    delay(1);                                             // minimum delay necessary to read values
    ambientIR1 = analogRead(IRpin1);  // storing IR coming from the ambient
    digitalWrite(IRemitter1,HIGH);          // turning the IR LEDs on to read the IR coming from the obstacle
    delay(1);                                             // minimum delay necessary to read values
    obstacleIR1 = analogRead(IRpin1);  // storing IR coming from the obstacle
    value1[x] = ambientIR1-obstacleIR1;   // calculating changes in IR values and storing it for future average
  }
  for(int x=0;x<times1;x++){        // calculating the average based on the "accuracy"
    distance1+=value1[x];
  }
  return(distance1/times1);            // return the final value
}


With only 1 or 2 sensors, the delay is negligible and does not make a noticeable difference; however, when scaled up to 10 sensors, it causes a noticeable 20ms delay. The sensors dictate what pattern is displayed on individual RGB rings, so the delay makes it very choppy, hence the reason I am reaching out to try to resolve the issue with using delay.

Is there a way to rewrite the above code to omit the delay function?

Thanks in advance for the help, I've spent the last 6 or so hours researching and rewriting, but have not been able to solve it.


Also, if your interested, I have attached the code in it's entirety in case it helps to see what it is doing.

aarg

Impossible to answer without documentation, photos or links to the hardware.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

ClncyFshSlayer

The code is run on an Arduino Mega 2560, the LED rings are a standard WS2812 setup, and the IR sensors are TCRT5000 Reflective Optical Sensor with Transistor Output.

I currently have it written to run on a 60 LED strip and a 12 LED ring.

Strip: http://www.amazon.com/gp/product/B00XL449VS?psc=1&redirect=true&ref_=oh_aui_detailpage_o04_s00
 
Ring:  http://www.amazon.com/gp/product/B0105VMUUQ?psc=1&redirect=true&ref_=oh_aui_detailpage_o00_s00

Sensors:  http://www.amazon.com/gp/product/B008A3VP6M?psc=1&redirect=true&ref_=oh_aui_detailpage_o00_s00

aarg

Thanks. What happens when you reduce the 1ms delay time to less? Did you arrive at that number by a recommendation or experiment?

Also, are the sensors too close to enable them all at the same time?
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

ClncyFshSlayer

Yes to both. I started with a code someone else had built, and the 1ms delay is what was written in to that code. I did try to reduce the delay, but dropping the delay caused it to not function properly.

ClncyFshSlayer

And they will be spaced enough that they could all be run concurrently.

aarg

Yes to both. I started with a code someone else had built, and the 1ms delay is what was written in to that code. I did try to reduce the delay, but dropping the delay caused it to not function properly.
Can you be more specific? The reason I ask, is that it makes no sense. The analogRead() function takes time, but turning the emitters on doesn't. What time delay did you try and how did you implement it?

And - catching your update - you should sample them simultaneously if you can get away with it. That will be a 16x speed improvement.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

aarg

Your code needs factoring badly. To have the same 25 lines of code repeated 16 times, one for each sensor, is just not acceptable. You need to use arrays, so one function can handle all the sensors. Probably in much of the rest of the program as well. It will reduce the code size by about 10 times.

But crucially, it will make it humanly possible to implement the correct logic for the sensors.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

ClncyFshSlayer

Thanks for the input. I will try to pick up on the arrays and see if I can figure out how to bring them in to this sketch. I don't understand them all that well; especially having to use an array within an array for this application(?), but will do some more research then bring a cleaner code back in if I still can not get the delay() function dealt with.

Also, if you see a better solution, I am all ears!  I am pretty new to this, so I am still working on picking up some of the coding procedures.

aarg

  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

PaulS

Quote
Your code needs factoring badly.
It looked to me like it had been.
The art of getting good answers lies in asking good questions.

ClncyFshSlayer

You never answered to reply #6.
In response to reply #6. I tried changing the delays to 0.1 and 0.5, and at both of those timings, I was not getting a good reading. Values were very low and jumping from negative to positive. Essentially, having a shorter delay gives me crap input data that is unusable to run the sketch effectively.

PaulS

Quote
I tried changing the delays to 0.1 and 0.5
When delay() takes an integral value, it is pointless to pass it a value of less than 1.
The art of getting good answers lies in asking good questions.

Go Up