Last active
August 4, 2016 21:04
-
-
Save GreenMoonArt/5f330f28965bf2a8d9562147d4879075 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// hint: see how this person changed the code for their robot: http://www.techbitar.com/fast-line-following-robot.html | |
#include <QTRSensors.h> | |
#include <YD_MotorDriver1.h> | |
#define NUM_SENSORS 6 // number of sensors used | |
#define TIMEOUT 2500 | |
#define EMITTER_PIN QTR_NO_EMITTER_PIN // or 2 if you want to use this feature | |
#define Motor1A 8 | |
#define Motor1B 7 | |
#define Motor2C 6 | |
#define Motor2D 5 | |
#define StartMoveSpeed 200 | |
#define SlowMoveSpeed 280 | |
#define SlowMoveAdjust 5 //determine the adjustment factors to use | |
#define MediumMoveSpeed 300 | |
#define MediumMoveAdjust 5 | |
#define FastMoveSpeed 350 | |
#define FastMoveAdjust 5 | |
#define MaxMoveSpeed 385 | |
#define MaxMoveAdjust 5 | |
YD_MotorDriver1 RobotDriver(Motor1A,Motor1B,Motor2C,Motor2D); | |
// sensor connected through analog pins A0 - A5 i.e. digital pins 14-19 | |
QTRSensorsRC qtrrc((unsigned char[]) {14, 15, 16, 17, 18, 19}, NUM_SENSORS, TIMEOUT, EMITTER_PIN); | |
unsigned int sensorValues[NUM_SENSORS]; | |
void setup() | |
{ | |
RobotDriver.init(); | |
delay(500); | |
pinMode(13, OUTPUT); | |
digitalWrite(13, HIGH); // turn on Arduino's LED to indicate we are in calibration mode | |
for (int i = 0; i < 400; i++) // make the calibration take about 10 seconds | |
{ | |
qtrrc.calibrate(); // reads all sensors 10 times at 2500 us per read (i.e. ~25 ms per call) | |
delay(20); | |
} | |
digitalWrite(13, LOW); // turn off Arduino's LED to indicate we are through with calibration | |
delay(2000); //allow time to position the robot | |
} | |
void loop() | |
{ | |
unsigned int sensors[NUM_SENSORS]; | |
int position = qtrrc.readLine(sensors); | |
int error = position - 2500; // you may need to adjust this factor | |
int errorMargin = 100; // you may need to adjust this factor | |
int delayAmount = 1000; // you WILL need to adjust this | |
if(error > errorMargin) | |
{ | |
//code turn to left | |
TurnLeft(delayAmount); // QUIZ: determine value to use | |
} | |
else if(error < -errorMargin) | |
{ | |
//code turn to right | |
TurnRight(delayAmount); // QUIZ: determine value to use | |
} | |
else | |
{ | |
// code to go straight | |
// if you want to go faster or slower, use the relevant code | |
ForwardMedium(delayAmount); | |
} | |
} | |
void ForwardSlow(int howLong) | |
{ | |
RobotDriver.Motor1Speed(SlowMoveSpeed + SlowMoveAdjust); | |
RobotDriver.Motor2Speed(SlowMoveSpeed - SlowMoveAdjust); | |
delay(howLong); | |
Stop(); | |
} | |
/*---------------------------*/ | |
void ForwardMedium(int howLong) | |
{ | |
RobotDriver.Motor1Speed(MediumMoveSpeed + MediumMoveAdjust); | |
RobotDriver.Motor2Speed(MediumMoveSpeed - MediumMoveAdjust); | |
delay(howLong); | |
Stop(); | |
} | |
/*---------------------------*/ | |
void ForwardFast(int howLong) | |
{ | |
RobotDriver.Motor1Speed(FastMoveSpeed + FastMoveAdjust); | |
RobotDriver.Motor2Speed(FastMoveSpeed - FastMoveAdjust); | |
delay(howLong); | |
Stop(); | |
} | |
/*---------------------------*/ | |
void BackwardSlow(int HowMuch) | |
{ | |
RobotDriver.Motor1Speed(- SlowMoveSpeed ); | |
RobotDriver.Motor2Speed(- SlowMoveSpeed ); | |
delay(HowMuch); | |
Stop(); | |
} | |
/*---------------------------*/ | |
void BackwardMedium(int HowMuch) | |
{ | |
RobotDriver.Motor1Speed(- MediumMoveSpeed); | |
RobotDriver.Motor2Speed(- MediumMoveSpeed); | |
delay(HowMuch); | |
Stop(); | |
} | |
/*---------------------------*/ | |
void BackwardFast(int HowMuch) | |
{ | |
RobotDriver.Motor1Speed(- FastMoveSpeed); | |
RobotDriver.Motor2Speed(- FastMoveSpeed); | |
delay(HowMuch); | |
Stop(); | |
} | |
/*---------------------------*/ | |
void Stop() | |
{ | |
RobotDriver.Motor1Speed(0); | |
RobotDriver.Motor2Speed(0); | |
} | |
/*---------------------------*/ | |
void SpinLeft(int HowMuch) | |
{ | |
RobotDriver.Motor1Speed(MediumMoveSpeed); | |
RobotDriver.Motor2Speed(- MediumMoveSpeed); | |
delay(HowMuch); | |
Stop(); | |
} | |
/*---------------------------*/ | |
void SpinRight(int HowMuch) | |
{ | |
RobotDriver.Motor1Speed(MediumMoveSpeed); | |
RobotDriver.Motor2Speed(- MediumMoveSpeed); | |
delay(HowMuch); | |
Stop(); | |
} | |
// *** Turn left and right functions - you will need to experiment with these *** | |
void TurnLeft(int howLong) | |
{ | |
RobotDriver.Motor1Speed(0); | |
RobotDriver.Motor2Speed(MediumMoveSpeed); | |
delay(howLong); | |
Stop(); | |
} | |
void TurnRight(int howLong) | |
{ | |
RobotDriver.Motor1Speed(MediumMoveSpeed); | |
RobotDriver.Motor2Speed(0); | |
delay(howLong); | |
Stop(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment