Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.04 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 has
  97. uint16_t clear, red, green, blue; //see color from RGB
  98. delay(400);
  99. tcs.getRawData(&red, &green, &blue, &clear); //collect RGB data
  100.  
  101. if(red>=green){ //if it is more red than green
  102. char outgoing = 'r'; //add red character to string
  103. colors += "r"; //track that paddle was red
  104. //Serial2.println('r');
  105. digitalWrite(46, HIGH); //blink darker blue
  106. delay(500);
  107. digitalWrite(46, LOW);
  108. delay(500); //wait .5sec
  109. }
  110. else if(green>red){ //if it is more green than red
  111. char outgoing = 'g'; //add green character to string
  112. colors += "g"; //track that paddle was green
  113. //Serial2.println('g');
  114. digitalWrite(44, HIGH); //blink green
  115. delay(500);
  116. digitalWrite(44, LOW);
  117. delay(500); //wait .5sec
  118. }
  119.  
  120. servoLeft.writeMicroseconds(1540); //move forward
  121. servoRight.writeMicroseconds(1460);
  122. delay(400); //delay .2sec
  123. }
  124.  
  125. if(paddles == 5) { //if it has sensed all five paddles
  126.  
  127. digitalWrite(46, HIGH); //turn off built in LED
  128. digitalWrite(44, HIGH);
  129.  
  130. servoLeft.writeMicroseconds(1500-(0.3*200)); //back up at the end
  131. servoRight.writeMicroseconds(1500+(0.3*200));
  132. delay(1200);
  133. servoLeft.writeMicroseconds(1500); //stop
  134. servoRight.writeMicroseconds(1500);
  135. digitalWrite(7, HIGH);
  136. delay(3000);
  137.  
  138. for(int i=0; i<5; i++){ //for each character in colors
  139. char outgoing = colors.charAt(i); //next letter of colors
  140. Serial2.print(outgoing); //send char
  141. delay(500); //wait .5sec
  142. }
  143.  
  144. String chaser = ""; //create string for chaser's letters
  145. int score = 0; //initialize score
  146. while(!happening){
  147. if(Serial2.available()){ //if another robot is sending
  148. char in = Serial2.read(); //receive letter
  149. if(in == 's' || in == 'q'){ //if it's the chaser
  150. chaser += in; //add the letter to chaser string
  151. }
  152. }
  153. if(chaser.length() == 5){ //once all 5 have been received
  154. delay(50);
  155. for(int i=0; i<5; i++){ //for each letter in colors and chaser
  156. if(colors.charAt(i) == 'r' && chaser.charAt(i) == 's'){ //if quaffle and red
  157. score += 10; //add ten to score
  158. }
  159. }
  160. happening = true; //has score with chaser
  161. }
  162.  
  163. }
  164.  
  165. while(!finished){ //while it doesn't have the total
  166. delay(1);
  167. if(Serial2.available()){ //if someone is sending
  168. char seeIn = Serial2.read(); //read the character
  169.  
  170. if(! p1){ //if p1 score hasn't been received
  171. if(seeIn == 'A'){ //if 'A' is sent
  172. score += 10; //add ten to score
  173. p1 = true; //has received p1
  174. }
  175. if(seeIn == 'B'){ //if 'B' is sent
  176. score += 20; //add twenty to score
  177. p1 = true; //has received p1
  178. }
  179. if(seeIn == 'C'){ //if 'C' is sent
  180. score += 30; //add thirty to score
  181. p1 = true; //has received p1
  182. }
  183. if(seeIn == 'D'){ //if 'D' is sent
  184. score += 40; //add forty to score
  185. p1 = true; //has received p1
  186. }
  187. if(seeIn == 'E'){ //if 'E' is sent
  188. score += 50; //add fifty to score
  189. p1 = true; //has received p1
  190. }
  191. if(seeIn == 'F'){ //if 'F' is sent
  192. p1 = true; //do not add to score, has received p1
  193. }
  194. }
  195.  
  196. if(! seeker){ //if seeker score hasn't been received
  197. if(seeIn == 'x'){ //if 'x' is received
  198. seeker = true; //do not add to score, has received seeker
  199. }
  200. if(seeIn == 'z'){ //if 'z' is received
  201. score += 150; //add 150 to score
  202. seeker = true; //has received seeker
  203. }
  204. }
  205.  
  206. delay(1);
  207. if(p1 && seeker){ //if it has all bots' scores
  208. Serial3.write(12); //clear screen
  209. delay(5);
  210. Serial3.print("Total score: ");
  211. Serial3.println(score); //print total score
  212. Serial3.print(colors); //print paddles' colors seen
  213. finished = true; //consider computation complete
  214. }
  215. }
  216.  
  217. while(finished){ //while computation is complete
  218. Serial3.write(12); //clear screen
  219. Serial3.print("Total score: "); //print total score
  220. Serial3.println(score); //print total score
  221. Serial3.print(colors); //print paddles' colors seen
  222. delay(1000);
  223.  
  224. }
  225. }
  226.  
  227. }
  228.  
  229. }
  230.  
  231. long rcTime(int pin, boolean print) { //calculate QTI values
  232. pinMode(pin, OUTPUT); //send IR signal
  233. digitalWrite(pin, HIGH);
  234. delayMicroseconds(230);
  235. pinMode(pin, INPUT); //receive IR signal
  236. digitalWrite(pin, LOW); long
  237. time = micros(); while
  238. (digitalRead(pin)); time =
  239. micros() - time; //calculate time constant
  240. return time;
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement