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 IR
- RedBotSensor IRSensor2 = RedBotSensor(A5);// right IR
- RedBotBumper lBumper = RedBotBumper(3); // Left bumper
- RedBotBumper rBumper = RedBotBumper(11); //Right bumper
- int thr = 950; // wood floor varies more
- int lSpeed = -70; //forward speed
- int rSpeed = 65; //back speed
- int turn = 60; //turn speed that worked
- int lBumperState; //detect bumper touch
- int rBumperState; //detect bumper touch
- 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() {
- //test code
- ///Serial.print(IRSensor1.read());
- //Serial.print("\t");// tab character
- //Serial.print(IRSensor2.read());
- //Serial.print("\t");
- motors.leftMotor(lSpeed);
- motors.rightMotor(rSpeed);
- lBumperState = lBumper.read();
- rBumperState = rBumper.read();
- if(lBumperState == LOW || rBumperState == LOW) { //it touched something
- motors.stop();
- delay(500);
- motors.leftMotor(-1*lSpeed);
- motors.rightMotor(-1*rSpeed);
- delay(500);
- motors.stop();
- delay(500);
- motors.leftMotor(-70);
- motors.rightMotor(-70);
- delay(700);
- motors.stop();
- delay(500);
- motors.leftMotor(lSpeed);
- motors.rightMotor(rSpeed);
- delay(2500);
- motors.stop();
- delay(500);
- motors.leftMotor(70);
- motors.rightMotor(70);
- delay(1000);
- motors.stop();
- delay(500);
- motors.leftMotor(lSpeed);
- motors.rightMotor(rSpeed);
- delay(3500);
- motors.stop();
- delay(500);
- motors.leftMotor(70);
- motors.rightMotor(70);
- delay(1000);
- motors.stop();
- delay(500);
- motors.leftMotor(lSpeed);
- motors.rightMotor(rSpeed);
- delay(2500);
- motors.stop();
- motors.leftMotor(-70);
- motors.rightMotor(-70);
- delay(700);
- motors.stop();
- delay(500);
- }
- if(IRSensor1.read() > thr) { //Left sensor is off the line, compensate by adjusting right.
- motors.leftMotor(lSpeed+10);
- motors.rightMotor(rSpeed);
- }
- if (IRSensor2.read() > thr){ //Right sensor is off the line, compensate by adjusting left
- motors.leftMotor(lSpeed);
- motors.rightMotor(rSpeed+10);
- }
- //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