Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. Servo servoLeft;
  4. Servo servoRight;
  5.  
  6. int infraredState;
  7. int front; //button state for fron IR sensor
  8. int iFront = 13; //pin number for front IR sensor
  9. int left; //button state for left IR sensor
  10. int iLeft = 4; //pin number for left IR sensor
  11. int right; //button state for right IR sensor
  12. int iRight = 5; //pin number for right IR sensor
  13. int trigPin = 7;
  14. int echoPin = 8;
  15. volatile unsigned int Rindex;
  16. volatile unsigned int Lindex;
  17. int xY [2]; //xY[0] stores array index and xY[1] stores binary string index
  18. unsigned char dir = 1; //determines direction
  19. int arrIndex = 9; //array index starts at 9 because its the right-most column of the maze
  20. int binIndex = 0; //bin index starts at 0 because its the bottom row of the maze
  21.  
  22.  
  23.  
  24. void setup() {
  25. // put your setup code here, to run once:
  26. servoLeft.attach(10);
  27. servoRight.attach(11);
  28. pinMode(iLeft, INPUT);
  29. pinMode(iRight, INPUT);
  30. pinMode(iFront, INPUT);
  31. attachInterrupt(0, rtEncoder, RISING);
  32. attachInterrupt(1, ltEncoder, RISING);
  33. pinMode(trigPin, OUTPUT);
  34. pinMode(echoPin, INPUT);
  35.  
  36. }
  37.  
  38. void loop() {
  39. // put your main code here, to run repeatedly:
  40.  
  41. servoRight.write(90);
  42. servoLeft.write(90);
  43.  
  44. left = digitalRead(iLeft);
  45. right = digitalRead(iRight);
  46. front = digitalRead(iFront);
  47.  
  48. while ( left == LOW && right == LOW && front == HIGH) {
  49. distanceTest();
  50. forward();
  51. delay(500);
  52. left = digitalRead(iLeft);
  53. right = digitalRead(iRight);
  54. front = digitalRead(iFront);
  55.  
  56. }
  57.  
  58. while ( right == HIGH) {
  59. distanceTest();
  60. turnRight();
  61. delay(200);
  62. forward();
  63. delay(500);
  64. right = digitalRead(iRight);
  65. front = digitalRead(iFront);
  66. left = digitalRead(iLeft);
  67. }
  68. while (left == LOW && right == LOW && front == LOW) {
  69. distanceTest();
  70. turnRight();
  71. turnRight();
  72. delay(500);
  73. left = digitalRead(iLeft);
  74. right = digitalRead(iRight);
  75. front = digitalRead(iFront);
  76. }
  77.  
  78. while ( left == HIGH && right == LOW && front == LOW) {
  79. distanceTest();
  80. turnLeft();
  81. forward();
  82. delay(500);
  83. left = digitalRead(iLeft);
  84. right = digitalRead(iRight);
  85. front = digitalRead(iFront);
  86. }
  87.  
  88.  
  89.  
  90. }
  91.  
  92. void forward() {
  93. /*
  94. Rindex = 0;
  95. Lindex = 0;
  96. int testFront = HIGH;
  97. while (testFront == HIGH) {
  98.  
  99. servoRight.write(180);
  100. servoLeft.write(0);
  101. testFront = digitalRead(iFront);
  102. }
  103. stopRobot();
  104.  
  105. */
  106. Rindex = 0;
  107. Lindex = 0;
  108. while (Rindex < 32 && Lindex < 32) {
  109. servoRight.write(180);
  110. servoLeft.write(0);
  111. if ((Rindex >= 32) || (Lindex >= 32)) {
  112. stopRobot();
  113. break;
  114. }
  115. }
  116.  
  117. if(dir == 1)
  118. binIndex += 1;
  119. if(dir == 2)
  120. arrIndex += 1;
  121. if(dir == 3)
  122. binIndex -= 1;
  123. if(dir == 4)
  124. arrIndex -= 1;
  125.  
  126. }
  127.  
  128. void forward(int index) {
  129. Rindex = 0;
  130. Lindex = 0;
  131. while (Rindex < index + 1 && Lindex < index + 1) {
  132. servoRight.write(180);
  133. servoLeft.write(0);
  134.  
  135.  
  136. if ((Rindex >= index + 1) || (Lindex >= index + 1)) {
  137. stopRobot();
  138. break;
  139. }
  140. }
  141. }
  142.  
  143. void reverse() {
  144. servoLeft.write(180);
  145. servoRight.write(0);
  146. }
  147.  
  148. void turnRight(int index) {
  149. Rindex = 0;
  150. Lindex = 0;
  151.  
  152. while (Rindex < index + 1 && Lindex < index + 1) {
  153. servoRight.write(180);
  154. servoLeft.write(180);
  155.  
  156. if ((Rindex >= index + 1) || (Lindex >= index + 1)) {
  157. stopRobot();
  158. break;
  159. }
  160. }
  161. // forward(32);
  162.  
  163.  
  164. }
  165.  
  166. void turnRight() {
  167. Rindex = 0;
  168. Lindex = 0;
  169.  
  170. if(dir == 1){
  171. dir += 3;
  172. }
  173. else
  174. dir -= 1;
  175.  
  176. while (Rindex < 10 && Lindex < 10) {
  177. servoRight.write(180);
  178. servoLeft.write(180);
  179.  
  180. if ((Rindex >= 10) || (Lindex >= 10)) {
  181. stopRobot();
  182. break;
  183. }
  184. }
  185. // forward(32);
  186.  
  187. }
  188.  
  189. void turnLeft() {
  190. Rindex = 0;
  191. Lindex = 0;
  192.  
  193. if(dir == 4){
  194. dir -= 3;
  195. }
  196. else
  197. dir += 1;
  198.  
  199. while (Rindex < 10 && Lindex < 10) {
  200. servoRight.write(0);
  201. servoLeft.write(0);
  202. if ((Rindex >= 10) || (Lindex >= 10)) {
  203. stopRobot();
  204. break;
  205. }
  206. // forward(32);
  207.  
  208. }
  209. }
  210.  
  211. void turnLeft(int spokes){
  212. Rindex = 0;
  213. Lindex = 0;
  214. while(Rindex < spokes && Lindex < spokes){
  215. servoRight.write(0);
  216. servoLeft.write(0);
  217. if((Rindex >= spokes) || (Lindex >= spokes)){
  218. stopRobot();
  219. break;
  220. }
  221. }
  222. }
  223.  
  224. void stopRobot() {
  225. servoLeft.write(90);
  226. servoRight.write(90);
  227. }
  228.  
  229. void ltEncoder() {
  230. Lindex++;
  231. }
  232.  
  233. void rtEncoder() {
  234. Rindex++;
  235. }
  236.  
  237. double microsecondsToCentimeters(long microseconds){
  238. return (double) microseconds / 29 / 2;
  239. }
  240.  
  241. void distanceTest(){
  242. double duration, cm;
  243. digitalWrite(trigPin, LOW);
  244. delayMicroseconds(2);
  245. digitalWrite(trigPin, HIGH);
  246. delayMicroseconds(10);
  247. digitalWrite(trigPin, LOW);
  248.  
  249. duration = pulseIn(echoPin, HIGH);
  250. cm = microsecondsToCentimeters(duration);
  251. if( cm >= 4.9657 && cm <= 10){
  252. turnLeft(3);
  253. }
  254. if( cm <= 2.4257 && cm >= 0){
  255. turnRight(3);
  256. }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement