SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
User avatar
By TrollHammer
#147850
I was looking at my code and realized that the code for the Digital 1113 uses a lot of processing time, if I understand things correctly.

Currently, I have 4 digital IR sensors set up, each being read in series, and each writing a seperate value to a seperate variable. I still need to convert this into logical thresholds, but with the basic sample code for the 1113 sensors, this amounts to as much as 12ms of processing time for just the sensors. The machine's drivetrain could be moving the system at upwards of 5 feet per second, which results in 11/16 inch of delay between reads just for the sensors. I will also need to be reading other sensors and determining what the motors should do (kind of like a line follower, I guess). I need to figure out a way to shrink down the 3010us needed by each digital IR sensor with this code. Any ideas?

Here's the code I found:
Code: Select all
//Code for the QRE1113 Digital board
//Outputs via the serial terminal - Lower numbers mean more reflected
//3000 or more means nothing was reflected.

int QRE1113_Pin = 2; //connected to digital 2

void setup(){
  Serial.begin(9600);
}

void loop(){

  int QRE_Value = readQD();
  Serial.println(QRE_Value); 
//  Servo6.writeMicroseconds(QRE_Value);                          // move servo to value determined by digital sensor

}

int readQD(){
  //Returns value from the QRE1113 
  //Lower numbers mean more refleacive
  //More than 3000 means nothing was reflected.
  pinMode( QRE1113_Pin, OUTPUT );
  digitalWrite( QRE1113_Pin, HIGH );
  delayMicroseconds(10);
  pinMode( QRE1113_Pin, INPUT );

  long time = micros();

  //time how long the input is HIGH, but quit after 3ms as nothing happens after that
  while (digitalRead(QRE1113_Pin) == HIGH && micros() - time < 3000);
  int diff = micros() - time;

  return diff;
}
Here's the part that is taking up so much time, the way I modified it:
Code: Select all
int readQD0(){
  //Returns value from the QRE1113_D0 
  //Lower numbers mean more refleacive
  //More than 3000 means nothing was reflected.
  pinMode( QRE1113_Pin_D0, OUTPUT );  // Sets IR sensor pin to output mode
  digitalWrite( QRE1113_Pin_D0, HIGH );  // Charges the capacitor
  delayMicroseconds(IRon);  // Wait for it to fully charge
  pinMode( QRE1113_Pin_D0, INPUT );  // Switch to input

  long time = micros();  // Sets the variable "time" to count in microseconds?

  //time how long the input is HIGH, but quit after 3ms as nothing happens after that
  while (digitalRead(QRE1113_Pin_D0) == HIGH && micros() - time < IRwait);
  int diff_D0 = micros() - time;

  return diff_D0;
}

int readQD1(){
  //Returns value from the QRE1113_D1 
  //Lower numbers mean more refleacive
  //More than 3000 means nothing was reflected.
  pinMode( QRE1113_Pin_D1, OUTPUT );  // Sets IR sensor pin to output mode
  digitalWrite( QRE1113_Pin_D1, HIGH );  // Charges the capacitor
  delayMicroseconds(IRon);  // Wait for it to fully charge
  pinMode( QRE1113_Pin_D1, INPUT );  // Switch to input

  long time = micros();  // Sets the variable "time" to count in microseconds?

  //time how long the input is HIGH, but quit after 3ms as nothing happens after that
  while (digitalRead(QRE1113_Pin_D1) == HIGH && micros() - time < IRwait);
  int diff_D1 = micros() - time;

  return diff_D1;
}
int readQD2(){
  //Returns value from the QRE1113_D2 
  //Lower numbers mean more refleacive
  //More than 3000 means nothing was reflected.
  pinMode( QRE1113_Pin_D2, OUTPUT );  // Sets IR sensor pin to output mode
  digitalWrite( QRE1113_Pin_D2, HIGH );  // Charges the capacitor
  delayMicroseconds(IRon);  // Wait for it to fully charge
  pinMode( QRE1113_Pin_D2, INPUT );  // Switch to input

  long time = micros();  // Sets the variable "time" to count in microseconds?

  //time how long the input is HIGH, but quit after 3ms as nothing happens after that
  while (digitalRead(QRE1113_Pin_D2) == HIGH && micros() - time < IRwait);
  int diff_D2 = micros() - time;

  return diff_D2;
}
int readQD3(){
  //Returns value from the QRE1113_D3 
  //Lower numbers mean more refleacive
  //More than 3000 means nothing was reflected.
  pinMode( QRE1113_Pin_D3, OUTPUT );  // Sets IR sensor pin to output mode
  digitalWrite( QRE1113_Pin_D3, HIGH );  // Charges the capacitor
  delayMicroseconds(IRon);  // Wait for it to fully charge
  pinMode( QRE1113_Pin_D3, INPUT );  // Switch to input

  long time = micros();  // Sets the variable "time" to count in microseconds?

  //time how long the input is HIGH, but quit after 3ms as nothing happens after that
  while (digitalRead(QRE1113_Pin_D3) == HIGH && micros() - time < IRwait);
  int diff_D3 = micros() - time;

  return diff_D3;
}
Obviously, that's not all of it, but it's the part I'm concerned about.
User avatar
By TrollHammer
#162893
Instead of posting a new thread, I'm reusing the old one.

I ended up using analog sensors for the four main sensors, and a digital for another section (as I'm out of analog inputs). It ends up not being a big of an issue as I thought.

I have another problem, though. The standard QRE1113 (analog, but it holds true for both sensors) has a VERY limited range, on the order of 1/16". I have already had damage to sensors because they have to be so close. As a result, I have built my own sensors using discrete parts following the schematic. The range is up to 1/2" now, which is perfectly usable.

My problem is I need an off-the-shelf sensor, as this is for a work project, and they won't want any hand-made stuff.
User avatar
By TrollHammer
#163097
jremington wrote:Check these out: http://www.pololu.com/catalog/category/79
If I'm understanding right, distance = reflectance (more light reflected, the closer the object is everything else being equal), so a reflector on black track should work similar, right? I've not been certain so I didn't look at distance sensors seriously, especially long range types since my sensors are stacked so close together (0.75" spacing). I'm going to try to scare up some 1134 or 1132 to test with, though. Thanks for the help!

(on further inspection, I'm not sure if it will work. these are a go-nogo digital sensor, which is fast, but will I be able to differentiate between black that's .75" away and reflective .75" away... I can increase this by up to 1") Hmm.....

(2nd edit) Perhaps if I make an aperture cover for the LED side I can decrease the range to make it work with my project. It would save analog inputs and reduce code, and be off the shelf, if I can get it to work.

(3rd)Wow, their site isn't well laid out. Poked around and found this:
http://www.pololu.com/catalog/product/2454

Its functionally the same as the original QTR sensor, but with different emitter/transmitter pair and configured edge on. This is almost identical to how I built my board in layout, but with longer range (up to 1" theoretically). I might start with this as its cheaper and more functionally compatible. Sure took long enough to find it, though.

There is also another sensor that might work, however it's for longer range as well, like the above ones, up to 12", digital on or off, so I don't know if it will end up detecting black when it's 1" away. I mention it because it's a sensor that does not show up under normal listings, you have to look at it on the side bar of other parts.