Zanuark

Untitled

Apr 7th, 2020
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include <RedBot.h> //includes the redbot library
  2. RedBotMotors motors;// Instantiate the motor control object.
  3. RedBotSensor IRSensor1 = RedBotSensor(A4);// initialize sensors - left
  4. RedBotSensor IRSensor2 = RedBotSensor(A5);// right
  5. int thr = 915; // wood floor is roughly 895-915
  6. int lSpeed = -70; //forward speed
  7. int rSpeed = 65; //back speed
  8. int turn = 60; //turn speed that worked
  9. void setup() {
  10.  
  11. //test code
  12.  
  13. //motors.leftMotor(-80);
  14. //motors.rightMotor(75);
  15. //delay(6000);
  16. //motors.stop();
  17. //delay(100);
  18. //motors.leftMotor(-100);
  19. //motors.rightMotor(-100);
  20. //delay(520);
  21. //motors.stop();
  22. Serial.begin(9600);
  23.  
  24. }
  25.  
  26. void loop() {
  27.  
  28. if (IRSensor1.read() > thr && IRSensor2.read() > thr){ // Sensors are over the line, move forward
  29. motors.leftMotor(lSpeed);
  30. motors.rightMotor(rSpeed);
  31. }
  32. if(IRSensor1.read() < thr && IRSensor2.read() < thr) { //Both sensors are off the line, assume its off the line and turn
  33. motors.stop();
  34.  
  35. motors.leftMotor(-turn);
  36. motors.rightMotor(-turn);
  37.  
  38. }
  39. if(IRSensor1.read() < thr && IRSensor2.read() > thr) { //Left sensor is off the line, compensate by adjusting right.
  40. motors.leftMotor(lSpeed-15);
  41. motors.rightMotor(rSpeed);
  42. }
  43. if (IRSensor1.read() > thr && IRSensor2.read() < thr){ //Right sensor is off the line, compensate by adjusting left
  44. motors.leftMotor(lSpeed);
  45. motors.rightMotor(rSpeed+15);
  46. }
  47.  
  48. //test code
  49.  
  50. //Serial.print(IRSensor1.read());
  51. //Serial.print("\t");// tab character
  52. //Serial.print(IRSensor2.read());
  53. //Serial.print("\t");
  54. }
Advertisement
Add Comment
Please, Sign In to add comment