Guest User

Untitled

a guest
Oct 17th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. #include <RedBot.h>
  2. RedBotSensor left = RedBotSensor(A3); // initialize a left sensor object on A3
  3. RedBotSensor center = RedBotSensor(A6); // initialize a center sensor object on A6
  4. RedBotSensor right = RedBotSensor(A7); // initialize a right sensor object on A7
  5.  
  6. // constants that are used in the code. LINETHRESHOLD is the level to detect
  7. // if the sensor is on the line or not. If the sensor value is greater than this
  8. // the sensor is above a DARK line.
  9. //
  10. // SPEED sets the nominal speed
  11.  
  12. #define LINETHRESHOLD 800
  13. #define SPEED 60 // sets the nominal speed. Set to any number from 0 - 255.
  14.  
  15. RedBotMotors motors;
  16. int leftSpeed; // variable used to store the leftMotor speed
  17. int rightSpeed; // variable used to store the rightMotor speed
  18.  
  19. void setup()
  20. {
  21. Serial.begin(9600);
  22. Serial.println("Welcome to experiment 6.2 - Line Following");
  23. Serial.println("------------------------------------------");
  24. delay(2000);
  25. Serial.println("IR Sensor Readings: ");
  26. delay(500);
  27. }
  28.  
  29. void loop()
  30. {
  31. Serial.print(left.read());
  32. Serial.print("\t"); // tab character
  33. Serial.print(center.read());
  34. Serial.print("\t"); // tab character
  35. Serial.print(right.read());
  36. Serial.println();
  37.  
  38. // if on the line drive left and right at the same speed (left is CCW / right is CW)
  39. if(center.read() > LINETHRESHOLD)
  40. {
  41. leftSpeed = -SPEED;
  42. rightSpeed = SPEED;
  43. }
  44.  
  45. // if the line is under the right sensor, adjust relative speeds to turn to the right
  46. else if(right.read() > LINETHRESHOLD)
  47. {
  48. leftSpeed = -(SPEED + 50);
  49. rightSpeed = SPEED - 50;
  50. }
  51.  
  52. // if the line is under the left sensor, adjust relative speeds to turn to the left
  53. else if(left.read() > LINETHRESHOLD)
  54. {
  55. leftSpeed = -(SPEED - 50);
  56. rightSpeed = SPEED + 50;
  57. }
  58.  
  59. // if all sensors are on black or up in the air, stop the motors.
  60. // otherwise, run motors given the control speeds above.
  61. if((left.read() > LINETHRESHOLD) && (center.read() > LINETHRESHOLD) && (right.read() > LINETHRESHOLD) )
  62. {
  63. motors.stop();
  64. }
  65. else
  66. {
  67. motors.leftMotor(leftSpeed);
  68. motors.rightMotor(rightSpeed);
  69.  
  70. }
  71. delay(0); // add a delay to decrease sensitivity.
  72. }
Add Comment
Please, Sign In to add comment