Arduino 蜂鸣器音乐预期在数字常量之前'}'



我正试图创建Arduino蜂鸣器控制器来播放音乐,但当我为音符的持续时间定义H(半)时,它会给我错误

sketch_jan30g:24:11:错误:数字常量之前应为"}">

#定义H2*Q//半个2/4

^

C: \Users\koumakpet\AppData\Local\Temp\arduino_modified_sketch_955882\sketch_jan30g.ino:49:3:注意:在宏‘H’的扩展中

H+Q,Q,Q

^

sketch_jan30g:24:11:错误:应为","或";"数字之前恒定

#定义H2*Q//半个2/4

^

C: \Users\koumakpet\AppData\Local\Temp\arduino_modified_sketch_955882\sketch_jan30g.ino:49:3:注意:在宏‘H’的扩展中

H+Q,Q,Q

^

sketch_jan30g:57:1:错误:在"}"标记之前需要声明

};

^

退出状态1在数值常量之前应为"}">

我不确定,是什么错误,代码:

//*****************************************
#define B3  247
#define C4  262   //Defining note frequency
#define D4  294
#define E4  330
#define F4  349
#define G4  392
#define LA4  440
#define B4  494
#define Bb4 466
#define C5  523
#define D5  587
#define E5  659
#define F5  698
#define G5  784
#define LA5  880
#define B5  988
// DURATION OF THE NOTES 
#define BPM 120    //  you can change this value changing all the others
#define H 2*Q //half 2/4
#define Q 60000/BPM //quarter 1/4 
#define E Q/2   //eighth 1/8
#define S Q/4 // sixteenth 1/16
#define W 4*Q // whole 4/4
//*****************************************
int notes[] = {       //Note of the song, 0 is a rest/pulse
E4, C5, E5,
D5, F5, G5,
E5,
0, Bb4, F5, G5, LA5, F5,
E5, E5, C5, E5,
B4, 0,
E4, C5, E5,
D5, F5, G5,
E5,
0, Bb4, F5, G5, LA5, F5,
E5, E5, C5, E5,
B4, 0,
0
};
//*****************************************
int duration[] = { 
H+Q, Q, Q
H+Q, Q, Q
W,
Q, Q, Q, Q, Q, Q,
H, H
H, Q, Q, Q,
W+H+Q, Q,
3*W
};
void setup() {
for (int i=0;i<203;i++){              //203 is the total number of music notes in the song
int wait = duration[i];
tone(buzzer,notes[i],wait);          //tone(pin,frequency,duration)
delay(wait);}                        //delay is used so it doesn't go to the next loop before tone is finished playing
//You can click reset on Arduino to replay the song
}
void loop() {
}

编辑:注意,我已经尝试替换H和Q的位置(因为H依赖于Q),错误仍然存在。

在此代码中:

int duration[] = { 
H+Q, Q, Q
H+Q, Q, Q
W,
Q, Q, Q, Q, Q, Q,
H, H
H, Q, Q, Q,
W+H+Q, Q,
3*W
};

前两行末尾缺少逗号。

我已经有一段时间没有玩宏展开了,但试着切换你对H的定义和对Q的定义。可能是H没有正确展开,因为它依赖于Q。

最新更新