Hi,
this is my first post, so apologies if I make some mistakes posting
I have tried googling around, but maybe I found the answer and I didn't understand it, so I am asking you guys!
I have a fairly simple project:
three buttons, one piezo
when pressing one button, the piezo plays some notes
It works when doing in a "procedural" way, but I would like to be able to extend it using OOP, so I created two classes.
The first one is a Player class
It contains the algorithm to play the notes
The second one is a Songs class
Songs
Songs.h
class Songs{
public:
Songs(char title[]);
int melody[];
int tempo[];
};
Songs.cpp
Songs::Songs(char title[]){
if (title == "sm"){
//this bit here does not work...
//compiler says: error: assigning to an array from an initializer list
melody = {
NOTE_E7, NOTE_E7, 0, NOTE_E7, 0, NOTE_C7, NOTE_E7, 0, NOTE_G7, 0, 0, 0, NOTE_G6, 0, 0, 0, NOTE_C7, 0, 0, NOTE_G6,
0, 0, NOTE_E6, 0, 0, NOTE_A6, 0, NOTE_B6, 0, NOTE_AS6, NOTE_A6, 0, NOTE_G6, NOTE_E7, NOTE_G7, NOTE_A7, 0, NOTE_F7, NOTE_G7,
0, NOTE_E7, 0, NOTE_C7, NOTE_D7, NOTE_B6, 0, 0, NOTE_C7, 0, 0, NOTE_G6, 0, 0, NOTE_E6, 0, 0, NOTE_A6, 0, NOTE_B6, 0, NOTE_AS6, NOTE_A6, 0,
NOTE_G6, NOTE_E7, NOTE_G7, NOTE_A7, 0, NOTE_F7, NOTE_G7, 0, NOTE_E7, 0, NOTE_C7, NOTE_D7, NOTE_B6, 0, 0,
NOTE_E7, NOTE_E7, 0, NOTE_E7, 0, NOTE_C7, NOTE_E7, 0, NOTE_G7, 0, 0, 0, NOTE_G6, 0, 0, 0,
};
//this bit here does not work, same as above
tempo = {
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12
};
}
}
The notes are defined in the Songs class, each one has a value
In my sketch, I want to do the following:
//initialisation is performed outside setup and loop
Song supermario("sm");
Player player;
When pressing a button, it should work like this:
//this goes in the loop
player.play(supermario.melody, supermario.tempo);
It seems like my problem is the array initialisation in the constructor given a certain parameter to the constructor itself.
Can someone point me in the right direction to achieve this?
I would appreciate to know as well if this is the right way of doing it, or there is a better approach.
Thanks in advance!