Hi. I'm programming an Arduino UNO to control a series of 6 trainhorns. The goal is to play different tunes on the trainhorns, by controlling a relay-board that connects to the airvalves of the horns. The different tunes can be selected with a 4x4 matrix keyboard, and some feedback is presented on an LCD.
The songs are stored in arrays, and are multi-tone - each note is played by a up to 6 horns at the same time. A second array stores the length that each note should be played. New as I am to Arduino (and very novice to programming) I soon hit the memory barrier. Then I learned about PROGMEM.
What I'm walking into: I've got a buffer-array SongN[6][40] (and SongL[40] for the corresponding note-lengths). I've got songs in arrays of different sizes but never more than [6][40] (6 rows since I only have 6 horns and thus will never play more horns simultaneously - "6 trainhorns should be enough for anybody" ).
Now when i use memcpy_P(SongN, Song1N, sizeof(Song1N)) I get an incorrect copy of the array, although parts of it are correct (the first byte of each row).
Brief version of code, stripped for the purpose of my question:
#include <avr/pgmspace.h>
int SongN[6][40] = {}; // Array to store the current playing song - the number of rows must equal rowCount
int SongL[40] = {}; // Array to store the current playing song's timing (the amount of time that each note should play)
//----------------------------------------------
//SONGS TO PLAY
//----------------------------------------------
//Song 1 = Test song multi-toned
int const Song1C = 13; // Length of the song will never change
int const Song1N[rowCount][Song1C] PROGMEM = { // Write the song to flash-memory instead of SRAM
{6, 0, 5, 0, 4, 0, 3, 0, 2, 0, 1, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2},
{4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 3},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5},
{1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 6}
};
int const Song1L[] PROGMEM = {250, 250, 250, 250, 250, 250, 250, 250, 750, 250, 750, 250, 1000}; // Write the timing to flash-memory
void setup() {
Serial.begin(9600); // For debugging purposes
}
void loop() {
if (key == '1') { // copy song 1 to the play-arrays
noteCount = Song1C;
memcpy_P(SongN, Song1N, sizeof(Song1N)); // Copy the array to play from flash to SRAM array
memcpy_P(SongL, Song1L, sizeof(Song1L)); // Do the same for the corresponding note-length-array