Advertisement
jpglickwebber

Sumo

May 7th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. /*
  2.  
  3. This demonstration shows how to use a set of four Parallax QTI sensors to provide line-following
  4. capability to your BOE Shield-Bot Arduino robot.
  5.  
  6. Refer to the following pages for using the QTI Line Follower AppKit.
  7. http://www.parallax.com/product/28108
  8.  
  9. Refer to the following help pages for additional wiring diagrams when using the QTI sensors with the
  10. Arduino Uno:
  11. http://learn.parallax.com/KickStart/555-27401
  12.  
  13. Wiring Diagram for QTI Sensors:
  14. Arduino Sensor
  15. D7 QTI4 - Far left
  16. D6 QTI3 - Mid left
  17. D5 QTI2 - Mid right
  18. D4 QTI1 - Far right
  19.  
  20. Wiring Diagram for Servos:
  21. Arduino Servo
  22. D13 Left servo
  23. D12 Right servo
  24.  
  25. This example code makes use of an intermediate Arduino programming technique, specifically directly
  26. manipulating multiple pins at once on the Arduino. This technique is referred to as port manipulation,
  27. and is more fully discussed here:
  28. http://playground.arduino.cc/Learning/PortManipulation
  29.  
  30. Important: This demonstration was written, and intended for, use with the Arduino Uno microcontroller.
  31. Other Arduino boards may not be compatible.
  32.  
  33. */
  34.  
  35. #include <Servo.h> // Use the Servo library (included with Arduino IDE)
  36.  
  37. Servo servoL; // Define the left and right servos
  38. Servo servoR;
  39.  
  40. // Perform these steps with the Arduino is first powered on
  41. void setup()
  42. {
  43. Serial.begin(9600); // Set up Arduino Serial Monitor at 9600 baud
  44. servoL.attach(12); // Attach (programmatically connect) servos to pins on Arduino
  45. servoR.attach(11);
  46. }
  47.  
  48. // This code repeats indefinitely
  49. void loop()
  50. {
  51. DDRD |= B11110000; // Set direction of Arduino pins D4-D7 as OUTPUT
  52. PORTD |= B11110000; // Set level of Arduino pins D4-D7 to HIGH
  53. delayMicroseconds(230); // Short delay to allow capacitor charge in QTI module
  54. DDRD &= B00001111; // Set direction of pins D4-D7 as INPUT
  55. PORTD &= B00001111; // Set level of pins D4-D7 to LOW
  56. delayMicroseconds(230); // Short delay
  57. int pins = PIND; // Get values of pins D0-D7
  58. pins >>= 4; // Drop off first four bits of the port; keep only pins D4-D7
  59.  
  60. Serial.println(pins, BIN); // Display result of D4-D7 pins in Serial Monitor
  61.  
  62. // Determine how to steer based on state of the four QTI sensors
  63. int vL, vR, delayTime;
  64. switch(pins) // Compare pins to known line following states
  65. {
  66. case B0000:
  67. vL=100;
  68. vR=100;
  69. delayTime=20;
  70. break;
  71. case B1111:
  72. vL=100;
  73. vR=100;
  74. delayTime=20;
  75. break;
  76. case B0111:
  77. vL=200;
  78. vR=0;
  79. delayTime=800;
  80. break;
  81. case B0011:
  82. vL = 100;
  83. vR = 0;
  84. delayTime=800;
  85. break;
  86. case B0001:
  87. vL=100;
  88. vR=0;
  89. delayTime=800;
  90. break;
  91. case B1110:
  92. vL=0;
  93. vR=200;
  94. delayTime=800;
  95. case B1100:
  96. vL=0;
  97. vR=100;
  98. delayTime=800;
  99. break;
  100. case B1000:
  101. vL=0;
  102. vR=100;
  103. delayTime=800;
  104. break;
  105. }
  106.  
  107. servoL.writeMicroseconds(1500 + vL); // Steer robot to recenter it over the line
  108. servoR.writeMicroseconds(1500 - vR);
  109.  
  110. delay(delayTime); // Delay for 50 milliseconds (1/20 second)
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement