This project uses my MusicWithoutDelay library to play multiple sounds at the same time.
Devices and components
Arduino Uno Rev3
Speaker: 0.25 W, 8 ohms
vibrating motor
Resistance 221 ohms
12mm push button switch
Jumper wires (generic)
Breadboard (generic)
RGB diffused common cathode
Software and tools
Arduino IDE
Project description
Video
Download library
Why?
I created the library because there was no way to play 8-bit classical music while your main sketch was running. Additionally, playing other people's music files was difficult and disorganized.
I want to play more than one note at the same time. After much research, I acquired this ability using Bhagman's Tone library. Unfortunately, the number of notes that can be played at the same time depends on the number of timers your array has (cannot use Timer0 since millis() uses it). The Arduino Uno can therefore play up to 2 notes at the same time.
How does it work?
To learn how to read and create your own music, learn RTTL in my library's $ReadMe file
RTTL is an extremely easy to understand language for musicians and beginners
Benefits
Play more than one note at the same time
Run other things like serial monitor in the background
Play music backwards and forwards
Pause/play music
Skip to specific sections of the song
Choose a new song
more to come :D
Legend of Zelda
arduino
A demo of my MusicWithoutDelay library. It plays the Legend of Zelda and allows the user to pause/play, skip and reverse the song at any time. Better yet, since it doesn't use delay(), you can easily add more code to it and make it do other cool things, like read the serial monitor or display on an Oled.
1/*Example for the MusicWithoutDelay Library by Nathan Ramanathan. nathan6ramanathan@gmail.com
2 This sketch puts all the functions to the test.
3
4*/
5//To learn more about this project go to https://github.com/nathanRamaNoodles/MusicWithoutDelay-LIbrary
6#include <MusicWithoutDelay.h>
7#include <Button.h> //https://github.com/JChristensen/Button acquired by JChristensen's popular button library
8#include <Tone.h> //https://github.com/bhagman/Tone
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); //o=5 means that the instrument will play at the 5th Octave. o=4 is default
12MusicWithoutDelay instrument2(Bass); //f=aeb, means all a's, e's, and b's are flats
13Tone myTone; //b=160, means the bpm(tempo) is 160, default is 100
14Tone myTone2;
15#define BUTTON_PIN 2 //Connect a tactile button switch (or something similar) from Arduino pin 2 to ground.
16#define BUTTON_PIN2 3 //Connect a tactile button switch (or something similar) from Arduino pin 3 to ground.
17#define BUTTON_PIN3 4 //Connect a tactile button switch (or something similar) from Arduino pin 3 to ground.
18
19#define PULLUP true //To keep things simple, we use the Arduino's internal pullup resistor.
20#define INVERT true //Since the pullup resistor will keep the pin high
21#define DEBOUNCE_MS 70 //A debounce time of 70 milliseconds usually works well for noisy button switches. if not, try 20.
22Button pauseButton(BUTTON_PIN, PULLUP, INVERT, DEBOUNCE_MS); //Declare the button
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 // put your setup code here, to run once:
37 myTone.begin(7); //attach both pins to same speaker with one 1k resistor to pin 11,
38 myTone2.begin(6); //and another 1k resistor to pin 10.
39 for (int i = 0; i < 3; i++) {
40 pinMode(rgbLeds[i], OUTPUT);
41 }
42 pinMode(vibratingMotor, OUTPUT);
43}
44
45void loop() {
46 // put your main code here, to run repeatedly:
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(); // stop or play song
60 stopped = false;
61 }
62 if (reverseButton.wasPressed()) {
63 instrument.reverse(); //reverse direction of song
64 instrument2.reverse();
65 }
66 if (randomSkipButton.wasPressed()) { //skip to location in song depending on value of potentiometer
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) { //This function fixes songs that may have multiple instruments containing different TotalTimes
101 if (m.isEnd()) {
102 m.pause();
103 stopped = true;
104 }
105}
106void Pause() {
107 instrument.pause();
108 instrument2.pause();
109}
110
Downloadable files
Schemes
Follow the diagram.
Schemes
Schemes
Follow the diagram.
Schemes
Note: Content and images are from: https://projecthub.arduino.cc/, with some modifications.
If you want it removed due to copyright reasons, please leave a comment. Thank you.
I want to share this article more widely so that everyone knows about Arduino and your project.