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

Posted

技术标签:

【中文标题】Arduino IDE:在“”标记之前不允许函数定义【英文标题】:Arduino IDE: a function-definition is not allowed here before '' tokenArduino IDE:在“”标记之前不允许函数定义 【发布时间】:2021-06-23 14:00:35 【问题描述】:

我为我的家庭工作室准备了一个 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,我在我的代码中找不到任何丢失的括号或类似内容,并且我多次自动格式化所有内容。

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

【问题讨论】:

> code (that works flawlessly in the Arduino Uno by the way) ? ? ? 【参考方案1】:

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



【讨论】:

当然,谢谢!现在它正在工作(:

以上是关于Arduino IDE:在“”标记之前不允许函数定义的主要内容,如果未能解决你的问题,请参考以下文章

arduino开发板型号和端口如何设置

arduino IDE 主函数入口在哪?

关于Arduino Due开发板在Arduino IDE串口打印问题解决(附Serial函数整理)

进阶之路(基础篇) - 020 放弃Arduino IDE,拥抱Sublime Text 3

arduino ide 环境esp8266 01s板bitset如何使用

Arduino软件IDE简介