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