Arduino IDE:此处不允许在“{”标记之前定义函数

Arduino IDE: a function-definition is not allowed here before '{' token

我有一个 DIY midi-foot-pedal 用于我的家庭工作室,有两个按钮被编程为向 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

据我所知,所有这些错误都可以追溯到“不允许定义函数”。通过在互联网上研究问题,我发现这通常是由于缩进不当造成的。 Homever,我在我的代码中找不到任何丢失的括号或类似的东西,而且我多次自动格式化所有内容。

谁能帮我解决这个问题?谢谢 (:

您的代码中的问题是您在 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;
  }


}