我正在使用带有两个压电元件的独立ATmega328P来生成一些音乐。
我已经用音符的频率定义了一些常数。然后我定义了一个结构,其中包含第一个和第二个压电的注释以及注释的长度。然后我制作了更多这些结构的数组来描述每首歌曲。
问题是这样我很快就会耗尽内存。我试图将结构数组存储在 PROGMEM 中,以避免这个问题。我尝试使用一个名为 PROGMEM_readAnything 的小型库、memcpy_P() 或 pgm_read_word() 和 pgm_read_byte() 函数,但在所有情况下我都会遇到同样的问题。
当我遍历 NOTES 数组时,它会跳过一些元素,而正确读取和播放其他元素。它总是跳过相同的元素,而不仅仅是随机元素。
我什至尝试更换微控制器,认为芯片的某些部分可能已被某些东西损坏,但上传相同的草图我得到了相同的结果,因此微控制器可能完好无损。
这是代码:
#include <Tone.h>
#include <avr/pgmspace.h>
// Define the notes frequency
#define G2 98
#define Gs2 104
#define Ab2 104
#define A2 110
#define As2 116
//... and so on with many other music notes ...
#define Fs7 2960
#define Gb7 2960
#define G7 3136
//Rest
#define R 0
typedef struct {
int n1;
int n2;
byte units;
} NOTES;
Tone buzzer1;
Tone buzzer2;
int myTempo = 100;
// Walkyrie
const NOTES walkyrie[] PROGMEM = {
{Fs3, Fs4, 2},
{B3, B4, 3},
{Fs3, Fs4, 1},
{B3, B4, 2},
{D4, D5, 6},
{B3, B4, 6},
{D4, D5, 3},
{B3, B4, 1},
{D4, D5, 2},
{Fs4, Fs5, 6},
{D4, D5, 6},
{Fs4, Fs5, 3},
{D4, D5, 1},
{Fs4, Fs5, 2},
{A4, A5, 6},
{A3, A4, 6},
{D4, D5, 3},
{A3, A4, 1},
{D4, D5, 2},
{Fs4, Fs5, 6},
{R, 0, 4},
{A3, A4, 2},
{D4, D5, 3},
{A3, A4, 1},
{D4, D5, 2},
{Fs4, Fs5, 6},
{D4, D5, 6},
{Fs4, Fs5, 3},
{D4, D5, 1},
{Fs4, Fs5, 2},
{A4, A5, 6},
{Fs4, Fs5, 6},
{A4, A5, 3},
{Fs4, Fs5, 1},
{A4, A5, 2},
{Cs5, Cs6, 6},
{Cs4, Cs5, 6},
{Fs4, Fs5, 3},
{Cs4, Cs5, 1},
{Fs4, Fs5, 2},
{As4, As5, 6}
};
void playSong()
{
// We store the frequency of the second piezo in this variable
int secondFreq = 0;
Serial.println(sizeof(walkyrie)/sizeof(walkyrie[0]));
// Walk through the array of music
for(int i = 0; i < sizeof(walkyrie)/sizeof(walkyrie[0]); i++)
{
int n1;
int n2;
byte units;
// Only play if it is not a rest
if (walkyrie[i].n1 > 0)
{
n1 = pgm_read_word(&(walkyrie[i].n1));
n2 = pgm_read_word(&(walkyrie[i].n2));
units = pgm_read_byte(&(walkyrie[i].units));
Serial.print("Row ");
Serial.print(i);
Serial.print(": Frequency 1: ");
Serial.print(n1);
Serial.print(" Frequency 2: ");
Serial.print(n2);
Serial.print(" Units: ");
Serial.println(units);
// Play the note of the first piezo
buzzer1.play(n1, (units*myTempo));
// If the frequency of the second piezo is 0, we play the same note
// as the first, else the note set for the second one
if (n2 == 0)
{
secondFreq = n1;
}
else {
secondFreq = n2;
}
buzzer2.play(secondFreq, (units*myTempo));
}
// Then we wait for the note to end plus a little, between two notes
delay((units*myTempo) + 10);
}
}
void setup() {
Serial.begin(9600);
buzzer1.begin(11);
buzzer2.begin(12);
}
void loop()
{
playSong();
}
我添加了一些行以在串行监视器中查看会发生什么。它读取正确的长度...
串行监视器的输出如下:
41 (correct length)
Row 1: Freq1: 247 Freq2: 499 Units: 3 (row 0 - the first note is already missing)
Row 2: Freq1: 185 Freq2: 370 Units: 1
Row 3: Freq1: 247 Freq2: 499 Units: 2 (row 4 missing)
Row 5: Freq1: 247 Freq2: 499 Units: 6 (row 6-7 missing)
Row 8: Freq1: 294 Freq2: 587 Units: 2
Row 9: Freq1: 370 Freq2: 740 Units: 6
Row 10: Freq1: 294 Freq2: 587 Units: 6
Row 11: Freq1: 370 Freq2: 740 Units: 3
Row 12: Freq1: 294 Freq2: 587 Units: 1
Row 13: Freq1: 370 Freq2: 740 Units: 2
Row 14: Freq1: 440 Freq2: 880 Units: 6
Row 15: Freq1: 220 Freq2: 440 Units: 6 (row 16-17 missing)
Row 18: Freq1: 294 Freq2: 587 Units: 2
Row 19: Freq1: 370 Freq2: 740 Units: 6
Row 20: Freq1: 0 Freq2: 0 Units: 4
Row 21: Freq1: 220 Freq2: 440 Units: 2
Row 22: Freq1: 294 Freq2: 587 Units: 3
Row 23: Freq1: 220 Freq2: 440 Units: 1
Row 24: Freq1: 294 Freq2: 587 Units: 2
Row 25: Freq1: 370 Freq2: 740 Units: 6
Row 26: Freq1: 294 Freq2: 587 Units: 6
Row 27: Freq1: 370 Freq2: 740 Units: 3
Row 28: Freq1: 294 Freq2: 587 Units: 1
Row 29: Freq1: 370 Freq2: 740 Units: 2
Row 30: Freq1: 440 Freq2: 880 Units: 6
Row 31: Freq1: 370 Freq2: 740 Units: 6
Row 32: Freq1: 440 Freq2: 880 Units: 3
Row 33: Freq1: 370 Freq2: 740 Units: 1
Row 34: Freq1: 440 Freq2: 880 Units: 2
Row 35: Freq1: 554 Freq2: 1109 Units: 6
Row 36: Freq1: 277 Freq2: 554 Units: 6
Row 37: Freq1: 370 Freq2: 740 Units: 3
Row 38: Freq1: 277 Freq2: 554 Units: 1
Row 39: Freq1: 370 Freq2: 740 Units: 2
Row 40: Freq1: 466 Freq2: 932 Units: 6
为什么会这样?或者有没有更好、更有效的方法来解决这个问题?