Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.43 KB | None | 0 0
  1. //Included for the Adafruit 8x8 LED Backpack
  2. #include <Wire.h>
  3. #include <Adafruit_GFX.h>
  4. #include "Adafruit_LEDBackpack.h"
  5.  
  6. Adafruit_8x8matrix matrix = Adafruit_8x8matrix();
  7.  
  8. //Sensor 1 define the pins
  9. const int trigPin = 9;
  10. const int echoPin = 10;
  11.  
  12. //Sensor 2 define the pins
  13. const int trigPin2 = 11;
  14. const int echoPin2 = 12;
  15.  
  16. //Sensor 3 define the pins
  17. const int trigPin3 = 7;
  18. const int echoPin3 = 8;
  19.  
  20. // define the variables
  21.  
  22. //Sensor 1
  23. long duration;
  24. int distance;
  25. int counter1;
  26.  
  27. //Sensor 2
  28. long duration2;
  29. int distance2;
  30. int counter2;
  31.  
  32. //Sesnor 3
  33. long duration3;
  34. int distance3;
  35. int counter5;
  36.  
  37. //Counter for both sensors when distance equals
  38. int counter3;
  39.  
  40. //Counter for loop
  41. int counter4;
  42.  
  43. void setup() {
  44. // put your setup code here, to run once:
  45. pinMode(trigPin, OUTPUT);
  46. pinMode(echoPin, INPUT);
  47.  
  48. pinMode(trigPin2, OUTPUT);
  49. pinMode(echoPin2, INPUT);
  50.  
  51. pinMode(trigPin3, OUTPUT);
  52. pinMode(echoPin3, INPUT);
  53.  
  54. Serial.begin(9600);
  55.  
  56. //Adafruit 8x8 LED Backpack address
  57. matrix.begin(0x70);
  58.  
  59. //Initialize the counters
  60. counter1 = 0;
  61. counter2 = 0;
  62. counter3 = 1; //counter3 is incremented +2 to accommodate difficulties the bot is having with the center sensor which might be due to its proximity between the left and right sensors. It seems to work more reliably.
  63. counter4 = 0;
  64. counter5 = 0;
  65.  
  66. //Set Adafruit 8x8 LED Backpack orientation
  67. matrix.setRotation(3);
  68. }
  69.  
  70. //Bitmaps for eye movement
  71. static const uint8_t PROGMEM
  72. eye_left[] =
  73. { B00111100,
  74. B01111110,
  75. B11111111,
  76. B00111111,
  77. B00111111,
  78. B11111111,
  79. B01111110,
  80. B00111100 },
  81. eye_center[] =
  82. { B00111100,
  83. B01111110,
  84. B11111111,
  85. B11100111,
  86. B11100111,
  87. B11111111,
  88. B01111110,
  89. B00111100 },
  90. eye_right[] =
  91. { B00111100,
  92. B01111110,
  93. B11111111,
  94. B11111100,
  95. B11111100,
  96. B11111111,
  97. B01111110,
  98. B00111100 };
  99.  
  100. static const uint8_t PROGMEM // Bitmaps are stored in program memory
  101. annoyedBlinkImg[]= { // Eye animation frames
  102. {
  103. B10000001, // Fully open annoyed eye
  104. B01100110,
  105. B00000000,
  106. B11111111,
  107. B11111111,
  108. B11111111,
  109. B01111110,
  110. B00111100 }
  111. ,
  112. {
  113. B10000001,
  114. B01100110,
  115. B00000000,
  116. B11111111,
  117. B11111111,
  118. B11111111,
  119. B01111110,
  120. B00000000 }
  121. ,
  122. {
  123. B10000001,
  124. B01100110,
  125. B00000000,
  126. B11111111,
  127. B11111111,
  128. B01111110,
  129. B00000000,
  130. B00000000 }
  131. ,
  132. {
  133. B10000001,
  134. B01100110,
  135. B00000000,
  136. B11111111,
  137. B01111110,
  138. B00000000,
  139. B00000000,
  140. B00000000 }
  141. ,
  142. {
  143. B10000001, // Fully closed annoyed eye
  144. B01100110,
  145. B00000000,
  146. B10000001,
  147. B01111110,
  148. B00000000,
  149. B00000000,
  150. B00000000 }
  151.  
  152. };
  153.  
  154. void loop() {
  155.  
  156. //Increment the loop counter
  157. counter4++;
  158.  
  159. //Sensor 1
  160. //Clears the trigPin
  161. digitalWrite(trigPin, LOW);
  162. delayMicroseconds(2);
  163.  
  164. //Sets the trigPin on HIGH state for 10 microseconds
  165. digitalWrite(trigPin, HIGH);
  166. delayMicroseconds(10);
  167. digitalWrite(trigPin, LOW);
  168.  
  169. //Reads the echoPin, returns the sound wave travel time in microseconds
  170. duration = pulseIn(echoPin, HIGH);
  171.  
  172. //Sensor 2
  173. digitalWrite(trigPin2, LOW);
  174. delayMicroseconds(2);
  175. digitalWrite(trigPin2, HIGH);
  176. delayMicroseconds(10);
  177. digitalWrite(trigPin2, LOW);
  178. duration2 = pulseIn(echoPin2, HIGH);
  179.  
  180. //Sensor 3
  181. digitalWrite(trigPin3, LOW);
  182. delayMicroseconds(2);
  183. digitalWrite(trigPin3, HIGH);
  184. delayMicroseconds(10);
  185. digitalWrite(trigPin3, LOW);
  186. duration3 = pulseIn(echoPin3, HIGH);
  187.  
  188. //Calculating the distance
  189. distance = duration*0.034/2;
  190. distance2 = duration2*0.034/2;
  191. distance3 = duration3*0.034/2;
  192.  
  193. //Prints the distanct on the Serial Monitor
  194. /*Serial.print("Distance: ");
  195. Serial.println(distance);
  196. Serial.print("Distance2: ");
  197. Serial.println(distance2);
  198. */
  199.  
  200. if (distance3 < distance && distance3 < distance2)
  201. {
  202. counter3++;
  203. /*Serial.print("MIDDLE");
  204. Serial.println();
  205. */
  206. }
  207. if (distance < distance2)
  208. {
  209. counter1++;
  210. /*Serial.print("RIGHT");
  211. Serial.println();
  212. */
  213. }
  214. if (distance > distance2)
  215. {
  216. counter2++;
  217. /*Serial.print("LEFT");
  218. Serial.println();
  219. */
  220. }
  221. if (counter4 == 5)
  222. {
  223. if (counter1 > counter2 && counter1 > counter3)
  224. {
  225. Serial.print("RIGHT");
  226. Serial.println();
  227. matrix.clear();
  228. matrix.drawBitmap(0, 0, eye_right, 8, 8, LED_ON);
  229. matrix.writeDisplay();
  230. matrix.clear();
  231. matrix.drawBitmap(0, 0, annoyedBlinkImg, 8, 8, LED_ON);
  232. matrix.writeDisplay();
  233. //delay(500);
  234. }
  235. if (counter2 > counter1 && counter2 > counter3)
  236. {
  237. Serial.print("LEFT");
  238. Serial.println();
  239. matrix.clear();
  240. matrix.drawBitmap(0, 0, eye_left, 8, 8, LED_ON);
  241. matrix.writeDisplay();
  242. //delay(500);
  243. }
  244. if (counter3 > counter1 && counter3 > counter2)
  245. {
  246. Serial.print("MIDDLE");
  247. Serial.println();
  248. matrix.clear();
  249. matrix.drawBitmap(0, 0, eye_center, 8, 8, LED_ON);
  250. matrix.writeDisplay();
  251. //delay(500);
  252. }
  253. //Reset the counters
  254. counter4 = 0;
  255. counter3 = 1; //counter3 is incremented +2 to accommodate difficulties the bot is having with the center sensor which might be due to its proximity between the left and right sensors. It seems to work more reliably.
  256. counter2 = 0;
  257. counter1 = 0;
  258. //counter5 = 0;
  259. }
  260. //delay(10);
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement