The working of an LFR , is very simple. It has two sensors placed on either side of the line. If the left sensor comes on the black line , the left motor should stop , for it to correct its position . Similarly if the right sensor comes above the black line , the right motor should stop moving.
How is the black line detected :
Black and white colours have different optic properties. While black absorbs light of all colours incident on it , white reflects most of these back. So naturally when the sensor is above the white portion , it will receive a higher intensity of light and thus will provide a higher value.
Our task here then is to make a sensor that can convert light intensity , into a voltage reading , which the Arduino can read via its analog input pins.
We will use a photo resistor , which is a device whose resistance to current changes with the incidence of light intensity.
Making the sensor
The sensor is a basic voltage divider circuit. The photo-resistor is connected in series with a 100 ohm resistor. On end of this combination is connected to +5v and other to ground. The junction is connected to any of the analog pins on the arduino. A resistor is added to this connection to prevent damage to the arduino due to large currents. The junction potential changes when the resistance of the photo resistor changes. This change in potential is measured by the Arduino and is taken advantage of .
Created using Fritzing
Wiring The Motors.
Check my previous post on running motors using the Arduino.
http://crazzycircuits.blogspot.com/2015/07/how-to-run-motor-using-l293d-double-h.html
The Program:
#define M1pin1 9
#define M1pin2 8
#define M2pin1 10
#define M2pin2 11
#define Sensor1 A0
#define Sensor2 A1
int sensor_value1,sensor_value2,diff;
void setup()
{
Serial.begin (9600);
pinMode(M1pin1, OUTPUT);
pinMode(M1pin2, OUTPUT);
pinMode(M2pin1, OUTPUT);
pinMode(M2pin2, OUTPUT);
sensor_value1 = analogRead(Sensor1);
sensor_value2=analogRead(Sensor2);
diff= sensor_value2-sensor_value1;
#define M1pin2 8
#define M2pin1 10
#define M2pin2 11
#define Sensor1 A0
#define Sensor2 A1
int sensor_value1,sensor_value2,diff;
void setup()
{
Serial.begin (9600);
pinMode(M1pin1, OUTPUT);
pinMode(M1pin2, OUTPUT);
pinMode(M2pin1, OUTPUT);
pinMode(M2pin2, OUTPUT);
sensor_value1 = analogRead(Sensor1);
sensor_value2=analogRead(Sensor2);
diff= sensor_value2-sensor_value1;
// We account for this difference to equalise both sensor inputs values for the white portion. This little trick eliminates the need for calibration.
}
void loop()
{
}
void loop()
{
sensor_value1 = analogRead(Sensor1)+diff;
sensor_value2=analogRead(Sensor2);
if(sensor_value2-sensor_value1>1) //Sensor values differ when either sensor is on the black line
OFF(M2pin1,M2pin2);
else
ON(M2pin1,M2pin2);
// ______________________
if(sensor_value1-sensor_value2>1)
OFF(M1pin1,M1pin2);
else
ON(M1pin1,M1pin2);
//Serial.println(sensor_value1);
//Serial.println(sensor_value2);
}
void ON(int Mpin1,int Mpin2) // runs motor
{
digitalWrite(Mpin1, HIGH);
digitalWrite(Mpin2, LOW);
}
void OFF(int Mpin1,int Mpin2) // Stops motor
{
digitalWrite(Mpin1, LOW);
digitalWrite(Mpin2, LOW);
}
//End of Program
else
ON(M2pin1,M2pin2);
// ______________________
if(sensor_value1-sensor_value2>1)
OFF(M1pin1,M1pin2);
else
ON(M1pin1,M1pin2);
//Serial.println(sensor_value1);
//Serial.println(sensor_value2);
}
void ON(int Mpin1,int Mpin2) // runs motor
{
digitalWrite(Mpin1, HIGH);
digitalWrite(Mpin2, LOW);
}
void OFF(int Mpin1,int Mpin2) // Stops motor
{
digitalWrite(Mpin1, LOW);
digitalWrite(Mpin2, LOW);
}
//End of Program
...................................................................................................................................................................
No comments:
Post a Comment