macca-nz

Otto_1

May 7th, 2023 (edited)
960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //OTTODIY - REMIX - SDR_PRINT3D
  2.  
  3. #include "Otto.h"
  4. Otto Otto; //This is Otto!
  5. int song = 0;
  6.  
  7. #define LeftLeg 2
  8. #define RightLeg 3
  9. #define LeftFoot 4
  10. #define RightFoot 5
  11. #define Buzzer  13
  12. #define Trigger 8 // ultrasonic sensor trigger pin
  13. #define Echo 9 // ultrasonic sensor echo pin
  14.  
  15. int SpeakerPin = 13;
  16.  
  17. long doPingNow() {
  18.    digitalWrite(Trigger,LOW);
  19.    delayMicroseconds(2);
  20.    digitalWrite(Trigger, HIGH);
  21.    delayMicroseconds(10);
  22.    digitalWrite(Trigger, LOW);
  23.    long duration = pulseIn(Echo, HIGH);
  24.    return duration;
  25. }
  26.  
  27. #define NOTE_B0  31
  28. #define NOTE_C1  33
  29. #define NOTE_CS1 35
  30. #define NOTE_D1  37
  31. #define NOTE_DS1 39
  32. #define NOTE_E1  41
  33. #define NOTE_F1  44
  34. #define NOTE_FS1 46
  35. #define NOTE_G1  49
  36. #define NOTE_GS1 52
  37. #define NOTE_A1  55
  38. #define NOTE_AS1 58
  39. #define NOTE_B1  62
  40. #define NOTE_C2  65
  41. #define NOTE_CS2 69
  42. #define NOTE_D2  73
  43. #define NOTE_DS2 78
  44. #define NOTE_E2  82
  45. #define NOTE_F2  87
  46. #define NOTE_FS2 93
  47. #define NOTE_G2  98
  48. #define NOTE_GS2 104
  49. #define NOTE_A2  110
  50. #define NOTE_AS2 117
  51. #define NOTE_B2  123
  52. #define NOTE_C3  131
  53. #define NOTE_CS3 139
  54. #define NOTE_D3  147
  55. #define NOTE_DS3 156
  56. #define NOTE_E3  165
  57. #define NOTE_F3  175
  58. #define NOTE_FS3 185
  59. #define NOTE_G3  196
  60. #define NOTE_GS3 208
  61. #define NOTE_A3  220
  62. #define NOTE_AS3 233
  63. #define NOTE_B3  247
  64. #define NOTE_C4  262
  65. #define NOTE_CS4 277
  66. #define NOTE_D4  294
  67. #define NOTE_DS4 311
  68. #define NOTE_E4  330
  69. #define NOTE_F4  349
  70. #define NOTE_FS4 370
  71. #define NOTE_G4  392
  72. #define NOTE_GS4 415
  73. #define NOTE_A4  440
  74. #define NOTE_AS4 466
  75. #define NOTE_B4  494
  76. #define NOTE_C5  523
  77. #define NOTE_CS5 554
  78. #define NOTE_D5  587
  79. #define NOTE_DS5 622
  80. #define NOTE_E5  659
  81. #define NOTE_F5  698
  82. #define NOTE_FS5 740
  83. #define NOTE_G5  784
  84. #define NOTE_GS5 831
  85. #define NOTE_A5  880
  86. #define NOTE_AS5 932
  87. #define NOTE_B5  988
  88. #define NOTE_C6  1047
  89. #define NOTE_CS6 1109
  90. #define NOTE_D6  1175
  91. #define NOTE_DS6 1245
  92. #define NOTE_E6  1319
  93. #define NOTE_F6  1397
  94. #define NOTE_FS6 1480
  95. #define NOTE_G6  1568
  96. #define NOTE_GS6 1661
  97. #define NOTE_A6  1760
  98. #define NOTE_AS6 1865
  99. #define NOTE_B6  1976
  100. #define NOTE_C7  2093
  101. #define NOTE_CS7 2217
  102. #define NOTE_D7  2349
  103. #define NOTE_DS7 2489
  104. #define NOTE_E7  2637
  105. #define NOTE_F7  2794
  106. #define NOTE_FS7 2960
  107. #define NOTE_G7  3136
  108. #define NOTE_GS7 3322
  109. #define NOTE_A7  3520
  110. #define NOTE_AS7 3729
  111. #define NOTE_B7  3951
  112. #define NOTE_C8  4186
  113. #define NOTE_CS8 4435
  114. #define NOTE_D8  4699
  115. #define NOTE_DS8 4978
  116.  
  117. #define melodyPin 13
  118. //Mario main theme melody
  119. int melody[] = {
  120.   NOTE_E7, NOTE_E7, 0, NOTE_E7,
  121.   0, NOTE_C7, NOTE_E7, 0,
  122.   NOTE_G7, 0, 0,  0,
  123.   NOTE_G6, 0, 0, 0,
  124.  
  125.   NOTE_C7, 0, 0, NOTE_G6,
  126.   0, 0, NOTE_E6, 0,
  127.   0, NOTE_A6, 0, NOTE_B6,
  128.   0, NOTE_AS6, NOTE_A6, 0,
  129.  
  130.   NOTE_G6, NOTE_E7, NOTE_G7,
  131.   NOTE_A7, 0, NOTE_F7, NOTE_G7,
  132.   0, NOTE_E7, 0, NOTE_C7,
  133.   NOTE_D7, NOTE_B6, 0, 0,
  134.  
  135.   NOTE_C7, 0, 0, NOTE_G6,
  136.   0, 0, NOTE_E6, 0,
  137.   0, NOTE_A6, 0, NOTE_B6,
  138.   0, NOTE_AS6, NOTE_A6, 0,
  139.  
  140.   NOTE_G6, NOTE_E7, NOTE_G7,
  141.   NOTE_A7, 0, NOTE_F7, NOTE_G7,
  142.   0, NOTE_E7, 0, NOTE_C7,
  143.   NOTE_D7, NOTE_B6, 0, 0
  144. };
  145. //Mario main them tempo
  146. int tempo[] = {
  147.   12, 12, 12, 12,
  148.   12, 12, 12, 12,
  149.   12, 12, 12, 12,
  150.   12, 12, 12, 12,
  151.  
  152.   12, 12, 12, 12,
  153.   12, 12, 12, 12,
  154.   12, 12, 12, 12,
  155.   12, 12, 12, 12,
  156.  
  157.   9, 9, 9,
  158.   12, 12, 12, 12,
  159.   12, 12, 12, 12,
  160.   12, 12, 12, 12,
  161.  
  162.   12, 12, 12, 12,
  163.   12, 12, 12, 12,
  164.   12, 12, 12, 12,
  165.   12, 12, 12, 12,
  166.  
  167.   9, 9, 9,
  168.   12, 12, 12, 12,
  169.   12, 12, 12, 12,
  170.   12, 12, 12, 12,
  171. };
  172. //Underworld melody
  173. int underworld_melody[] = {
  174.   NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
  175.   NOTE_AS3, NOTE_AS4, 0,
  176.   0,
  177.   NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
  178.   NOTE_AS3, NOTE_AS4, 0,
  179.   0,
  180.   NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
  181.   NOTE_DS3, NOTE_DS4, 0,
  182.   0,
  183.   NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
  184.   NOTE_DS3, NOTE_DS4, 0,
  185.   0, NOTE_DS4, NOTE_CS4, NOTE_D4,
  186.   NOTE_CS4, NOTE_DS4,
  187.   NOTE_DS4, NOTE_GS3,
  188.   NOTE_G3, NOTE_CS4,
  189.   NOTE_C4, NOTE_FS4, NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4,
  190.   NOTE_GS4, NOTE_DS4, NOTE_B3,
  191.   NOTE_AS3, NOTE_A3, NOTE_GS3,
  192.   0, 0, 0
  193. };
  194. //Underwolrd tempo
  195. int underworld_tempo[] = {
  196.   12, 12, 12, 12,
  197.   12, 12, 6,
  198.   3,
  199.   12, 12, 12, 12,
  200.   12, 12, 6,
  201.   3,
  202.   12, 12, 12, 12,
  203.   12, 12, 6,
  204.   3,
  205.   12, 12, 12, 12,
  206.   12, 12, 6,
  207.   6, 18, 18, 18,
  208.   6, 6,
  209.   6, 6,
  210.   6, 6,
  211.   18, 18, 18, 18, 18, 18,
  212.   10, 10, 10,
  213.   10, 10, 10,
  214.   3, 3, 3
  215. };
  216.  
  217. void setup() {
  218.   Otto.init(LeftLeg, RightLeg, LeftFoot, RightFoot, true, Buzzer); //Set the servo pins and Buzzer pin
  219.   pinMode(Trigger, OUTPUT);
  220.   pinMode(Echo, INPUT);
  221.   pinMode(13, OUTPUT);
  222. }
  223.  
  224. void mySong(){
  225.     while (melody){
  226.         //sing the tunes
  227.         sing(1);
  228.         sing(1);
  229.         sing(2);
  230.     }
  231.   return;
  232. }
  233.  
  234. void loop() {
  235.   float cmDist = 0.01723 * doPingNow();      //returns distance in CM
  236.   float inchDist = ((float) cmDist / 2.54);   //convert to inch's
  237.       while (inchDist < 15.0){
  238.               Otto.sing(S_surprise);
  239.               Otto.playGesture(OttoConfused);
  240.               Otto.walk(2,1000,-1); // BACKWARD x2
  241.               Otto.turn(3,1000,1); // LEFT x3
  242.               Otto.walk(1,1000,1); // FORWARD x1
  243.               mySong();
  244.         }
  245. }
  246.  
  247. void sing(int s) {
  248.   // iterate over the notes of the melody:
  249.   song = s;
  250.   if (song == 2) {
  251.     Serial.println(" 'Underworld Theme'");
  252.     int size = sizeof(underworld_melody) / sizeof(int);
  253.     for (int thisNote = 0; thisNote < size; thisNote++) {
  254.  
  255.       // to calculate the note duration, take one second
  256.       // divided by the note type.
  257.       //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  258.       int noteDuration = 1000 / underworld_tempo[thisNote];
  259.  
  260.       buzz(melodyPin, underworld_melody[thisNote], noteDuration);
  261.  
  262.       // to distinguish the notes, set a minimum time between them.
  263.       // the note's duration + 30% seems to work well:
  264.       int pauseBetweenNotes = noteDuration * 1.30;
  265.       delay(pauseBetweenNotes);
  266.  
  267.       // stop the tone playing:
  268.       buzz(melodyPin, 0, noteDuration);
  269.  
  270.     }
  271.  
  272.   } else {
  273.  
  274.     Serial.println(" 'Mario Theme'");
  275.     int size = sizeof(melody) / sizeof(int);
  276.     for (int thisNote = 0; thisNote < size; thisNote++) {
  277.  
  278.       // to calculate the note duration, take one second
  279.       // divided by the note type.
  280.       //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  281.       int noteDuration = 1000 / tempo[thisNote];
  282.  
  283.       buzz(melodyPin, melody[thisNote], noteDuration);
  284.  
  285.       // to distinguish the notes, set a minimum time between them.
  286.       // the note's duration + 30% seems to work well:
  287.       int pauseBetweenNotes = noteDuration * 1.30;
  288.       delay(pauseBetweenNotes);
  289.  
  290.       // stop the tone playing:
  291.       buzz(melodyPin, 0, noteDuration);
  292.  
  293.     }
  294.   }
  295. }
  296.  
  297. void buzz(int targetPin, long frequency, long length) {
  298.   digitalWrite(13, HIGH);
  299.   long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
  300.   //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  301.   //// there are two phases to each cycle
  302.   long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
  303.   //// multiply frequency, which is really cycles per second, by the number of seconds to
  304.   //// get the total number of cycles to produce
  305.   for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
  306.     digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
  307.     delayMicroseconds(delayValue); // wait for the calculated delay value
  308.     digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
  309.     delayMicroseconds(delayValue); // wait again or the calculated delay value
  310.   }
  311.   digitalWrite(13, LOW);
  312.  
  313. }
Advertisement
Add Comment
Please, Sign In to add comment