PROGMEM 中的 Arduino 结构数组
Arduino array of structs in PROGMEM
我正在使用独立的 ATmega328P with two piezo elements 来生成一些音乐。
我已经用音符的频率定义了一些常量。
然后我定义了一个结构,其中包含第一个和第二个压电的音符以及音符的长度。
然后我制作了更多这些结构的数组来描述每首歌曲。
问题是这样我运行很快内存不足。我试图将结构数组存储在 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
为什么会这样?或者有没有更好、更有效的方法来解决这个问题?
在这一行中,您检查了数据,但是您还没有'pgm_read_word()'实际从闪存中获取数据:
if(walkyrie[i].n1 > 0)
如果您不小心得到了一个非零值,那么您就正确地从闪存中读取了这些值,否则,您将跳过该行。
进一步的证据:
Row 20: Frq1: 0 Frq2: 0 Units: 4
此处,n1 为零,但该测试应跳过该行。
此外,'rest' 的逻辑有点不对劲。现在,您不会在剩余的持续时间内读取 units,因此它使用以前的值(来自演奏的音符)。
我想我会先获取所有三个值,然后再检查它们。
我还将频率编码为一个字节,并使用查找 table 将 "key number" 转换为频率(如 MIDI 键号)。这样你的结构数组就会小一些。也许还可以打开 __packed__(随便什么)属性,以避免条目之间的填充——如果保存 flash space 很重要(那么你可以在那里获得更多歌曲!)
听起来很有趣!祝你好运!
我正在使用独立的 ATmega328P with two piezo elements 来生成一些音乐。
我已经用音符的频率定义了一些常量。 然后我定义了一个结构,其中包含第一个和第二个压电的音符以及音符的长度。 然后我制作了更多这些结构的数组来描述每首歌曲。
问题是这样我运行很快内存不足。我试图将结构数组存储在 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
为什么会这样?或者有没有更好、更有效的方法来解决这个问题?
在这一行中,您检查了数据,但是您还没有'pgm_read_word()'实际从闪存中获取数据:
if(walkyrie[i].n1 > 0)
如果您不小心得到了一个非零值,那么您就正确地从闪存中读取了这些值,否则,您将跳过该行。
进一步的证据:
Row 20: Frq1: 0 Frq2: 0 Units: 4
此处,n1 为零,但该测试应跳过该行。
此外,'rest' 的逻辑有点不对劲。现在,您不会在剩余的持续时间内读取 units,因此它使用以前的值(来自演奏的音符)。
我想我会先获取所有三个值,然后再检查它们。
我还将频率编码为一个字节,并使用查找 table 将 "key number" 转换为频率(如 MIDI 键号)。这样你的结构数组就会小一些。也许还可以打开 __packed__(随便什么)属性,以避免条目之间的填充——如果保存 flash space 很重要(那么你可以在那里获得更多歌曲!)
听起来很有趣!祝你好运!