Hi guys:
I built a line follower robot composed of the following items:
- 2 DC toy motors with gearbox
- Dual L298 H bridge
- Arduino UNO
- 2 QTR analogic sensors
- 3 leds
The wiring goes as follows:
- QTR signal 1 to A0
- QTR signal 2 to A1
- H bridge:
ENA=5;//connected to Arduino's port 5(output pwm)
IN1=2;//connected to Arduino's port 2
IN2=3;//connected to Arduino's port 3
ENB=6;//connected to Arduino's port 6(output pwm)
IN3=4;//connected to Arduino's port 4
IN4=7;//connected to Arduino's port 7
- Led connected to pins 8,9 and 10
I run the following code:
#define SENSOR_PINA0 A0
#define SENSOR_PINA1 A1
int LightValueA0; // The value from the sensor (0..1023)
int LightValueA1; // The value from the sensor (0..1023)
int ENA=5;//connected to Arduino's port 5(output pwm)
int IN1=2;//connected to Arduino's port 2
int IN2=3;//connected to Arduino's port 3
int ENB=6;//connected to Arduino's port 6(output pwm)
int IN3=4;//connected to Arduino's port 4
int IN4=7;//connected to Arduino's port 7
void setup() {
Serial.begin (9600);
pinMode(SENSOR_PINA0, INPUT); // Not really necessary: here for example
pinMode(SENSOR_PINA1, INPUT); // Not really necessary: here for example
pinMode(ENA,OUTPUT);//output
pinMode(ENB,OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
delay(100);
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);//stop driving
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
}
void loop() {
{
LightValueA0 = analogRead(SENSOR_PINA0); //Read the voltage from sensor
Serial.println(LightValueA0,DEC); // Send result to Serial Monitor
delay(100);
LightValueA1 = analogRead(SENSOR_PINA1); //Read the voltage from sensor
Serial.println(LightValueA1,DEC); // Send result to Serial Monitor
delay(100);
if (analogRead(SENSOR_PINA0) <= 900 && analogRead(SENSOR_PINA1) <= 900)
{digitalWrite(9, HIGH); // set the LED on
delay(100); // wait for a second
digitalWrite(9, LOW); // set the LED off
delay(100); // wait for a second
analogWrite(ENA,220);//start driving motorA
analogWrite(ENB,250);//start driving motorB
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);//setting motorA's directon
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);//setting motorB's directon
delay(70);
analogWrite(ENA,250);//start driving motorA
analogWrite(ENB,250);//start driving motorB
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);//setting motorA's directon
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);//setting motorB's directon
delay(50);
digitalWrite(9, HIGH); // set the LED on
delay(100); // wait for a second
digitalWrite(9, LOW); // set the LED off
delay(100);} // wait for a second
}
if (analogRead(SENSOR_PINA0) >= 900 && analogRead(SENSOR_PINA1) <= 900)
{digitalWrite(9, HIGH); // set the LED on
delay(100); // wait for a second
digitalWrite(9, LOW); // set the LED off
delay(100); // wait for a second
analogWrite(ENA,250);//start driving motorA
analogWrite(ENB,250);//start driving motorB
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);//setting motorA's directon
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);//setting motorB's directon
delay(50);
analogWrite(ENA,200);//start driving motorA
analogWrite(ENB,230);//start driving motorB
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);//setting motorA's directon
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);//setting motorB's directon
delay(50);
digitalWrite(9, HIGH); // set the LED on
delay(100); // wait for a second
digitalWrite(9, LOW); // set the LED off
delay(100); // wait for a second
}
{
if (analogRead(SENSOR_PINA0) <= 900 && analogRead(SENSOR_PINA1) >= 900)
{digitalWrite(9, HIGH); // set the LED on
delay(100); // wait for a second
digitalWrite(9, LOW); // set the LED off
delay(100); // wait for a second
analogWrite(ENA,250);//start driving motorA
analogWrite(ENB,250);//start driving motorB
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);//setting motorA's directon
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);//setting motorB's directon
delay(50);
analogWrite(ENA,200);//start driving motorA
analogWrite(ENB,230);//start driving motorB
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);//setting motorA's directon
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);//setting motorB's directon
delay(50);
digitalWrite(9, HIGH); // set the LED on
delay(100); // wait for a second
digitalWrite(9, LOW); // set the LED off
delay(100); // wait for a second
}
delay(100);
}}
The track is composed of a black line over a white pitch. Both sensors are places by the sides of the black line, so in a straight position the robot sees white in both sensors. When a turn comes one sensor reads black and then correct position. It all works well, but I have to introduce the stops to avoid the robot going off truck and the whole process of line following becomes to slow.
I would like to try code with PID algorithm or another simpler code but yet better than then one I am using until now. Can you help me please to improve the performance of my line follower?
Thanks!