首页 > 解决方案 > Arduino IDE:在“{”标记之前不允许函数定义

问题描述

我为我的家庭工作室准备了一个 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

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

谁能帮我这个?谢谢 (:

标签: arduinoarduino-unomidiarduino-ide

解决方案


您的代码中的问题是您在void noteOn()函数内部声明了该void loop()函数。你永远不应该在另一个函数中声明一个函数。您需要在我修复它之前或之后声明该void noteOn()函数。这是工作代码:void setup(),void loop().

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;
  }


}

推荐阅读