Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.60 KB | None | 0 0
  1. //We have adhered to the Duke Community Standard in Completing this Assignment
  2. //Signed: Cameron Jarnot, Sophie Rubin
  3.  
  4. long rcTime(int pin, boolean print); //QTI values
  5. #include <Servo.h> //servos
  6. #include <Wire.h>
  7. #include "Adafruit_TCS34725.h" //RGB
  8. #define Rx 17 //Receiving
  9. #define Tx 16 //Transmitting
  10.  
  11. const int TxPin = 10; //transmit pin
  12.  
  13. #include <SoftwareSerial.h> //include communication
  14. SoftwareSerial mySerial = SoftwareSerial(255, TxPin); //set communication pins
  15.  
  16. int paddles = 0; //to count how far along the path it is
  17. String colors = ""; //to record the paddle colors
  18. char myScore = 'f'; //set score to zero
  19. Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X); //initialize RGB
  20.  
  21. Servo servoLeft; //turn on left servo
  22. Servo servoRight; //turn on right servo
  23.  
  24. boolean seeker = false; //waiting for seeker score
  25. boolean p1 = false; //waiting for other pair's score
  26. boolean finished = false; //waiting to have all scores
  27. boolean happening = false; //tracking whether or not it has score with chaser
  28.  
  29. void setup() {
  30.  
  31. pinMode(7, OUTPUT); //non-built in LED
  32. pinMode(44, OUTPUT); //red
  33. pinMode(46, OUTPUT); //green
  34. pinMode(49,OUTPUT); //left QTI
  35. pinMode(51,OUTPUT); //center QTI
  36. pinMode(53,OUTPUT); //right QTI
  37. Serial.begin(9600);
  38. delay(300);
  39. servoLeft.attach(12); //connect to left servo
  40. servoRight.attach(11); //connect to right servo
  41.  
  42. pinMode(LED_BUILTIN, OUTPUT);
  43. Serial.begin(9600);
  44. Serial2.begin(9600);
  45. delay(500);
  46.  
  47. Serial3.begin(9600);
  48. delay(100);
  49. Serial3.write(17); // Turn backlight on
  50. delay(5); // Required delay
  51. Serial3.write(18); // Turn backlight off
  52.  
  53. }
  54.  
  55. void loop() {
  56.  
  57. digitalWrite(44, LOW); //red on
  58. digitalWrite(46, LOW); //green on
  59.  
  60. long valueL = rcTime(49, false); //left QTI
  61. long valueM = rcTime(51, false); //center QTI
  62. long valueR = rcTime(53, false); //right QTI
  63.  
  64. boolean qtL = true; //left QTI sense (true for black)
  65. boolean qtM = true; //middle QTI sense (true for black)
  66. boolean qtR = true; //right QTI sense (true for black)
  67. if(valueL<300){ //if left rcTime less than threshold
  68. qtL = false; //it sees white
  69. }
  70. if(valueM<300){ //if center rcTime less than threshold
  71. qtM = false; //it sees white
  72. }
  73. if(valueR<300){ //if right rcTime less than threshold
  74. qtR = false; //it sees white
  75. }
  76.  
  77. if(!qtL && qtM && !qtR){ //if only middle black
  78. servoLeft.writeMicroseconds(1540); //move forward
  79. servoRight.writeMicroseconds(1460);
  80. delay(5);
  81. }
  82. else if(qtR && !qtL){ //if left white and right black
  83. servoLeft.writeMicroseconds(1700); //turn right
  84. servoRight.writeMicroseconds(1500);
  85. delay(5);
  86. }
  87. else if(!qtR && qtL){ //if right white and left black
  88. servoLeft.writeMicroseconds(1500); //turn left
  89. servoRight.writeMicroseconds(1300);
  90. delay(5);
  91. }
  92.  
  93. else if(qtL && qtM && qtR){ //if all black
  94. servoLeft.writeMicroseconds(1500); //stall, no movement
  95. servoRight.writeMicroseconds(1500);
  96. paddles+=1; //track one more hash
  97. // Serial.println("paddles: ");
  98. // Serial.println(paddles);
  99. uint16_t clear, red, green, blue; //see color from RGB
  100. delay(400);
  101. tcs.getRawData(&red, &green, &blue, &clear); //collect RGB data
  102.  
  103. if(red>=green){ //if it is more red than green
  104. char outgoing = 'r'; //add red character to string
  105. // Serial.println("outgoing: ");
  106. // Serial.println(outgoing);
  107. colors += "r"; //track that paddle was red
  108. //Serial2.println('r');
  109. digitalWrite(46, HIGH); //blink darker blue
  110. delay(500);
  111. digitalWrite(46, LOW);
  112. delay(500); //wait .5sec
  113. }
  114. else if(green>red){ //if it is more green than red
  115. char outgoing = 'g'; //add green character to string
  116. // Serial.println("outgoing: ");
  117. // Serial.println(outgoing);
  118. colors += "g"; //track that paddle was green
  119. //Serial2.println('g');
  120. digitalWrite(44, HIGH); //blink green
  121. delay(500);
  122. digitalWrite(44, LOW);
  123. delay(500); //wait .5sec
  124. }
  125.  
  126. servoLeft.writeMicroseconds(1540); //move forward
  127. servoRight.writeMicroseconds(1460);
  128. delay(400); //delay .2sec
  129. }
  130.  
  131. if(paddles == 5) { //if it has sensed all five paddles
  132.  
  133. digitalWrite(46, HIGH); //turn off built in LED
  134. digitalWrite(44, HIGH);
  135.  
  136. servoLeft.writeMicroseconds(1500-(0.3*200)); //back up at the end
  137. servoRight.writeMicroseconds(1500+(0.3*200));
  138. delay(1200);
  139. servoLeft.writeMicroseconds(1500); //stop
  140. servoRight.writeMicroseconds(1500);
  141. digitalWrite(7, HIGH);
  142. //Serial3.print(colors);
  143. delay(3000);
  144.  
  145. for(int i=0; i<5; i++){ //for each character in colors
  146. char outgoing = colors.charAt(i); //next letter of colors
  147. Serial2.print(outgoing); //send char
  148. //Serial3.print(outgoing); //print char to LCD
  149. //Serial3.write(12); //clear LCD
  150. delay(500); //wait .5sec
  151. }
  152.  
  153. String chaser = ""; //create string for chaser's letters
  154. int score = 0; //initialize score
  155. while(!happening){
  156. if(Serial2.available()){ //if another robot is sending
  157. char in = Serial2.read(); //receive letter
  158. if(in == 's' || in == 'q'){ //if it's the chaser
  159. chaser += in; //add the letter to chaser string
  160. //Serial3.print(in); //print letter to LCD
  161. }
  162. // Serial.println(chaser);
  163. }
  164. if(chaser.length() == 5){ //once all 5 have been received
  165. delay(50);
  166. for(int i=0; i<5; i++){ //for each letter in colors and chaser
  167. if(colors.charAt(i) == 'r' && chaser.charAt(i) == 's'){ //if quaffle and red
  168. score += 10; //add ten to score
  169. }
  170. }
  171. //Serial3.write(12); //clear LCD
  172. //Serial3.print("Score: ");
  173. //Serial3.print(score); //print score
  174. happening = true; //has score with chaser
  175. }
  176.  
  177. }
  178.  
  179. while(!finished){ //while it doesn't have the total
  180. delay(1);
  181. if(Serial2.available()){ //if someone is sending
  182. char seeIn = Serial2.read(); //read the character
  183.  
  184. if(! p1){ //if p1 score hasn't been received
  185. if(seeIn == 'A'){ //if 'A' is sent
  186. score += 10; //add ten to score
  187. p1 = true; //has received p1
  188. }
  189. if(seeIn == 'B'){ //if 'B' is sent
  190. score += 20; //add twenty to score
  191. p1 = true; //has received p1
  192. }
  193. if(seeIn == 'C'){ //if 'C' is sent
  194. score += 30; //add thirty to score
  195. p1 = true; //has received p1
  196. }
  197. if(seeIn == 'D'){ //if 'D' is sent
  198. score += 40; //add forty to score
  199. p1 = true; //has received p1
  200. }
  201. if(seeIn == 'E'){ //if 'E' is sent
  202. score += 50; //add fifty to score
  203. p1 = true; //has received p1
  204. }
  205. if(seeIn == 'F'){ //if 'F' is sent
  206. p1 = true; //do not add to score, has received p1
  207. }
  208. }
  209.  
  210. if(! seeker){ //if seeker score hasn't been received
  211. if(seeIn == 'x'){ //if 'x' is received
  212. seeker = true; //do not add to score, has received seeker
  213. }
  214. if(seeIn == 'z'){ //if 'z' is received
  215. score += 150; //add 150 to score
  216. seeker = true; //has received seeker
  217. }
  218. }
  219.  
  220. delay(1);
  221. if(p1 && seeker){ //if it has all bots' scores
  222. Serial3.write(12); //clear screen
  223. delay(5);
  224. Serial3.print("Total score: ");
  225. Serial3.println(score); //print total score
  226. Serial3.print(colors); //print paddles' colors seen
  227. finished = true; //consider computation complete
  228. }
  229. }
  230.  
  231. while(finished){ //while computation is complete
  232. Serial3.write(12); //clear screen
  233. Serial3.print("Total score: "); //print total score
  234. Serial3.println(score); //print total score
  235. Serial3.print(colors); //print paddles' colors seen
  236. delay(1000);
  237.  
  238. }
  239. }
  240.  
  241. }
  242.  
  243. }
  244.  
  245. long rcTime(int pin, boolean print) { //calculate QTI values
  246. pinMode(pin, OUTPUT); //send IR signal
  247. digitalWrite(pin, HIGH);
  248. delayMicroseconds(230);
  249. pinMode(pin, INPUT); //receive IR signal
  250. digitalWrite(pin, LOW); long
  251. time = micros(); while
  252. (digitalRead(pin)); time =
  253. micros() - time; //calculate time constant
  254. return time;
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement