首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PROGMEM中的Arduino结构阵列

PROGMEM中的Arduino结构阵列
EN

Stack Overflow用户
提问于 2016-08-11 20:00:06
回答 1查看 1K关注 0票数 0

我使用一个独立的ATmega328P和两个压电元件来生成一些音乐。

我已经定义了一些常量与音乐音符的频率。然后,我定义了一个结构,它包含第一个和第二个音符的音符以及音符的长度。然后,我对这些结构做了更多的数组来描述每首歌。

问题是这样我很快就会耗尽记忆。为了避免这个问题,我尝试在PROGMEM中存储结构数组。我尝试使用一个名为PROGMEM_readAnything、memcpy_P()或pgm_read_word()和pgm_read_byte()函数的小型库,但在所有情况下我都遇到了同样的问题。

当我循环遍历注释数组时,它跳过一些元素,同时正确地读取和播放其他元素。它总是跳过相同的元素,而不仅仅是随机的。

我甚至试图改变微控制器,认为芯片的某些部分可能被什么东西损坏了,但上传相同的草图我得到了同样的结果,所以微控制器可能是完整的。

以下是代码:

代码语言:javascript
复制
#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();
}

我在串行监视器中添加了一些行以查看发生了什么。它读的是正确的长度。

串行监视器的输出如下:

代码语言:javascript
复制
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

为什么会发生这种事?还是有更好、更有效的方法来解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-11 21:03:46

在这一行中,您检查数据,但还没有执行“pgm_read_word()”操作来实际从闪存中获取数据:

代码语言:javascript
复制
if(walkyrie[i].n1 > 0)

如果意外地得到一个非零值,则正确地读取闪存中的值,否则跳过该行。

进一步证据:

代码语言:javascript
复制
Row 20: Frq1: 0 Frq2: 0 Units: 4

在这里,n1为0,但是该测试应该跳过行。

此外,“rest”的逻辑也有点不正确。现在,您不需要在剩下的时间内读取单元,所以它使用的是前面的值(从播放的音符中)。

我想我会先得到这三个值,然后再检查它们。

我还将频率编码成一个字节,并使用一个查找表将“键号”转换为频率(就像MIDI键号)。这样,您的结构数组就会更小一些。也可以打开__packed__ (不管什么)属性,以避免条目之间的填充--如果节省闪存空间很重要(那么您可以在那里获得更多的歌曲!)

听起来很有趣!祝好运!

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38905105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档