#include <AFMotor.h>
AF_DCMotor motorL(3, MOTOR12_1KHZ);
AF_DCMotor motorR(2, MOTOR12_1KHZ);
// These constants won't change:
const int LeftPR = A1; // pin that the sensor is attached to
const int CentrePR = A2; // pin that the sensor is attached to
const int RightPR = A3; // pin that the sensor is attached to
const int IndicatorL = 10; // pin that the LED is attached to
const int IndicatorC = 9; // pin that the LED is attached to
const int IndicatorR = 19; // pin that the LED is attached to
const int button = 18;
// variables:
int LeftValue = 0; // the sensor value
int LeftMin = 1023; // minimum sensor value
int LeftMax = 0; // maximum sensor value
int CentreValue = 0; // the sensor value
int CentreMin = 1023; // minimum sensor value
int CentreMax = 0; // maximum sensor value
int RightValue = 0; // the sensor value
int RightMin = 1023; // minimum sensor value
int RightMax = 0; // maximum sensor value
void setup() {
// Serial monitor
Serial.begin(9600); // set up Serial library at 9600 bps
//Motors
//set the speed to 200 of 255 of "motorL".
//Note that 0 is stop, 255 is full speed:
motorL.setSpeed(200);
// set the speed to 200 of 255 of "motorR":
motorR.setSpeed(200);
//Calibraton
// turn on LEDs to signal the start of the calibration period:
pinMode(IndicatorL,OUTPUT);
pinMode(IndicatorC,OUTPUT);
pinMode(IndicatorR,OUTPUT);
digitalWrite(IndicatorL, HIGH);
digitalWrite(IndicatorC, HIGH);
digitalWrite(IndicatorR, HIGH);
// calibrate during the first 5 seconds
while (millis() < 5000) {
//Spin on the spot
motorL.run(FORWARD); // turn it on going forward
motorR.run(BACKWARD); // motor 2 goes forward as well
//Calibrate
LeftValue = analogRead(LeftPR);
// record the maximum sensor value
if (LeftValue > LeftMax) {
LeftMax = LeftValue;
}
// record the minimum sensor value
if (LeftValue < LeftMin) {
LeftMin = LeftValue;
}
CentreValue = analogRead(CentrePR);
// record the maximum sensor value
if (CentreValue > CentreMax) {
CentreMax = CentreValue;
}
// record the minimum sensor value
if (CentreValue < CentreMin) {
CentreMin = CentreValue;
}
RightValue = analogRead(RightPR);
// record the maximum sensor value
if (RightValue > RightMax) {
RightMax = RightValue;
}
// record the minimum sensor value
if (RightValue < RightMin) {
RightMin = RightValue;
}
}
// signal the end of the calibration period
digitalWrite(IndicatorL, LOW);
digitalWrite(IndicatorC, LOW);
digitalWrite(IndicatorR, LOW);
//hold
while (digitalRead (button) == HIGH){
// stops script. Its waiting for a button press (LOW on "button")
}
}
void loop() {
{
//LEFT SENSOR
LeftValue = analogRead(LeftPR);
// apply the calibration to the sensor reading
LeftValue = map(LeftValue, LeftMin, LeftMax, 0, 100);
LeftValue = constrain(LeftValue, 0, 200);
// in case the sensor value is outside the range seen during calibration
if (LeftValue > 100) {
digitalWrite (IndicatorL, HIGH);
}
if (LeftValue < 100) {
digitalWrite (IndicatorL, LOW);
}
//CENTRE SENSOR
CentreValue = analogRead(CentrePR);
// apply the calibration to the sensor reading
CentreValue = map(CentreValue, CentreMin, CentreMax, 0, 100);
CentreValue = constrain(CentreValue, 0, 200);
// in case the sensor value is outside the range seen during calibration
if (CentreValue > 100) {
digitalWrite (IndicatorC, HIGH);
}
if (CentreValue < 100) {
digitalWrite (IndicatorC, LOW);
}
// RIGHT SENSOR
RightValue = analogRead(RightPR);
// apply the calibration to the sensor reading
RightValue = map(RightValue, RightMin, RightMax, 0, 100);
RightValue = constrain(RightValue, 0, 200);
// in case the sensor value is outside the range seen during calibration
if (RightValue > 100) {
digitalWrite (IndicatorR, HIGH);
}
if (RightValue < 100) {
digitalWrite (IndicatorR, LOW);
}
}
// Line following arguments
//Straight:
if (digitalRead(IndicatorL) == LOW && digitalRead(IndicatorC) == HIGH && digitalRead(IndicatorR) == LOW){
motorL.run(FORWARD);
motorR.run(FORWARD);
}
if (digitalRead(IndicatorL) == LOW && digitalRead(IndicatorC) == LOW && digitalRead(IndicatorR) == LOW){
motorL.run(FORWARD);
motorR.run(FORWARD);
}
//Cross roads
if (digitalRead(IndicatorL) == HIGH && digitalRead(IndicatorC) == HIGH && digitalRead(IndicatorR) == HIGH){
motorL.run(FORWARD);
motorR.run(FORWARD);
}
//Turn right:
if (digitalRead(IndicatorL) == LOW && digitalRead(IndicatorC) == LOW && digitalRead(IndicatorR) == HIGH){
motorL.run(FORWARD);
motorR.run(RELEASE);
}
//Turn Left
if (digitalRead(IndicatorL) == HIGH && digitalRead(IndicatorC) == LOW && digitalRead(IndicatorR) == LOW){
motorL.run(RELEASE);
motorR.run(FORWARD);
}
Serial.print("Left:");
Serial.print(LeftValue);
Serial.print(" Centre:");
Serial.print(CentreValue);
Serial.print(" Right:");
Serial.print(RightValue);
Serial.println();
}