I decided to put the QRE1113 breakout boards onto movable arms so that I could experiment with the spacing between them. The breakout boards themselves are soldered to 3 lengths of 22 gauge hookup wire. Three different wire colours makes hookup simpler. The wires are enclosed in a 3" length of straw. The arms are made out of found pieces of plastic moulding attached to the Line Follower base with a single #6 bolt and nut. The hole for the straw is a tight fit so that the height of the QRE1113 can be easily adjusted.
The QRE1113 is connected to +5V power and ground and the signal wires (out) connected to digital inputs 2 and 3.
This is the code used to verify the operation of the detectors:
//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);
}
int readQD(){
//Returns value from the QRE1113
//Lower numbers mean more reflective
//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;
}
The simplest line following algorithm is to stall the wheel on the same side causing the platform to move away from the line. If the left hand detector encounters the line then the left hand motor stops for a period of time while the right hand motor continues causing the platform to turn left or away from the line. This assumes that the line is always between the 2 sensors. In the code below the amount of time the wheel stalls is the variable turn.
int M1F = 6; // motor 1 forward
int M1R = 5; // motor 1 reverse
int M2F = 11; // motor 2 forward
int M2R = 10; // motor 2 reverse
int turn = 120;
int forward = 2000;
int leftPin = 2; //left detector connected to digital 2
int rightPin = 3; //right detector connected to digital 3
int rightQRE_Value;
int leftQRE_Value;
void setup(){
//Serial.begin(9600);
}
void loop(){
rightQRE_Value = readQD(rightPin);
leftQRE_Value = readQD(leftPin);
//Serial.println(rightQRE_Value);
//Serial.println(leftQRE_Value);
if (rightQRE_Value > 100){
analogWrite(M2F,0);
delay(turn);
}
else if (leftQRE_Value > 100){
analogWrite(M1F,0);
delay(turn);
}
else{
analogWrite(M1F, 150);
analogWrite(M2F, 150);
}
}
int readQD(int pin){
//Returns value from the QRE1113
//Lower numbers mean more reflective
//More than 3000 means nothing was reflected.
pinMode( pin, OUTPUT );
digitalWrite( pin, HIGH );
delayMicroseconds(10);
pinMode( pin, INPUT );
long time = micros();
//time how long the input is HIGH, but quit after 3ms as nothing happens after that
while (digitalRead(pin) == HIGH && micros() - time < 3000);
int diff = micros() - time;
return diff;
}
A little adjustment to the speed (ended up at 150) and turn (ended up at 120) and the platform was able to follow this path.
No comments:
Post a Comment