Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. #include <SendOnlySoftwareSerial.h>
  2. #include <ArduinoInit.h>
  3.  
  4.  
  5. boolean forwards = true, black = true, targetFound = false;
  6. unsigned int sensor0, sensor1, sensor2, sensor3;
  7. unsigned int floorThreshold = 20000;
  8. unsigned int targetThreshold = 18000;
  9. unsigned int navThreshold = 38000;
  10. unsigned int homecolorFunc();
  11. boolean currentcolor;
  12. boolean homecolor;
  13.  
  14.  
  15. void setup()
  16. {
  17. configArduino ();
  18. outputHigh (4);
  19.  
  20. attachInterrupt (0, bumper_int, LOW);
  21. attachInterrupt (1, bumper_int, LOW);
  22. floorSensor();
  23.  
  24. //calls sensor to determine homecolor
  25. sensor0 = readADC(0);
  26. if (sensor0 >= floorThreshold) homecolor = 0; //0=black 1=white
  27. else homecolor = 1;
  28. }
  29.  
  30. void loop()
  31. {
  32.  
  33. sensor0 = readADC(0); //floor
  34. Serial.print("Floor: ");
  35. Serial.println(sensor0);
  36. sensor1 = readADC(1); //nav
  37. Serial.print("Nav: ");
  38. Serial.println(sensor1);
  39. sensor2 = readADC(2); //right
  40. Serial.print("Right: ");
  41. Serial.println(sensor2);
  42. sensor3 = readADC(3); //left
  43. Serial.print("Left: ");
  44. Serial.println(sensor3);
  45.  
  46. floorSensor();
  47. if (homecolor == currentcolor)
  48. {
  49. navLight();
  50. }
  51. else
  52. {
  53. if (!targetFound)
  54. {
  55. targetLight();
  56. }
  57. }
  58.  
  59. pause(200);
  60. }
  61.  
  62.  
  63. void targetLight ()
  64. {
  65. if (sensor2 < targetThreshold)
  66. {
  67. targetFound = true;
  68. right();
  69. pause(200); //TODO Determine this
  70. halt();
  71. pause(100);
  72. forward();
  73. }
  74. else if (sensor3 < targetThreshold)
  75. {
  76. targetFound = true;
  77. left();
  78. pause(400); //TODO Determine this
  79. halt();
  80. pause(100);
  81. forward();
  82. }
  83. }
  84.  
  85. void navLight()
  86. {
  87. if (sensor1 > navThreshold)
  88. {
  89. right();
  90. }
  91. else
  92. {
  93. forward();
  94. }
  95. }
  96.  
  97. void floorSensor()
  98. {
  99. if (sensor0 >= floorThreshold)
  100. {
  101. currentcolor = 0; //0=black
  102. }
  103. else
  104. {
  105. currentcolor = 1;
  106. }
  107. }
  108.  
  109.  
  110.  
  111. void bumper_int () // This function will be automatically called upon an interrupt
  112. { // 1 means free, 0 means pressed
  113. pause (5); // Pause 5 msec to allow for bumper switch contact bounce
  114. if (readInput (2) == 1 && readInput (3) == 1)
  115. return; // No bumper hit, return
  116. halt(); //Turn both motors off, retain the last speed setting
  117. pause (100); //Always allow time (100ms) for the motors to stop
  118. while (readInput (2) == 0 || readInput (3) == 0) //While a bumper is hit
  119. {
  120. if (targetFound)
  121. {
  122. targetFound = false;
  123. }
  124. if (readInput(2) == 0)
  125. {
  126. reverse();
  127. pause(100);
  128. halt();
  129. pause(100);
  130. left();
  131. pause(200);
  132. halt();
  133. pause(100);
  134. }
  135. else if (readInput(3) == 0)
  136. {
  137. reverse();
  138. pause(100);
  139. halt();
  140. pause(100);
  141. right();
  142. pause(200);
  143. halt();
  144. pause(100);
  145. }
  146. }
  147. forward();
  148. return;
  149. }
  150.  
  151. void reverse()
  152. {
  153. motors('1', 'a', 70);
  154. motors('2', 'a', 70);
  155. }
  156. void left()
  157. {
  158. motors('1', 'b', 25);
  159. motors('2', 'a', 25);
  160. }
  161. void right()
  162. {
  163. motors('1', 'a', 25);
  164. motors('2', 'b', 25);
  165. }
  166. void halt()
  167. {
  168. motors('1', 'o', 0);
  169. motors('2', 'o', 0);
  170. }
  171. void forward()
  172. {
  173. motors('1', 'b', 70);
  174. motors('2', 'b', 70);
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement