我的家庭演播室有一个DIY midi脚踏板,两个按钮被编程为发送信号到MIDI输出,然后我的DAW接收并解释为MIDI控制器。
一切都像现在一样工作,但是现在Arduino IDE突然开始在我的代码中发现问题,因此我无法对代码进行任何编辑以添加更多功能。
这是代码(顺便说一下,它在Arduino Uno中完美地工作):
// define pins
const int switchPin1 = 2;
const int switchPin2 = 3;
// midi notes
char note1 = 60; //Middle C
char note2 = 62; //D
// variables
int switchState1 = LOW;
int switchState2 = LOW;
int currentSwitchState1 = LOW;
int currentSwitchState2 = LOW;
void setup() {
// set the states of the I/O pins:
pinMode(switchPin1, INPUT);
pinMode(switchPin2, INPUT);
// set MIDI baud rate :
Serial.begin(31250);
}
void loop() {
currentSwitchState1 = digitalRead(switchPin1);
currentSwitchState2 = digitalRead(switchPin2);
// button 1 push
if (currentSwitchState1 == HIGH && switchState1 == LOW) {
noteOn(0x94, note1, 0x45);
delay(100);
noteOn(0x94, note1, 0x00);
switchState1 = HIGH;
}
// button 1 release
if (currentSwitchState1 == LOW && switchState1 == HIGH) {
switchState1 = LOW;
}
// button 2 push
if (currentSwitchState2 == HIGH && switchState2 == LOW) {
noteOn(0x94, note2, 0x45);
delay(100);
noteOn(0x94, note2, 0x00);
switchState2 = HIGH;
}
// button 2 release
if (currentSwitchState2 == LOW && switchState2 == HIGH) {
switchState2 = LOW;
}
void noteOn(char cmd, char data1, char data2) {
Serial.print(cmd);
Serial.print(data1); Serial.print(data2);
}
}
这是我得到的错误日志:
Arduino: 1.8.15 (Windows Store 1.8.49.0) (Windows 10), Board: "Arduino Uno"
C:\Users\CrazyCringeComplex\Desktop\MIDI Pedal\midipedal\midipedal.ino: In function 'void loop()':
midipedal:46:5: error: 'noteOn' was not declared in this scope
noteOn(0x94, note1, 0x45);
^~~~~~
C:\Users\CrazyCringeComplex\Desktop\MIDI Pedal\midipedal\midipedal.ino:46:5: note: suggested alternative: 'note2'
noteOn(0x94, note1, 0x45);
^~~~~~
note2
midipedal:58:5: error: 'noteOn' was not declared in this scope
noteOn(0x94, note2, 0x45);
^~~~~~
C:\Users\CrazyCringeComplex\Desktop\MIDI Pedal\midipedal\midipedal.ino:58:5: note: suggested alternative: 'note2'
noteOn(0x94, note2, 0x45);
^~~~~~
note2
midipedal:68:49: error: a function-definition is not allowed here before '{' token
void noteOn(char cmd, char data1, char data2) {
^
exit status 1
'noteOn' was not declared in this scope
所有这些错误都可以追溯到“函数定义是不允许的”。通过在互联网上研究这个问题,我发现这通常是由于不正确的缩进造成的。无论如何,我在我的代码中找不到任何丢失的括号或类似的东西,并且多次自动格式化所有内容。
有人能帮我吗?谢谢(:
发布于 2021-06-23 14:13:01
代码中的问题是在void loop()
函数中声明了void noteOn()
函数。您永远不应该在另一个函数中声明函数。您需要在void setup(),
之前或在void loop().
之后声明void noteOn()
函数,我已经修复了它。以下是工作代码:
const int switchPin1 = 2;
const int switchPin2 = 3;
// midi notes
char note1 = 60; //Middle C
char note2 = 62; //D
// variables
int switchState1 = LOW;
int switchState2 = LOW;
int currentSwitchState1 = LOW;
int currentSwitchState2 = LOW;
void noteOn(char cmd, char data1, char data2) { // Always declare it before void setup
Serial.print(cmd);
Serial.print(data1); Serial.print(data2);
}
void setup() {
// set the states of the I/O pins:
pinMode(switchPin1, INPUT);
pinMode(switchPin2, INPUT);
// set MIDI baud rate :
Serial.begin(31250);
}
void loop() {
currentSwitchState1 = digitalRead(switchPin1);
currentSwitchState2 = digitalRead(switchPin2);
// button 1 push
if (currentSwitchState1 == HIGH && switchState1 == LOW) {
noteOn(0x94, note1, 0x45);
delay(100);
noteOn(0x94, note1, 0x00);
switchState1 = HIGH;
}
// button 1 release
if (currentSwitchState1 == LOW && switchState1 == HIGH) {
switchState1 = LOW;
}
// button 2 push
if (currentSwitchState2 == HIGH && switchState2 == LOW) {
noteOn(0x94, note2, 0x45);
delay(100);
noteOn(0x94, note2, 0x00);
switchState2 = HIGH;
}
// button 2 release
if (currentSwitchState2 == LOW && switchState2 == HIGH) {
switchState2 = LOW;
}
}
https://stackoverflow.com/questions/68101356
复制