我正在尝试创建Arduino蜂鸣器控制器来播放音乐,但是当我在音符持续时间内定义H(一半)时,它给了我错误
sketch_jan30g:24:11:错误:数值常量前应有'}'
#define H 2 * 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:错误:预期为','或';'在数字之前 恒定
#define H 2 * 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),是误差仍然存在。
答案 0 :(得分:0)
自从我进行宏扩展以来已经有一段时间了,但是请尝试在H的定义和Q的定义上切换位置。由于H依赖于Q,因此可能无法正确扩展H。
答案 1 :(得分: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
};
您在前两行末尾缺少逗号。