1
5
6#include <MusicWithoutDelay.h>
7#include <Button.h>
8#include <Tone.h>
9char *Soprano = "Zelda:o=5,b=160,f=aeb:4b,4f+8.f,16b,16b,16c1,16d1,16e1,2f1,8p,8f1,12f1,12g_1,12a1,2b1,12p,12b1,12b1,12b1,12a1,12g_1,8.a1,16g_1,2f1,4f1,8e1,16e1,16f1,2g_1,8f1,8e1,8d_1,16d_1,16e1,2f1,8e1,8d_1,8c1,16c1,16d1,2e#1,4g1,8f1,16f,16f,8f,16f,16f,8f,16f,16f,8f,8f";
10char *Bass = ":b=160,f=aeb:4d,12d,12d,12c,8.d,16d,16d,16e,16f,16g,8.a,16b,16b,16c1,16d1,16e1,4f1,12a,12b,12c1,8.d_1,16g_,16g_,16a,16b,16c1,12d_1,12p,12d_1,12d_1,12c1,12b,8.d_,16a,12a,12a,12g_,8.a,16a,12a,12g_,12a,8g_,16g_,16f,8g_,16g_,16a,4b,8a,8g_,8f,16f,16e,8f,16f,16g_,4a,8g_,8f,4e#,8e#,16e#,16f,8g,16g,16a#,8b,8c1,8a#,16a#-1,16a#-1,8a#-1,16a#-1,16a#-1,8a#-1,16a#-1,16a#-1,8a#-1,8a#-1";
11MusicWithoutDelay instrument(Soprano);
12MusicWithoutDelay instrument2(Bass);
13Tone myTone;
14Tone myTone2;
15#define BUTTON_PIN 2
16#define BUTTON_PIN2 3
17#define BUTTON_PIN3 4
18
19#define PULLUP true
20#define INVERT true
21#define DEBOUNCE_MS 70
22Button pauseButton(BUTTON_PIN, PULLUP, INVERT, DEBOUNCE_MS);
23Button randomSkipButton(BUTTON_PIN2, PULLUP, INVERT, DEBOUNCE_MS);
24Button reverseButton(BUTTON_PIN3, PULLUP, INVERT, DEBOUNCE_MS);
25
26const int potPin = A1;
27const int vibratingMotor = A0;
28unsigned long motorMillis = 0;
29
30int rgbLeds[] = {13, 12, 11};
31int ledCursor = 0;
32bool pState;
33bool state;
34bool stopped = true;
35void setup() {
36
37 myTone.begin(7);
38 myTone2.begin(6);
39 for (int i = 0; i < 3; i++) {
40 pinMode(rgbLeds[i], OUTPUT);
41 }
42 pinMode(vibratingMotor, OUTPUT);
43}
44
45void loop() {
46
47 unsigned long cMillis = millis();
48 instrument.play(cMillis, myTone);
49 instrument2.play(cMillis, myTone2);
50 pauseButton.read();
51 randomSkipButton.read();
52 reverseButton.read();
53 boundsCheck(instrument);
54 boundsCheck(instrument2);
55 if (instrument.isPaused() && instrument2.isPaused() && stopped) {
56 Pause();
57 }
58 if (pauseButton.wasPressed()) {
59 Pause();
60 stopped = false;
61 }
62 if (reverseButton.wasPressed()) {
63 instrument.reverse();
64 instrument2.reverse();
65 }
66 if (randomSkipButton.wasPressed()) {
67 int val = analogRead(potPin);
68 val = map(val, 0, 1023, 0, instrument.getTotalTime());
69 instrument.skipTo(val);
70 val = analogRead(potPin);
71 val = map(val, 0, 1023, 0, instrument2.getTotalTime());
72 instrument2.skipTo(val);
73 }
74 if (instrument2.isNote()) {
75 digitalWrite(vibratingMotor, HIGH);
76 motorMillis = cMillis;
77 }
78 if (cMillis - motorMillis >= 40) {
79 digitalWrite(vibratingMotor, LOW);
80 }
81 state = instrument.isNote();
82 if (state != pState) {
83 pState = state;
84 if (pState) {
85 digitalWrite(rgbLeds[ledCursor], LOW);
86 }
87 else {
88 digitalWrite(rgbLeds[ledCursor], LOW);
89 ledCursor++;
90 if (ledCursor == 3) {
91 ledCursor = 0;
92 }
93 digitalWrite(rgbLeds[ledCursor], HIGH);
94 }
95 }
96 if (instrument.isRest()) {
97 digitalWrite(rgbLeds[ledCursor], LOW);
98 }
99}
100void boundsCheck(MusicWithoutDelay &m) {
101 if (m.isEnd()) {
102 m.pause();
103 stopped = true;
104 }
105}
106void Pause() {
107 instrument.pause();
108 instrument2.pause();
109}
110