Sunday, March 26, 2017

Line following example

// These constants won't change.  They're used to give names
// to the pins used:
const int analogInPinL = A0;  // Analog input pin that the IR sensor is attached to
const int analogInPinR = A1;  // Analog input pin that the IR sensor is attached to

const int maxSpeed = 250;     // fastest the robot will go (on one wheel)

int leftBalance = 50;

int motorLeftSpeed = 150;
int motorRightSpeed = 150;

int sensorValueL = 0;        // value read from the pot
int outputValueL = 0;        // value output to the PWM (analog out)
int sensorValueR = 0;        // value read from the pot
int outputValueR = 0;        // value output to the PWM (analog out)

int combinedSensor = 0;     // value of both sensors

// connect motor controller pins to Arduino digital pins
// motor one
int motorSpeedA = 10;
int motorDirectionAin1 = 9;
int motorDirectionAin2 = 8;
// motor two
int motorSpeedB = 5;
int motorDirectionBin3 = 7;
int motorDirectionBin4 = 6;

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);

 // set all the motor control pins to outputs
  pinMode(motorSpeedA, OUTPUT);
  pinMode(motorSpeedB, OUTPUT);
  pinMode(motorDirectionAin1, OUTPUT);
  pinMode(motorDirectionAin2, OUTPUT);
  pinMode(motorDirectionBin3, OUTPUT);
  pinMode(motorDirectionBin4, OUTPUT);
}

void loop() {

  delay(2);
  sensorValueL = analogRead(analogInPinL);
  sensorValueR = analogRead(analogInPinR);

  combinedSensor = sensorValueL + sensorValueR;

 Serial.print("total sensor = ");
 Serial.println(combinedSensor);

  if (combinedSensor < 2000)

  {
    // turn on motors
    digitalWrite(motorDirectionAin1, HIGH);
    digitalWrite(motorDirectionAin2, LOW); 
    digitalWrite(motorDirectionBin3, HIGH);
    digitalWrite(motorDirectionBin4, LOW);   
Serial.println("motor on");
      motorLeftSpeed = 150;
      motorRightSpeed = 150;

    analogWrite(motorSpeedA, motorLeftSpeed);
    analogWrite(motorSpeedB, motorRightSpeed);
  }

    digitalWrite(motorDirectionAin1, HIGH);
    digitalWrite(motorDirectionAin2, LOW); 
    digitalWrite(motorDirectionBin3, HIGH);
    digitalWrite(motorDirectionBin4, LOW);   

 
  while (combinedSensor < 1500)

  {
    if (combinedSensor < 250)
    {
      motorLeftSpeed = 150;
      motorRightSpeed = 150;
    digitalWrite(motorDirectionAin1, HIGH);
    digitalWrite(motorDirectionAin2, LOW); 
    digitalWrite(motorDirectionBin3, HIGH);
    digitalWrite(motorDirectionBin4, LOW);   

    }

    if ((sensorValueL > 100) && (motorRightSpeed < 250))
    {
      motorLeftSpeed = motorLeftSpeed - 15;
      motorRightSpeed = motorRightSpeed + 10;
    digitalWrite(motorDirectionAin1, LOW);
    digitalWrite(motorDirectionAin2, HIGH); 
    digitalWrite(motorDirectionBin3, HIGH);
    digitalWrite(motorDirectionBin4, LOW);   


    }   

    if ((sensorValueR > 100) && (motorLeftSpeed < 250))
    {
      motorLeftSpeed = motorLeftSpeed + 10;
      motorRightSpeed = motorRightSpeed - 15;
    digitalWrite(motorDirectionAin1, HIGH);
    digitalWrite(motorDirectionAin2, LOW); 
    digitalWrite(motorDirectionBin3, LOW);
    digitalWrite(motorDirectionBin4, HIGH);   

    }   

    analogWrite(motorSpeedA, motorLeftSpeed);
    analogWrite(motorSpeedB, motorRightSpeed);
    delay(5);

    // read the analog in value:
    sensorValueL = analogRead(analogInPinL);
    sensorValueR = analogRead(analogInPinR);

    combinedSensor = sensorValueL + sensorValueR;

  }

    // now turn off motors
    digitalWrite(motorDirectionAin1, LOW);
    digitalWrite(motorDirectionAin2, LOW); 
    digitalWrite(motorDirectionBin3, LOW);
    digitalWrite(motorDirectionBin4, LOW); 
 

}