Comments (4)
Hi @AceoStar , I'm glad that this repository is helpful to you.
I think that the problem it that you are passing a pointer to the function and it can not calculate the length of the array anymore. The pointer just says where the array starts in memory.
If you want to make the code generic, you have to pass the length of the array to the function as well. While you are at it, also pass the tempo as a parameter. Example:
void playMelody(int melody[], int notes , int tempo){
// this calculates the duration of a whole note in ms
int wholenote = (60000 * 2) / tempo;
int divider = 0, noteDuration = 0;
for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {
// calculates the duration of each note
divider = melody[thisNote + 1];
if (divider > 0) {
// regular note, just proceed
noteDuration = (wholenote) / divider;
} else if (divider < 0) {
// dotted notes are represented with negative durations!!
noteDuration = (wholenote) / abs(divider);
noteDuration *= 1.5; // increases the duration in half for dotted notes
}
// we only play the note for 90% of the duration, leaving 10% as a pause
tone(buzzer, melody[thisNote], noteDuration*0.9);
// Wait for the specief duration before playing the next note.
delay(noteDuration);
// stop the waveform generation before the next note.
noTone(buzzer);
}
}
And you call the function as:
playMelody(cantinaMelody, sizeof(cantinaMelody)/sizeof(cantinaMelody[0])/ 2, 140);
To make it more readable, you can do:
#define MELODY_LENGTH(melody) sizeof(melody)/sizeof(melody[0])/ 2
...
playMelody(cantinaMelody, MELODY_LENGTH(cantinaMelody), 140);
I hope it helps!
from arduino-songs.
Thanks for the tips! I went this route and defined the size and tempo as part of the config. It feels a bit weird to pass the size though. :D Should I use & and pass by reference or should I avoid that in arduino? I'm fairly new to arduino. :D
int bloodyTears_melody[] = {
// Bloody Tears, from Castlevania II
// Arranged by Bobby Lee. THe flute part was used
// https://musescore.com/user/263171/scores/883296
NOTE_G5, 16, NOTE_D5, 16, NOTE_D6, 16, NOTE_D5, 16, NOTE_C6, 16, NOTE_D5, 16, NOTE_AS5, 16, NOTE_D5, 16,
NOTE_A5, 16, NOTE_D5, 16, NOTE_AS5, 16, NOTE_D5, 16, NOTE_A5, 16, NOTE_D5, 16, NOTE_G5, 16, NOTE_D5, 16,
NOTE_A5, 16, NOTE_D5, 16, NOTE_AS5, 16, NOTE_D5, 16, NOTE_C6, 16, NOTE_D5, 16, NOTE_AS5, 16, NOTE_D5, 16,
NOTE_A5, 16, NOTE_D5, 16, NOTE_F5, 16, NOTE_D5, 16, NOTE_A5, 16, NOTE_D5, 16, NOTE_G5, 16, NOTE_D5, 16, //20
NOTE_G5, 16, NOTE_D5, 16, NOTE_D6, 16, NOTE_D5, 16, NOTE_C6, 16, NOTE_D5, 16, NOTE_AS5, 16, NOTE_D5, 16,
NOTE_A5, 16, NOTE_D5, 16, NOTE_AS5, 16, NOTE_D5, 16, NOTE_A5, 16, NOTE_D5, 16, NOTE_G5, 16, NOTE_D5, 16,
NOTE_A5, 16, NOTE_D5, 16, NOTE_AS5, 16, NOTE_D5, 16, NOTE_C6, 16, NOTE_D5, 16, NOTE_AS5, 16, NOTE_D5, 16,
NOTE_A5, 16, NOTE_D5, 16, NOTE_F5, 16, NOTE_D5, 16, NOTE_A5, 16, NOTE_D5, 16, NOTE_G5, 16, NOTE_D5, 16, //20
NOTE_G5, 16, NOTE_D5, 16, NOTE_D6, 16, NOTE_D5, 16, NOTE_C6, 16, NOTE_D5, 16, NOTE_AS5, 16, NOTE_D5, 16,
NOTE_A5, 16, NOTE_D5, 16, NOTE_AS5, 16, NOTE_D5, 16, NOTE_A5, 16, NOTE_D5, 16, NOTE_G5, 16, NOTE_D5, 16,
NOTE_A5, 16, NOTE_D5, 16, NOTE_AS5, 16, NOTE_D5, 16, NOTE_C6, 16, NOTE_D5, 16, NOTE_AS5, 16, NOTE_D5, 16,
NOTE_A5, 16, NOTE_D5, 16, NOTE_F5, 16, NOTE_D5, 16, NOTE_A5, 16, NOTE_D5, 16, NOTE_G5, 16, NOTE_D5, 16, //20
NOTE_G5, 16, NOTE_D5, 16, NOTE_D6, 16, NOTE_D5, 16, NOTE_C6, 16, NOTE_D5, 16, NOTE_AS5, 16, NOTE_D5, 16,
NOTE_A5, 16, NOTE_D5, 16, NOTE_AS5, 16, NOTE_D5, 16, NOTE_A5, 16, NOTE_D5, 16, NOTE_G5, 16, NOTE_D5, 16,
NOTE_A5, 16, NOTE_D5, 16, NOTE_AS5, 16, NOTE_D5, 16, NOTE_C6, 16, NOTE_D5, 16, NOTE_AS5, 16, NOTE_D5, 16,
NOTE_A5, 16, NOTE_D5, 16, NOTE_F5, 16, NOTE_D5, 16, NOTE_A5, 16, NOTE_D5, 16, NOTE_G5, 16, NOTE_D5, 16, //20
};
int bloodyTears_size = sizeof(bloodyTears_melody)/sizeof(bloodyTears_melody[0])/ 2;
int bloodyTears_tempo = 70;
void playMelody(int melody[], int notes , int tempo){
// change this to make the song slower or faster
// int tempo = 70;
// this calculates the duration of a whole note in ms
int wholenote = (60000 * 2) / tempo;
int divider = 0, noteDuration = 0;
for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {
// calculates the duration of each note
divider = melody[thisNote + 1];
if (divider > 0) {
// regular note, just proceed
noteDuration = (wholenote) / divider;
} else if (divider < 0) {
// dotted notes are represented with negative durations!!
noteDuration = (wholenote) / abs(divider);
noteDuration *= 1.5; // increases the duration in half for dotted notes
}
// we only play the note for 90% of the duration, leaving 10% as a pause
tone(buzzer, melody[thisNote], noteDuration*0.9);
// Wait for the specief duration before playing the next note.
delay(noteDuration);
// stop the waveform generation before the next note.
noTone(buzzer);
}
}
from arduino-songs.
I guess it feels weird because in other programming languages the array contains information about its length and you can pass it alone, maybe?
Just my opinion, but I avoid passing arguments by reference if the function does not need to change the input parameters.
from arduino-songs.
I am glad you got this sorted out :)
Since there were no more comments, i will close this now.
from arduino-songs.
Related Issues (20)
- can any of these be used on the smart inventor-mega32_v2 HOT 1
- Any possibility to turn speak off during rest notes
- can you do among us drip
- How can I contribute to this repo?
- Please do Jurassic Park theme.
- Tetris is wrong
- add im blue HOT 1
- Created Arduino-Music library based off your examples.
- Add Megalovania(Undertale theme) *Look in comment for code*
- Could you make reach for the stars? (The one from sonic colors)
- podrian hacer una de caballeros del zodiaco la saga de ades?
- add another song :) HOT 3
- Buttons don't work while buzzer is playing HOT 3
- This nice music on ESP32
- The Godfather theme not according to sheet. Wrong note. HOT 1
- Songs are unlicensed
- Generator for songs
- Non-Blocking Code HOT 1
- Can you add Transformers G1 transition sound effect please i need it for a project HOT 1
- could you please code Treachery by Shiro Sagisu?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from arduino-songs.