Reflectance Sensor

Parts

-Arduino microcontroller and carrier board

-LiPo battery

-Pololu reflectance sensor

Prepare the breadboard

Program the Microcontroller (Analog Code)

/**
 * @file: Reflectance Sensor Example
 * @date: 4/5/2011
 *
 * @DESCRIPTION
 * analog read of values 
 *
 */
#define analogPin A0
int sensorValue =0; // variable to store
//--- Function: Setup ()
void setup()
{
    Serial.begin(9600);
}
//--- Function: Loop ()
void loop()
{
    // read the value from the sensor:
    sensorValue = analogRead(analogPin);
    Serial.println(sensorValue);
    delay(500);
}

Program the Microcontroller (Analog and Digital Code)

/**
* @file: Reflectance Sensor Example
* @date: 4/5/2011
*
* @DESCRIPTION
* Analog vs. digital
*
*/
#define analogPin A0
int sensorValue =0; // variable to store
//--- Function: Setup ()
void setup()
{
    Serial.begin(9600);
    pinMode(2, INPUT);
}
//--- Function: Loop ()
void loop()
{
     // read the analog value from the sensor:
    sensorValue = analogRead(analogPin);
    Serial.print("analog: ");
    Serial.println(sensorValue);
    delay(100);
    // read the digital value from the sensor:
    sensorValue = digitalReadu(2);
    Serial.print("digital: ");
    Serial.println(sensorValue);
    delay(500);
}

Line Following

// SAMUEL T. WHITTEMORE
// ROBOT CONTROL FOR 2.007
// If you use this code, please reference Sam!
// include additional headers
#include <Servo.h>
//global declarations
#define RIGHTMOTOR 9        //define a pin for servo
#define LEFTMOTOR 8         //define a pin for servo
#define LED 13              //define a pin for LED
#define PHOTOR 5            //define a pin for Photo resistor
Servo rightmotor;         // initialize  servo
Servo leftmotor;          // initialize  servo
#define leftIRpin A0
#define rightIRpin A1
#define midIRpin A2
#define leverIRpin A4
const int lineIRthreshold = 850;
const int leverIRthreshold = 100;
// played around with values that sets the servos to each position
// these values need to be set for each servo!!!
int neutral = 1500;
int forward = 2000;
int backward = 1000;
int moveforward = 2000;
int movebackward = 1000;
int forwardlow = 1700;
int backwardlow = 1300;
int forwardmid = 1850;
int backwardmid = 1150;
int turnforward = 1750;
int leftIRval =0;         // variable to store
int midIRval =0;          // variable to store
int rightIRval =0;        // variable to store
int leverIRval = 0;
//--- Function: Setup ()
void setup()
{
  Serial.begin(9600);                     // setup serial output
  pinMode(LED, OUTPUT);                   // setup LED
  pinMode (RIGHTMOTOR, OUTPUT);           // want servo pin to be an output
  rightmotor.attach(RIGHTMOTOR);          // attach pin to the servo
  rightmotor.writeMicroseconds(neutral);  // set servo to mid-point
  pinMode (LEFTMOTOR, OUTPUT);             // want servo pin to be n output
  leftmotor.attach(LEFTMOTOR);             // attach pin to the servo
  leftmotor.writeMicroseconds(neutral);   // set servo to mid-point
}
//--- Function: Loop ()
void loop()
{  
  while(analogRead(leverIRpin) < leverIRthreshold )
  {
    int movebackward = 1350;
    int moveforward = 1690;
    // left and right off line
    if ((analogRead(leftIRpin) < lineIRthreshold) &&
      (analogRead(rightIRpin) < lineIRthreshold) )
    {
      rightmotor.writeMicroseconds(moveforward);
      leftmotor.writeMicroseconds(moveforward);
    }
    // right on line
    else if ((analogRead(leftIRpin) < lineIRthreshold) &&
      (analogRead(rightIRpin) > lineIRthreshold ))
    {
      rightmotor.writeMicroseconds(movebackward);
      leftmotor.writeMicroseconds(turnforward);
    }
    // left on line
    else if ((analogRead(leftIRpin) > lineIRthreshold) &&
      (analogRead(rightIRpin) < lineIRthreshold) )
    {
      rightmotor.writeMicroseconds(turnforward);
      leftmotor.writeMicroseconds(movebackward);
    }
    else
    {
      rightmotor.writeMicroseconds(neutral);
      leftmotor.writeMicroseconds(neutral);
    }
  }
}

Line Following with PID

Error = target_pos – current_pos  //calculate error
P = Error * Kp             //error times proportional constant gives P
I = I + Error             //integral stores the accumulated error
I = I * Ki                 //calculates the integral value
D = Error – Previos_error //stores change in error to derivate
Correction = P + I + D

Reference Material