首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Arduino IDE:此处不允许在'{‘标记前使用函数定义

Arduino IDE:此处不允许在'{‘标记前使用函数定义
EN

Stack Overflow用户
提问于 2021-06-23 22:00:36
回答 1查看 147关注 0票数 0

我的家庭演播室有一个DIY midi脚踏板,两个按钮被编程为发送信号到MIDI输出,然后我的DAW接收并解释为MIDI控制器。

一切都像现在一样工作,但是现在Arduino IDE突然开始在我的代码中发现问题,因此我无法对代码进行任何编辑以添加更多功能。

这是代码(顺便说一下,它在Arduino Uno中完美地工作):

代码语言:javascript
运行
复制
// 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);
  }
}

这是我得到的错误日志:

代码语言:javascript
运行
复制
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

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

有人能帮我吗?谢谢(:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-23 22:13:01

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

代码语言:javascript
运行
复制
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;
  }


}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68101356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档