Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <RedBot.h> //includes the redbot library
- RedBotMotors motors;// Instantiate the motor control object.
- RedBotSensor IRSensor1 = RedBotSensor(A4);// initialize sensors - left
- RedBotSensor IRSensor2 = RedBotSensor(A5);// right
- int thr = 915; // wood floor is roughly 895-915
- int lSpeed = -70; //forward speed
- int rSpeed = 65; //back speed
- int turn = 60; //turn speed that worked
- void setup() {
- //test code
- //motors.leftMotor(-80);
- //motors.rightMotor(75);
- //delay(6000);
- //motors.stop();
- //delay(100);
- //motors.leftMotor(-100);
- //motors.rightMotor(-100);
- //delay(520);
- //motors.stop();
- Serial.begin(9600);
- }
- void loop() {
- if (IRSensor1.read() > thr && IRSensor2.read() > thr){ // Sensors are over the line, move forward
- motors.leftMotor(lSpeed);
- motors.rightMotor(rSpeed);
- }
- if(IRSensor1.read() < thr && IRSensor2.read() < thr) { //Both sensors are off the line, assume its off the line and turn
- motors.stop();
- motors.leftMotor(-turn);
- motors.rightMotor(-turn);
- }
- if(IRSensor1.read() < thr && IRSensor2.read() > thr) { //Left sensor is off the line, compensate by adjusting right.
- motors.leftMotor(lSpeed-15);
- motors.rightMotor(rSpeed);
- }
- if (IRSensor1.read() > thr && IRSensor2.read() < thr){ //Right sensor is off the line, compensate by adjusting left
- motors.leftMotor(lSpeed);
- motors.rightMotor(rSpeed+15);
- }
- //test code
- //Serial.print(IRSensor1.read());
- //Serial.print("\t");// tab character
- //Serial.print(IRSensor2.read());
- //Serial.print("\t");
- }
Advertisement
Add Comment
Please, Sign In to add comment