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 A0int 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 A0int 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 resistorServo rightmotor; // initialize servoServo leftmotor; // initialize servo#define leftIRpin A0#define rightIRpin A1#define midIRpin A2#define leverIRpin A4const 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 storeint midIRval =0; // variable to storeint rightIRval =0; // variable to storeint 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 errorP = Error * Kp //error times proportional constant gives PI = I + Error //integral stores the accumulated errorI = I * Ki //calculates the integral valueD = Error – Previos_error //stores change in error to derivateCorrection = P + I + DReference Material