Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. /** This example uses the Zumo's line sensors to detect the black
  2. border around a sumo ring. When the border is detected, it
  3. backs up and turns. */
  4.  
  5. const int c = 261;
  6. const int d = 294;
  7. const int e = 329;
  8. const int f = 349;
  9. const int g = 391;
  10. const int gS = 415;
  11. const int a = 440;
  12. const int aS = 455;
  13. const int b = 466;
  14. const int cH = 523;
  15. const int cSH = 554;
  16. const int dH = 587;
  17. const int dSH = 622;
  18. const int eH = 659;
  19. const int fH = 698;
  20. const int fSH = 740;
  21. const int gH = 784;
  22. const int gSH = 830;
  23. const int aH = 880;
  24.  
  25.  
  26. #include <Wire.h>
  27. #include <Zumo32U4.h>
  28.  
  29. // This might need to be tuned for different lighting conditions,
  30. // surfaces, etc.
  31. #define QTR_THRESHOLD 700 // microseconds
  32.  
  33. // These might need to be tuned for different motor types.
  34. #define REVERSE_SPEED 200 // 0 is stopped, 400 is full speed
  35. #define TURN_SPEED 200
  36. #define FORWARD_SPEED 200
  37. #define REVERSE_DURATION 200 // ms
  38. #define TURN_DURATION 300 // ms
  39.  
  40. int left, right, centerL, centerR;
  41.  
  42. Zumo32U4LCD lcd;
  43. Zumo32U4ButtonB buttonB;
  44. Zumo32U4Buzzer buzzer;
  45. Zumo32U4Motors motors;
  46. Zumo32U4LineSensors lineSensors;
  47. Zumo32U4ProximitySensors proxSensors;
  48.  
  49. const char sound_effect[] PROGMEM = "O4 T100 V15 L4 MS g12>c12>e12>G6>E12 ML>G2";
  50.  
  51. #define NUM_SENSORS 3
  52. unsigned int lineSensorValues[NUM_SENSORS];
  53. void firstSection();
  54. void waitForButtonAndCountDown();
  55.  
  56.  
  57. void setup()
  58. {
  59. Serial.begin(9600);
  60. lineSensors.initThreeSensors();
  61. proxSensors.initThreeSensors();
  62. waitForButtonAndCountDown();
  63. }
  64.  
  65. void loop()
  66. {
  67. if (buttonB.isPressed())
  68. {
  69. // If button is pressed, stop and wait for another press to
  70. // go again.
  71. motors.setSpeeds(0, 0);
  72. buttonB.waitForRelease();
  73. waitForButtonAndCountDown();
  74. }
  75.  
  76. /* 6. Read line sensor values */
  77. lineSensors.read(lineSensorValues);
  78. proxSensors.read();
  79. left=proxSensors.countsLeftWithLeftLeds();
  80. right=proxSensors.countsRightWithRightLeds();
  81. centerL=proxSensors.countsFrontWithLeftLeds();
  82. centerR=proxSensors.countsFrontWithRightLeds();
  83.  
  84. //Serial.println("left");
  85. //Serial.println(left);
  86. //Serial.println("right");
  87. //Serial.println(right);
  88.  
  89. //Serial.println(centerL);
  90. //Serial.println(centerR);
  91. //int sensorValue = lineSensorValues[0];
  92.  
  93.  
  94. //Serial.println(sensorValue);
  95.  
  96.  
  97.  
  98.  
  99. if (lineSensorValues[0] < QTR_THRESHOLD) //flip aligator sign if you want to detect white edges on a black board instead of black border on a white board.
  100. {
  101. // If leftmost sensor detects line, reverse and turn to the
  102. // right.
  103. motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
  104. delay(REVERSE_DURATION);
  105.  
  106. /* 7. Add three code lines for setting turn speed to turn right (+/- TURN_SPEED),
  107. * add turn duration delay and then continuing forward.
  108. */
  109. motors.setSpeeds(TURN_SPEED , -TURN_SPEED );
  110. delay(TURN_DURATION);
  111.  
  112.  
  113. }
  114. else if (lineSensorValues[NUM_SENSORS - 1] < QTR_THRESHOLD) //flip aligator sign if you want to detect white edges on a black board instead of black border on a white board.
  115. {
  116. /* 8. If rightmost sensor detects line, reverse and turn to the
  117. left. */
  118. motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
  119. delay(REVERSE_DURATION);
  120.  
  121. /* 7. Add three code lines for setting turn speed to turn right (+/- TURN_SPEED),
  122. * add turn duration delay and then continuing forward.
  123. */
  124. motors.setSpeeds(-TURN_SPEED , TURN_SPEED );
  125. delay(TURN_DURATION);
  126.  
  127. }
  128. else if(left>2){
  129. motors.setSpeeds(-TURN_SPEED , TURN_SPEED );
  130. delay(TURN_DURATION);
  131.  
  132. motors.setSpeeds(FORWARD_SPEED , FORWARD_SPEED );
  133. delay(100);
  134. }
  135. else if(right>2){
  136. motors.setSpeeds(TURN_SPEED , -TURN_SPEED );
  137. delay(TURN_DURATION);
  138.  
  139.  
  140. motors.setSpeeds(FORWARD_SPEED , FORWARD_SPEED );
  141. delay(100);
  142.  
  143. }
  144. else if(centerL>2){
  145. motors.setSpeeds(FORWARD_SPEED , FORWARD_SPEED );
  146. delay(100);
  147. }
  148. else if(centerR>2){
  149. motors.setSpeeds(FORWARD_SPEED , FORWARD_SPEED );
  150. delay(100);
  151. }
  152. else
  153. {
  154. /* 9. Otherwise, go straight.*/
  155. motors.setSpeeds(FORWARD_SPEED , FORWARD_SPEED );
  156.  
  157. }
  158.  
  159. }
  160.  
  161. void waitForButtonAndCountDown()
  162. {
  163. /* 1. See setup() */
  164. /* 2. Turn on yellow LED, clear LCD and add "Press B" text to LCD */
  165. ledYellow(1);
  166. lcd.print(F("Press B"));
  167.  
  168.  
  169. /* 3. Wait for button B to be pressed */
  170. buttonB.waitForButton();
  171.  
  172.  
  173. /* 4. Turn off Yellow LED and clear LCD */
  174. ledYellow(0);
  175.  
  176. // Play audible countdown.
  177. delay(100);
  178. buzzer.playNote(NOTE_A(4), 500, 15);
  179. delay(500);
  180. buzzer.playNote(NOTE_A(4), 500, 15);
  181. delay(500);
  182. buzzer.playNote(NOTE_A(4), 500, 15);
  183. delay(500);
  184. buzzer.playNote(NOTE_F(4), 350, 15);
  185. delay(500);
  186. buzzer.playNote(NOTE_C(4), 150, 15);
  187.  
  188. }
  189.  
  190. void firstSection()
  191. {
  192. buzzer.playFrequency(a, 3000, 15);
  193. delay(100);
  194. buzzer.playFrequency(f, 3000, 15);
  195. delay(100);
  196. buzzer.playFrequency(a, 3000, 15);
  197. delay(100);
  198. buzzer.playFrequency(f, 210, 15);
  199. buzzer.playFrequency(cH, 90, 15);
  200. buzzer.playFrequency(a, 300, 15);
  201. buzzer.playFrequency(f, 210, 15);
  202. buzzer.playFrequency(cH, 90, 15);
  203. buzzer.playFrequency(a, 390, 15);
  204. //3650
  205.  
  206. delay(3000);
  207.  
  208. buzzer.playFrequency(eH, 300, 15);
  209. buzzer.playFrequency(eH, 300, 15);
  210. buzzer.playFrequency(eH, 300, 15);
  211. buzzer.playFrequency(fH, 210, 15);
  212. buzzer.playFrequency(cH, 90, 15);
  213. buzzer.playFrequency(gS, 300, 15);
  214. buzzer.playFrequency(f, 210, 15);
  215. buzzer.playFrequency(cH, 90, 15);
  216. buzzer.playFrequency(a, 390, 15);
  217.  
  218. delay(320);
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement