Advertisement
jpglickwebber

Adjusting Circle

Apr 23rd, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. // Robotics with the BOE Shield - RoamingWithWhiskers
  2. // Go forward. Back up and turn if whiskers indicate BOE Shield bot bumped
  3. // into something.
  4.  
  5. #include <Servo.h> // Include servo library
  6.  
  7. Servo servoLeft; // Declare left and right servos
  8. Servo servoRight;
  9.  
  10. int leftSpeed = 1700;
  11. int rightSpeed = 1455;
  12.  
  13. void setup() // Built-in initialization block
  14. {
  15. pinMode(7, INPUT); // Set right whisker pin to input
  16. pinMode(5, INPUT); // Set left whisker pin to input
  17. pinMode(8, OUTPUT); // Left LED indicator -> output
  18. pinMode(2, OUTPUT); // Right LED indicator -> output
  19.  
  20. tone(4, 3000, 1000); // Play tone for 1 second
  21. delay(1000); // Delay to finish tone
  22.  
  23. servoLeft.attach(12); // Attach left signal to pin 13
  24. servoRight.attach(11); // Attach right signal to pin 12
  25. }
  26.  
  27. void loop() // Main loop auto-repeats
  28. {
  29. byte wLeft = digitalRead(5); // Copy left result to wLeft
  30. byte wRight = digitalRead(7); // Copy right result to wRight
  31. if(wLeft == 0) // If left whisker contact
  32. {
  33. digitalWrite(8, HIGH); // Left LED on
  34. }
  35. else // if no left whisker contact
  36. {
  37. digitalWrite(8, LOW); // Left LED off
  38. }
  39.  
  40. if(wRight == 0) // If right whisker contact
  41. {
  42. digitalWrite(2, HIGH); // Right LED on
  43. }
  44. else // If no right whisker contact
  45. {
  46. digitalWrite(2, LOW); // Right LED off
  47. }
  48. if((wLeft == 0) && (wRight == 0)) // If both whiskers contact
  49. {
  50. servoLeft.writeMicroseconds(1500);
  51. servoRight.writeMicroseconds(1500);
  52. song();
  53. }
  54. else if(wLeft == 0) // If only left whisker contact
  55. {
  56. rightSpeed-=5;
  57. delay(200);
  58. }
  59. else if(wRight == 0) // If only right whisker contact
  60. {
  61. rightSpeed+=5;
  62. delay(200);
  63. }
  64. else // Otherwise, no whisker contact
  65. {
  66. servoLeft.writeMicroseconds(leftSpeed);
  67. servoRight.writeMicroseconds(rightSpeed);
  68. delay(200);
  69. }
  70. }
  71.  
  72. void song(){
  73. //measure 1
  74. tone(4, 739.99, 500);
  75. delay(500);
  76. noTone(4);
  77. delay(500);
  78. tone(4, 739.99, 250);
  79. delay(250);
  80. tone(4, 783.99, 250);
  81. delay(250);
  82. tone(4, 739.99, 250);
  83. delay(250);
  84. tone(4, 739.99, 250);
  85. delay(250);
  86. //measure 2
  87. tone(4, 659.25, 500);
  88. delay(500);
  89. noTone(4);
  90. delay(500);
  91. delay(500);
  92. delay(250);
  93. tone(4, 739.99, 750); //measure 3 starts in here
  94. delay(750);
  95. delay(250);
  96. tone(4, 783.99, 750);
  97. delay(750);
  98. tone(4, 987.77, 250);
  99. delay(250);
  100. tone(4, 783.99, 250);
  101. delay(250);
  102. tone(4, 739.99, 250);
  103. delay(250);
  104. //measure 4
  105. tone(4, 659.25, 1000);
  106. delay(1000);
  107. tone(4, 987.77, 750);
  108. delay(750);
  109. tone(4, 739.99, 750);//measure 5 starts in here
  110. delay(750);
  111. noTone(4);
  112. delay(250);
  113. tone(4, 739.99, 500);
  114. delay(500);
  115. tone(4, 783.99, 250);
  116. delay(250);
  117. tone(4, 739.99, 250);
  118. delay(250);
  119. tone(4, 783.99, 250);
  120. delay(250);
  121. //measures 6 and 7
  122. tone(4, 987.77, 2000);
  123. delay(2000);
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement