Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.65 KB | None | 0 0
  1. // all the defines below are just defining notes, these notes come from a song that is copied from http://pasted.co/e525c1b2
  2. #define C0 16.35
  3. #define Db0 17.32
  4. #define D0 18.35
  5. #define Eb0 19.45
  6. #define E0 20.60
  7. #define F0 21.83
  8. #define Gb0 23.12
  9. #define G0 24.50
  10. #define Ab0 25.96
  11. #define LA0 27.50
  12. #define Bb0 29.14
  13. #define B0 30.87
  14. #define C1 32.70
  15. #define Db1 34.65
  16. #define D1 36.71
  17. #define Eb1 38.89
  18. #define E1 41.20
  19. #define F1 43.65
  20. #define Gb1 46.25
  21. #define G1 49.00
  22. #define Ab1 51.91
  23. #define LA1 55.00
  24. #define Bb1 58.27
  25. #define B1 61.74
  26. #define C2 65.41
  27. #define Db2 69.30
  28. #define D2 73.42
  29. #define Eb2 77.78
  30. #define E2 82.41
  31. #define F2 87.31
  32. #define Gb2 92.50
  33. #define G2 98.00
  34. #define Ab2 103.83
  35. #define LA2 110.00
  36. #define Bb2 116.54
  37. #define B2 123.47
  38. #define C3 130.81
  39. #define Db3 138.59
  40. #define D3 146.83
  41. #define Eb3 155.56
  42. #define E3 164.81
  43. #define F3 174.61
  44. #define Gb3 185.00
  45. #define G3 196.00
  46. #define Ab3 207.65
  47. #define LA3 220.00
  48. #define Bb3 233.08
  49. #define B3 246.94
  50. #define C4 261.63
  51. #define Db4 277.18
  52. #define D4 293.66
  53. #define Eb4 311.13
  54. #define E4 329.63
  55. #define F4 349.23
  56. #define Gb4 369.99
  57. #define G4 392.00
  58. #define Ab4 415.30
  59. #define LA4 440.00
  60. #define Bb4 466.16
  61. #define B4 493.88
  62. #define C5 523.25
  63. #define Db5 554.37
  64. #define D5 587.33
  65. #define Eb5 622.25
  66. #define E5 659.26
  67. #define F5 698.46
  68. #define Gb5 739.99
  69. #define G5 783.99
  70. #define Ab5 830.61
  71. #define LA5 880.00
  72. #define Bb5 932.33
  73. #define B5 987.77
  74. #define C6 1046.50
  75. #define Db6 1108.73
  76. #define D6 1174.66
  77. #define Eb6 1244.51
  78. #define E6 1318.51
  79. #define F6 1396.91
  80. #define Gb6 1479.98
  81. #define G6 1567.98
  82. #define Ab6 1661.22
  83. #define LA6 1760.00
  84. #define Bb6 1864.66
  85. #define B6 1975.53
  86. #define C7 2093.00
  87. #define Db7 2217.46
  88. #define D7 2349.32
  89. #define Eb7 2489.02
  90. #define E7 2637.02
  91. #define F7 2793.83
  92. #define Gb7 2959.96
  93. #define G7 3135.96
  94. #define Ab7 3322.44
  95. #define LA7 3520.01
  96. #define Bb7 3729.31
  97. #define B7 3951.07
  98. #define C8 4186.01
  99. #define Db8 4434.92
  100. #define D8 4698.64
  101. #define Eb8 4978.03
  102.  
  103. // These are how long notes stay on
  104. #define BPM 240 // you can change this value changing all the others
  105. #define H 2*Q //half 2/4
  106. #define Q 60000/BPM //quarter 1/4
  107. #define E Q/2 //eighth 1/8
  108. #define S Q/4 // sixteenth 1/16
  109. #define W 4*Q // whole 4/4
  110.  
  111.  
  112. #include <Servo.h> // Include servo library
  113. Servo ServoLeft; // Declares left and right servos objects
  114. Servo ServoRight;
  115.  
  116. // define all pins used later
  117.  
  118. const int leftsensor = 11;
  119. const int rightsensor = 10;
  120.  
  121. const int leftled = 5;
  122.  
  123. const int rightled = 7;
  124.  
  125. const int buzz = 8;
  126.  
  127. const int button = 9;
  128.  
  129. bool hasstopped = false;
  130. // for ultrasonic sensor
  131. //int timesturned = 0;
  132. // for ultrasonic sensor
  133. //int trigPin = 3;
  134. // for ultrasonic sensor
  135. //int echoPin = 4;
  136. //second button for turning
  137. const int button2 = 2;
  138.  
  139. void setup() {
  140.  
  141. // initialize all pins used
  142. pinMode(leftsensor, INPUT);
  143. pinMode(rightsensor, INPUT);
  144. pinMode(leftled, OUTPUT);
  145. pinMode(rightled, OUTPUT);
  146. pinMode(buzz, OUTPUT);
  147. pinMode(button, INPUT_PULLUP);
  148. pinMode(button2, INPUT_PULLUP);
  149. //used for debugging not for when robot is running
  150. Serial.begin(9600);
  151.  
  152. }
  153.  
  154. void loop() {
  155. //Start the loop when he button is pressed
  156. if (digitalRead(button) == LOW) {
  157. while (true) {
  158.  
  159. ServoLeft.attach(12); // Attach servo signal to pin 12 from servoleft object
  160. ServoRight.attach(13);// Attach servo signal to pin 13 from servoright object
  161.  
  162. int leftdetect = digitalRead(leftsensor);
  163. int rightdetect = digitalRead(rightsensor);
  164.  
  165. if (leftdetect == 0 && rightdetect == 0) {
  166. ServoLeft.writeMicroseconds(1600);
  167. ServoRight.writeMicroseconds(1350);
  168. straightleds();
  169.  
  170. }
  171.  
  172. if (leftdetect == 1 && rightdetect == 0) {
  173. ServoLeft.writeMicroseconds(1700);
  174. ServoRight.writeMicroseconds(1600);
  175. leftledonly();
  176.  
  177. }
  178.  
  179. if (leftdetect == 0 && rightdetect == 1) {
  180. ServoLeft.writeMicroseconds(1300);
  181. ServoRight.writeMicroseconds(1400);
  182. rightledonly();
  183.  
  184. }
  185.  
  186.  
  187. if (leftdetect == 1 && rightdetect == 1) {
  188. ServoLeft.writeMicroseconds(1650);
  189. ServoRight.writeMicroseconds(1350);
  190. straightleds();
  191.  
  192.  
  193. }
  194.  
  195. int phototransistorvalue = rcTime(6);
  196.  
  197. if (phototransistorvalue < 500 && hasstopped == false) {
  198. left(600);
  199. forward(1400);
  200. right(400);
  201. forward(1150);
  202. left(400);
  203. ServoRight.detach();
  204. ServoLeft.detach();
  205. straightleds();
  206. // play song, the song is copied from http://pasted.co/e525c1b2
  207.  
  208. song();
  209.  
  210.  
  211. ServoLeft.attach(12);
  212. ServoRight.attach(13);
  213.  
  214. forward(400);
  215.  
  216. ServoLeft.detach();
  217. ServoRight.detach();
  218. ServoLeft.attach(12);
  219. ServoRight.attach(13);
  220.  
  221. hasstopped = true;
  222. }
  223. phototransistorvalue = rcTime(6);
  224. // take readings from phototransitor by charging and discharging capacitor, since the hasstopped boolean is true, shining light will not stop robot and will instead cause robot to turn
  225. if (phototransistorvalue < 500 && hasstopped == true) {
  226. while (phototransistorvalue < 500 && hasstopped == true) {
  227. phototransistorvalue = rcTime(6);
  228. ServoLeft.writeMicroseconds(1400);
  229. ServoRight.writeMicroseconds(1400);
  230. delay(10);
  231. if (digitalRead(button) == LOW) {
  232.  
  233. break;
  234. }
  235. }
  236.  
  237. ServoLeft.writeMicroseconds(1500);
  238. ServoRight.writeMicroseconds(1500);
  239. }
  240.  
  241. if (digitalRead(button2) == LOW) {
  242. while (digitalRead(button2) == LOW) {
  243.  
  244. ServoLeft.writeMicroseconds(1400);
  245. ServoRight.writeMicroseconds(1400);
  246. delay(10);
  247.  
  248. if (digitalRead(button) == LOW) {
  249. exit;
  250.  
  251. }
  252.  
  253. }
  254.  
  255. ServoLeft.writeMicroseconds(1500);
  256. ServoRight.writeMicroseconds(1500);
  257. }
  258.  
  259. }
  260. }
  261. }
  262.  
  263.  
  264. long rcTime(int pin) { // rcTime measures decay at pin, this is used to take readings from the phototransistor using a capacitor, for more sensitivity to light, for more accuracy, the phototransistor isn't triggered by leds
  265. // this was taken from BOE-booklet
  266. pinMode(pin, OUTPUT); // Charge capacitor
  267. digitalWrite(pin, HIGH); // ..by setting pin ouput-high
  268. delay(5); // ..for 5 ms
  269. pinMode(pin, INPUT); // Set pin to input
  270. digitalWrite(pin, LOW); // ..with no pullup
  271. long time = micros(); // Mark the time
  272. while (digitalRead(pin)); // Wait for voltage < threshold
  273. time = micros() - time; // Calculate decay time
  274. return time; // Returns decay time
  275. }
  276.  
  277. void forward(int timeinms) {
  278.  
  279. ServoLeft.writeMicroseconds(1700);
  280. ServoRight.writeMicroseconds(1300);
  281. delay (timeinms);
  282. straightleds();
  283. }
  284.  
  285. void left(int timeinms) {
  286.  
  287. ServoLeft.writeMicroseconds(1300);
  288. ServoRight.writeMicroseconds(1300);
  289. delay (timeinms);
  290. leftledonly();
  291. }
  292. void right(int timeinms) {
  293.  
  294. ServoLeft.writeMicroseconds(1700);
  295. ServoRight.writeMicroseconds(1700);
  296. delay(timeinms);
  297. rightledonly();
  298. }
  299. void straightleds() {
  300.  
  301. digitalWrite(leftled, HIGH);
  302. digitalWrite(rightled, HIGH);
  303. }
  304. void leftledonly() {
  305.  
  306. digitalWrite(leftled, HIGH);
  307. digitalWrite(rightled, LOW);
  308. }
  309. void rightledonly() {
  310.  
  311. digitalWrite(leftled, LOW);
  312. digitalWrite(rightled, HIGH);
  313. }
  314. tones
  315. void hightone() {
  316. tone(8, Eb8);
  317. delay(10);
  318. noTone(8);
  319. }
  320.  
  321. void lowtone() {
  322. tone(8, C0);
  323. delay(10);
  324. noTone(8);
  325. }
  326. void song(){
  327. tone(8, LA3, Q);
  328. delay(1 + Q); //minimum delay of 1ms required to separate tones
  329. tone(8, LA3, Q);
  330.  
  331.  
  332. delay(1 + Q);
  333. tone(8, LA3, Q);
  334. delay(1 + Q);
  335. tone(8, F3, E + S);
  336. delay(1 + E + S);
  337. tone(8, C4, S);
  338. delay(1 + S);
  339.  
  340. tone(8, LA3, Q);
  341. delay(1 + Q);
  342. tone(8, F3, E + S);
  343. delay(1 + E + S);
  344. tone(8, C4, S);
  345. delay(1 + S);
  346. tone(8, LA3, H);
  347. delay(1 + H);
  348.  
  349. tone(8, E4, Q);
  350. delay(1 + Q);
  351. tone(8, E4, Q);
  352. delay(1 + Q);
  353. tone(8, E4, Q);
  354. delay(1 + Q);
  355. tone(8, F4, E + S);
  356. delay(1 + E + S);
  357. tone(8, C4, S);
  358. delay(1 + S);
  359.  
  360. tone(8, Ab3, Q);
  361. delay(1 + Q);
  362. tone(8, F3, E + S);
  363. delay(1 + E + S);
  364. tone(8, C4, S);
  365. delay(1 + S);
  366. tone(8, LA3, H);
  367. delay(1 + H);
  368.  
  369. tone(8, LA4, Q);
  370. delay(1 + Q);
  371. tone(8, LA3, E + S);
  372. delay(1 + E + S);
  373. tone(8, LA3, S);
  374. delay(1 + S);
  375. tone(8, LA4, Q);
  376. delay(1 + Q);
  377. tone(8, Ab4, E + S);
  378. delay(1 + E + S);
  379. tone(8, G4, S);
  380. delay(1 + S);
  381.  
  382. tone(8, Gb4, S);
  383. delay(1 + S);
  384. tone(8, E4, S);
  385. delay(1 + S);
  386. tone(8, F4, E);
  387. delay(1 + E);
  388. delay(1 + E); //PAUSE
  389. tone(8, Bb3, E);
  390. delay(1 + E);
  391. tone(8, Eb4, Q);
  392. delay(1 + Q);
  393. tone(8, D4, E + S);
  394. delay(1 + E + S);
  395. tone(8, Db4, S);
  396. delay(1 + S);
  397.  
  398. tone(8, C4, S);
  399. delay(1 + S);
  400. tone(8, B3, S);
  401. delay(1 + S);
  402. tone(8, C4, E);
  403. delay(1 + E);
  404. delay(1 + E); //PAUSE QUASI FINE RIGA
  405. tone(8, F3, E);
  406. delay(1 + E);
  407. tone(8, Ab3, Q);
  408. delay(1 + Q);
  409. tone(8, F3, E + S);
  410. delay(1 + E + S);
  411. tone(8, LA3, S);
  412. delay(1 + S);
  413.  
  414. tone(8, C4, Q);
  415. delay(1 + Q);
  416. tone(8, LA3, E + S);
  417. delay(1 + E + S);
  418. tone(8, C4, S);
  419. delay(1 + S);
  420. tone(8, E4, H);
  421. delay(1 + H);
  422.  
  423. tone(8, LA4, Q);
  424. delay(1 + Q);
  425. tone(8, LA3, E + S);
  426. delay(1 + E + S);
  427. tone(8, LA3, S);
  428. delay(1 + S);
  429. tone(8, LA4, Q);
  430. delay(1 + Q);
  431. tone(8, Ab4, E + S);
  432. delay(1 + E + S);
  433. tone(8, G4, S);
  434. delay(1 + S);
  435.  
  436. tone(8, Gb4, S);
  437. delay(1 + S);
  438. tone(8, E4, S);
  439. delay(1 + S);
  440. tone(8, F4, E);
  441. delay(1 + E);
  442. delay(1 + E); //PAUSE
  443. tone(8, Bb3, E);
  444. delay(1 + E);
  445. tone(8, Eb4, Q);
  446. delay(1 + Q);
  447. tone(8, D4, E + S);
  448. delay(1 + E + S);
  449. tone(8, Db4, S);
  450. delay(1 + S);
  451.  
  452. tone(8, C4, S);
  453. delay(1 + S);
  454. tone(8, B3, S);
  455. delay(1 + S);
  456. tone(8, C4, E);
  457. delay(1 + E);
  458. delay(1 + E); //PAUSE QUASI FINE RIGA
  459. tone(8, F3, E);
  460. delay(1 + E);
  461. tone(8, Ab3, Q);
  462. delay(1 + Q);
  463. tone(8, F3, E + S);
  464. delay(1 + E + S);
  465. tone(8, C4, S);
  466. delay(1 + S);
  467.  
  468. tone(8, LA3, Q);
  469. delay(1 + Q);
  470. tone(8, F3, E + S);
  471. delay(1 + E + S);
  472. tone(8, C4, S);
  473. delay(1 + S);
  474. tone(8, LA3, H);
  475. delay(1 + H);
  476.  
  477. delay(2 * H);
  478. }
  479.  
  480. /*All code below was for the sonic distance sensor and was intended to be used for going around the pylon
  481. but the sensor stopped functioning properly the day before the exam and began giving false readings, we then decided
  482. to just use the photoresistor for the stop and the turn
  483. */
  484.  
  485.  
  486. void doStuff()
  487. {
  488. //Configure pins and create a variable
  489. long duration, inches, cm;
  490. pinMode(trigPin, OUTPUT);
  491. //Send out a pulse (soundwave) and measure in microseconds how long it takes to return
  492. digitalWrite(trigPin, LOW);
  493. delayMicroseconds(2);
  494. digitalWrite(trigPin, HIGH);
  495. delayMicroseconds(10);
  496. digitalWrite(trigPin, LOW);
  497. pinMode(echoPin, INPUT);
  498. duration = pulseIn(echoPin, HIGH);
  499. //convert microseconds to inches and centimeters
  500. inches = microsecondsToInches(duration);
  501. cm = microsecondsToCentimeters(duration);
  502. //Print out distance to object in inches and centimeters in the serial monitor
  503. Serial.print(inches);
  504. Serial.print("in, ");
  505. Serial.print(cm);
  506. Serial.print("cm");
  507. Serial.println();
  508. //If the object is closer than 10cm, go forward and make a 90 degree turn, turn a maximum of 5 times
  509. if (cm <= 10 && timesturned <= 4) {
  510. left(600);
  511. forward(1000);
  512.  
  513. timesturned ++;
  514. }
  515. else {
  516.  
  517. }
  518. }
  519. //converting microseconds to centimeters and inches using math
  520. long microsecondsToInches(long microseconds)
  521. {
  522. return microseconds / 74 / 2;
  523. }
  524.  
  525. long microsecondsToCentimeters(long microseconds)
  526. {
  527. return microseconds / 29 / 2;
  528. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement