You could try the following (using memcpy):
#include "Arduino.h"
#define MAXSONGCOUNT 10
class Song {
public:
Song(int* notesArray, int sizeOfNotesArray, int* rythmArray, int sizeOfRythmArray, int pin);
void play();
private:
int _notesArray[MAXSONGCOUNT];
int _rythmArray[MAXSONGCOUNT];
int _pin;
int _songCount;
int _totalSongs;
};
//constructor
Song::Song(int* notesArray, int sizeOfNotesArray, int* rythmArray, int sizeOfRythmArray, int pin)
{
//determine how many songs there are
if(sizeOfRythmArray < sizeOfNotesArray){
_totalSongs = sizeOfRythmArray; //use the one with the fewest to avoid out of bounds errors on the other
} else {
_totalSongs = sizeOfNotesArray; //use the one with the fewest to avoid out of bounds errors on the other
}
//Copy the both arrays, ensuring that they don't go out of bounds.
if(_totalSongs > MAXSONGCOUNT){
_totalSongs = MAXSONGCOUNT;
memcpy(_notesArray,notesArray,MAXSONGCOUNT*sizeof(int)); //out of bounds, so only copy the maximum amount
memcpy(_rythmArray,rythmArray,MAXSONGCOUNT*sizeof(int)); //out of bounds, so only copy the maximum amount
} else {
memcpy(_notesArray,notesArray,_totalSongs*sizeof(int)); //copy the required amount from notesArray
memset(_notesArray + _totalSongs, 0, (MAXSONGCOUNT - _totalSongs)*sizeof(int)); //fill the rest with 0's
memcpy(_rythmArray,rythmArray,_totalSongs*sizeof(int)); //copy the required amount from rythmArray
memset(_rythmArray + _totalSongs, 0, (MAXSONGCOUNT - _totalSongs)*sizeof(int)); //fill the rest with 0's
}
_songCount = 0; //on song 0
}
void Song::play() //actually plays one note
{
tone(_pin, _notesArray[_songCount]);
delay(_rythmArray[_songCount]);
noTone(_pin);
_songCount = _songCount++; //move to the next
if (_songCount >= _totalSongs){
_songCount = 0; //reset back to the beginning maybe?
}
delay(50);
}
Test code:
#include "Song.h"
void setup()
{
// This code will only run once, after each powerup or reset of board
int boo[5] = {1,2,3,4,5};
int baa[7] = {1,2,3,4,5,6,7};
byte sizeOfboo = (sizeof(boo)/sizeof(int));
byte sizeOfbaa = (sizeof(baa)/sizeof(int));
Song hello(boo,sizeOfboo,baa,sizeOfbaa,1);
hello.play();
}
void loop()
{
// This code loops consecutively
}
Note that that code accounts for any mismatch in the size of the two arrays. If you know both arrays will always be the same size, then it can be simplified to the following:
#include "Arduino.h"
#define MAXSONGCOUNT 10
class Song {
public:
Song(int* notesArray, int* rythmArray, int sizeOfArray, int pin);
void play();
private:
int _notesArray[MAXSONGCOUNT];
int _rythmArray[MAXSONGCOUNT];
int _pin;
int _songCount;
int _totalSongs;
};
//constructor
Song::Song(int* notesArray, int* rythmArray, int sizeOfArray, int pin)
{
//determine how many songs there are
_totalSongs = sizeOfArray;
//Copy the both arrays, ensuring that they don't go out of bounds.
if(_totalSongs > MAXSONGCOUNT){
_totalSongs = MAXSONGCOUNT;
memcpy(_notesArray,notesArray,MAXSONGCOUNT*sizeof(int)); //out of bounds, so only copy the maximum amount
memcpy(_rythmArray,rythmArray,MAXSONGCOUNT*sizeof(int)); //out of bounds, so only copy the maximum amount
} else {
memcpy(_notesArray,notesArray,_totalSongs*sizeof(int)); //copy the required amount from notesArray
memset(_notesArray + _totalSongs, 0, (MAXSONGCOUNT - _totalSongs)*sizeof(int)); //fill the rest with 0's
memcpy(_rythmArray,rythmArray,_totalSongs*sizeof(int)); //copy the required amount from rythmArray
memset(_rythmArray + _totalSongs, 0, (MAXSONGCOUNT - _totalSongs)*sizeof(int)); //fill the rest with 0's
}
_songCount = 0; //on song 0
}
void Song::play() //actually plays one note
{
tone(_pin, _notesArray[_songCount]);
delay(_rythmArray[_songCount]);
noTone(_pin);
_songCount = _songCount++; //move to the next
if (_songCount >= _totalSongs){
_songCount = 0; //reset back to the beginning maybe?
}
delay(50);
}
Test code:
#include "Song.h"
void setup()
{
// This code will only run once, after each powerup or reset of board
int boo[5] = {1,2,3,4,5};
int baa[5] = {1,2,3,4,5};
Song hello(boo,baa,5,1);
hello.play();
}
void loop()
{
// This code loops consecutively
}