在C中得到一个错误,指出函数定义在此之前是不允许的';{';代币

在C中得到一个错误,指出函数定义在此之前是不允许的';{';代币,c,arduino,microcontroller,C,Arduino,Microcontroller,我为Arduino板制作了一个小C程序,通过压电蜂鸣器播放一些音调。我对它进行了调整,尝试从串行监视器获取输入,使用for循环迭代输入,并使用if/if-else播放相应的音调 不幸的是,我遇到了一个无法解决的问题,因为我对C还是一个新手。如果你有时间,你介意为我指出正确的方向来解决它吗 提前谢谢 我得到一个错误: /赋值_02_V2.ino:在函数“void loop()”中: /赋值_02_V2.ino:38:16:错误:此处不允许在“{”标记之前使用函数定义 int main(){ ^ 退

我为Arduino板制作了一个小C程序,通过压电蜂鸣器播放一些音调。我对它进行了调整,尝试从串行监视器获取输入,使用for循环迭代输入,并使用if/if-else播放相应的音调

不幸的是,我遇到了一个无法解决的问题,因为我对C还是一个新手。如果你有时间,你介意为我指出正确的方向来解决它吗

提前谢谢

我得到一个错误:

/赋值_02_V2.ino:在函数“void loop()”中: /赋值_02_V2.ino:38:16:错误:此处不允许在“{”标记之前使用函数定义

int main(){ ^ 退出状态1

代码如下:

#define noisyPin 11    // set pin 11 to "noisyPin"

// define notes
#define NOTE_A4 440   // set frequency of A
#define NOTE_B4 494   // set frequency of B
#define NOTE_C4 262   // set frequency of C
#define NOTE_D4 294   // set frequency of D
#define NOTE_E4 330   // set frequency of E
#define NOTE_F4 349   // set frequency of F
#define NOTE_G4 392   // set frequency of G
#define NOTE_C5 523   // set frequency to high C

// set strings for serial monitor input
String msgToUsr = "Enter your note selection (A-G). A space indicates a pause of 0.5 seconds: ";
String usrMelody;

void setup() {
    pinMode(13, OUTPUT);  // set the onboard LED to indicate when a sound is playing
    pinMode(noisyPin, OUTPUT);  // directs noisyPin (pin 3) to output
    Serial.begin(9600);
}

void loop() {
    Serial.println(msgToUsr);
    while (Serial.available()==0) { 
    }
  
    usrMelody = Serial.readString();
    melodyLength = strlen(usrMelody);
  
    int main() { 
        int i=0; 
        for (i = 1; i < melodyLength; i++) {
            if (i == c || i == C) {
                buzzer(noisyPin, NOTE_C4, 500);   // activate buzzer to play NOTE_C4 for 0.5 seconds
            }
            // delay(500);                       // set delay between HIGH to 0.5 seconds
            else if (i == d || i == D) {
                buzzer(noisyPin, NOTE_D4, 500);   // activate buzzer to play NOTE_D4 for 0.5 seconds
            }
            // delay(500);                       // set delay between HIGH to 0.5 seconds
            else if (i == e || i == E) {
                buzzer(noisyPin, NOTE_E4, 500);   // activate buzzer to play NOTE_E4 for 0.5 seconds
            }
            // delay(500);                       // set delay between HIGH to 0.5 seconds
            else if (i == f || i == F) {
                buzzer(noisyPin, NOTE_F4, 500);   // activate buzzer to play NOTE_F4 for 0.5 seconds
            }
            // delay(500);                       // set delay between HIGH to 0.5 seconds
            else if (i == g || i == G) {
                buzzer(noisyPin, NOTE_G4, 500);   // activate buzzer to play NOTE_G4 for 0.5 seconds
            }
            // delay(500);                       // set delay between HIGH to 0.5 seconds
            else if (i == a || i == A) {
                buzzer(noisyPin, NOTE_A4, 500);   // activate buzzer to play NOTE_A4 for 0.5 seconds
            }
            // delay(500);                       // set delay between HIGH to 0.5 seconds
            else  if (i == b || i == B) {
                buzzer(noisyPin, NOTE_B4, 500);   // activate buzzer to play NOTE_B4 for 0.5 seconds
            }
            // delay(500);                       // set delay between HIGH to 0.5 seconds
            else if (i == " ") {
            delay(500)
            }
        } 
    return 0; 
    }
} 

void buzzer(int targetPin, long frequency, long length) {
    digitalWrite(13, HIGH); // turn the onboard LED on
    long delayValue = 1000000 / frequency / 2; // calculate delay value
    // 1 second (in microseconds) divided by frequency divided into 2 (two phases in every cycle)
    long cycleCount = frequency * length / 1000; // calculate the total number of cycles
    // frequency (cycles per second) multiply by the number of seconds divded by 1 second
    for (long t = 0; t < cycleCount; t++) { // for loop to play sound based on the calculations above
        digitalWrite(targetPin, HIGH); // set pin to HIGH to play sound
        delayMicroseconds(delayValue);  // delay sound based on calculated delay value above
        digitalWrite(targetPin, LOW); // set pin to HIGH to stop sound
        delayMicroseconds(delayValue);  // delay sound based on calculated delay value above
    }
    digitalWrite(13, LOW); // turn the onboard LED off
}
#定义noisepin 11//将插脚11设置为“noisepin”
//定义注释
#定义注释\u A4 440//设置
#定义注释\u B4 494//设置B的频率
#定义注释\u C4 262//设置C的频率
#定义注释\u D4 294//设置D的频率
#定义注释\u E4 330//设置E的频率
#定义注释\u F4 349//设置F的频率
#定义注释\u G4 392//设置G的频率
#定义注释_C5523//将频率设置为高C
//设置串行监视器输入的字符串
String msgToUsr=“输入您的备忘选择(A-G)。空格表示暂停0.5秒:”;
字符串usrMelody;
无效设置(){
pinMode(13,输出);//设置板载LED指示何时播放声音
pinMode(noisepin,OUTPUT);//将noisepin(pin3)指向输出
Serial.begin(9600);
}
void循环(){
串行打印LN(msgToUsr);
而(Serial.available()==0){
}
usrMelody=Serial.readString();
旋律长度=strleng(usrMelody);
int main(){
int i=0;
对于(i=1;i
在C中不能有嵌套函数。在代码中,主函数写在循环函数中


检查一下,关于这个问题,

你在哪里得到了一个想法,你甚至需要在代码<循环> /COD>函数中定义一个<代码>主< /代码>函数?这不是有效的C++语法——错误消息甚至清楚地告诉你:<代码>函数定义不允许在这里 >{::
i
是一个整数(您可能希望将其用作索引)
c
是一个未定义的标识符,
c
@kaylum也是一个未定义的标识符,正如我所提到的,我对c还不熟悉,仍在努力解决它。在搜索之后,我意识到我不能有嵌套的函数。@wildplasser所以我先设置I=0,然后使用它进行迭代的方法是错误的?[除了嵌套的函数故障之外]您可能需要类似于
if(usrMelody[i]='c'| | usrMelody[i]='c'){…}
BTW:这是
开关()的一个很好的例子
statement。谢谢,我试图弄清楚整个结构,在发布这个问题的同时意识到了这一点。我会看看那个链接。@Andy perfect!记住,即使你正在编写arduino,也不能违反C的规则。(如果你能将答案标记为solven,那就太好了)@Lor